Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -79,10 +79,20 @@ def create_gradio_app(api_spec, api_url):
|
|
79 |
print(names)
|
80 |
data_field = gr.State(value=names)
|
81 |
inputs.append(data_field)
|
|
|
82 |
if output_schema["type"] == "string":
|
83 |
-
output_component = gr.
|
84 |
outputs.append(output_component)
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
#else if there's multiple outputs
|
87 |
|
88 |
def predict(*args):
|
@@ -96,22 +106,24 @@ def create_gradio_app(api_spec, api_url):
|
|
96 |
payload["input"][key] = value
|
97 |
print(payload)
|
98 |
response = requests.post(api_url, headers={"Content-Type": "application/json"}, json=payload)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
return output_images
|
112 |
-
|
113 |
-
return gr.Interface(fn=predict, inputs=inputs, outputs=outputs if outputs else "label")
|
114 |
|
|
|
|
|
|
|
|
|
115 |
|
116 |
API_URL = "http://localhost:5000/predictions"
|
117 |
app = create_gradio_app(api_spec, API_URL)
|
|
|
79 |
print(names)
|
80 |
data_field = gr.State(value=names)
|
81 |
inputs.append(data_field)
|
82 |
+
print(output_schema)
|
83 |
if output_schema["type"] == "string":
|
84 |
+
output_component = gr.Textbox(label="Output")
|
85 |
outputs.append(output_component)
|
86 |
+
elif output_schema["type"] == "array":
|
87 |
+
if "format" in output_schema["items"]:
|
88 |
+
if(output_schema["items"]["format"] == "uri"):
|
89 |
+
output_component = gr.Gallery(label=output_schema["title"])
|
90 |
+
else:
|
91 |
+
output_component = gr.Textbox(label=output_schema["title"])
|
92 |
+
else:
|
93 |
+
output_component = gr.Textbox(label=output_schema["title"])
|
94 |
+
outputs.append(output_component)
|
95 |
+
outputs.append(data_field)
|
96 |
#else if there's multiple outputs
|
97 |
|
98 |
def predict(*args):
|
|
|
106 |
payload["input"][key] = value
|
107 |
print(payload)
|
108 |
response = requests.post(api_url, headers={"Content-Type": "application/json"}, json=payload)
|
109 |
+
print(response)
|
110 |
+
if response.status_code == 200:
|
111 |
+
json_response = response.json()
|
112 |
+
print(json_response)
|
113 |
+
if "status" in json_response and json_response["status"] == "failed":
|
114 |
+
raise gr.Error("Failed to generate image")
|
115 |
|
116 |
+
output_images = []
|
117 |
+
for output_uri in json_response["output"]:
|
118 |
+
base64_image = output_uri.replace("data:image/png;base64,", "")
|
119 |
+
image_data = base64.b64decode(base64_image)
|
120 |
+
image_stream = io.BytesIO(image_data)
|
121 |
+
output_images.append(Image.open(image_stream))
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
return output_images
|
124 |
+
else:
|
125 |
+
raise gr.Error("The submission failed!")
|
126 |
+
return gr.Interface(fn=predict, inputs=inputs, outputs=outputs if outputs else "textbox")
|
127 |
|
128 |
API_URL = "http://localhost:5000/predictions"
|
129 |
app = create_gradio_app(api_spec, API_URL)
|