From f90ab24a72d89b2bf6fbc44852a8dec88b19fb28 Mon Sep 17 00:00:00 2001 From: Ali Abdalla Date: Thu, 10 Mar 2022 06:33:18 +0200 Subject: [PATCH] API Docs Fixes (#787) * fix examples format * add related methods * black formatting * added copy button with styling * upgraded black locally and reformatted * add inline comments to related methods * added syntax highlighting * fix examples bug with directories * expanded to audio, video, and file --- gradio/routes.py | 9 +- gradio/templates/api_docs.html | 159 +++++++++++++++++++++++++++++---- 2 files changed, 150 insertions(+), 18 deletions(-) diff --git a/gradio/routes.py b/gradio/routes.py index 36385ce4e1..e7b3828b4e 100644 --- a/gradio/routes.py +++ b/gradio/routes.py @@ -223,8 +223,13 @@ def api_docs(request: Request): output_types_doc, output_types = get_types(outputs, "output") input_names = [type(inp).__name__ for inp in app.launchable.input_components] output_names = [type(out).__name__ for out in app.launchable.output_components] - if app.launchable.examples is not None: - sample_inputs = app.launchable.examples[0] + if isinstance(app.launchable.examples, list): + example = app.launchable.examples[0] + sample_inputs = [] + for index, example_input in enumerate(example): + sample_inputs.append( + app.launchable.input_components[index].preprocess_example(example_input) + ) else: sample_inputs = [ inp.generate_sample() for inp in app.launchable.input_components diff --git a/gradio/templates/api_docs.html b/gradio/templates/api_docs.html index 8a79e754b0..d850733816 100644 --- a/gradio/templates/api_docs.html +++ b/gradio/templates/api_docs.html @@ -22,6 +22,8 @@ + + @@ -286,23 +318,31 @@

Payload:

-
+
+

  {

    "data": [{%for i in range(0, len_inputs)%} {{ input_types[i] }}{% if i != len_inputs - 1 %} ,{% endif %}{%endfor%} ]

  }

+
+
{% if auth is not none %}

Note: This interface requires authentication. This means you will have to first post to the login api before you can post to the predict endpoint. See below for more info

{% endif %}

Response:

-
+
+

  {

    "data": [{%for i in range(0, len_outputs)%} {{ output_types[i] }}{% if i != len_outputs - 1 %} ,{% endif %}{%endfor%} ],

    "durations": [ float ], # the time taken for the prediction to complete

    "avg_durations": [ float ] # the average time taken for all predictions so far (used to estimate the runtime)

  }

+
+

Try it (live demo):

@@ -325,16 +365,9 @@ -
- {% if auth is not none %} - - - - - - - - +
+
+ {% if auth is not none %}

import requests


sess = requests.session()

@@ -363,11 +396,15 @@

r.json()

{% endif %} +
+
-
+
+
{% if auth is not none %}

curl -X POST -F 'username=USERNAME' -F 'password=PASSWORD'  -c cookies.txt

@@ -386,10 +423,14 @@ {% endif %}
- +
+
+
-
+
+
{% if auth is not none %}

// Will only work locally.


@@ -415,6 +456,9 @@ console.log(json_response) })

{% endif %} +
+
@@ -437,11 +481,20 @@

  }

+ + +
- + + \ No newline at end of file