Raz Tsook commited on
Commit 路
14e9c7a
1
Parent(s): 5372e99
Fix Gradio compatibility issues - stable versions and simplified interface
Browse files- app.py +19 -20
- requirements.txt +5 -5
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi import FastAPI, HTTPException
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
| 5 |
import logging
|
|
|
|
| 6 |
|
| 7 |
# Configure logging
|
| 8 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -10,8 +11,8 @@ logger = logging.getLogger(__name__)
|
|
| 10 |
|
| 11 |
# FastAPI app instance
|
| 12 |
app = FastAPI(
|
| 13 |
-
title="
|
| 14 |
-
description="API endpoint
|
| 15 |
version="1.0.0"
|
| 16 |
)
|
| 17 |
|
|
@@ -97,14 +98,13 @@ def gradio_interface(json_text):
|
|
| 97 |
"""Gradio interface wrapper"""
|
| 98 |
try:
|
| 99 |
if not json_text.strip():
|
| 100 |
-
return "
|
| 101 |
|
| 102 |
# Try to parse the JSON
|
| 103 |
-
import json
|
| 104 |
try:
|
| 105 |
data = json.loads(json_text)
|
| 106 |
except json.JSONDecodeError:
|
| 107 |
-
return "
|
| 108 |
|
| 109 |
# Process the data
|
| 110 |
message = process_any_json(data)
|
|
@@ -118,41 +118,40 @@ def gradio_interface(json_text):
|
|
| 118 |
|
| 119 |
response_json = json.dumps(response, indent=2, ensure_ascii=False)
|
| 120 |
|
| 121 |
-
return f"
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
-
return f"
|
| 125 |
|
| 126 |
# Create Gradio interface
|
| 127 |
with gr.Blocks(title="JSON API Server") as demo:
|
| 128 |
gr.Markdown("# 馃摗 JSON API Server")
|
| 129 |
-
gr.Markdown("
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
with gr.Column():
|
| 133 |
-
gr.Markdown("###
|
| 134 |
-
json_input = gr.
|
| 135 |
label="JSON Input",
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
)
|
| 140 |
-
submit_btn = gr.Button("
|
| 141 |
|
| 142 |
with gr.Column():
|
| 143 |
-
gr.Markdown("###
|
| 144 |
output = gr.Textbox(
|
| 145 |
-
label="
|
| 146 |
lines=4,
|
| 147 |
interactive=False
|
| 148 |
)
|
| 149 |
|
| 150 |
gr.Markdown("### JSON Response:")
|
| 151 |
-
json_output = gr.
|
| 152 |
-
label="
|
| 153 |
-
language="json",
|
| 154 |
interactive=False,
|
| 155 |
-
lines=
|
| 156 |
)
|
| 157 |
|
| 158 |
submit_btn.click(
|
|
|
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
| 5 |
import logging
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 11 |
|
| 12 |
# FastAPI app instance
|
| 13 |
app = FastAPI(
|
| 14 |
+
title="JSON API Server",
|
| 15 |
+
description="API endpoint that accepts any JSON and returns a response",
|
| 16 |
version="1.0.0"
|
| 17 |
)
|
| 18 |
|
|
|
|
| 98 |
"""Gradio interface wrapper"""
|
| 99 |
try:
|
| 100 |
if not json_text.strip():
|
| 101 |
+
return "Please enter JSON", "{}"
|
| 102 |
|
| 103 |
# Try to parse the JSON
|
|
|
|
| 104 |
try:
|
| 105 |
data = json.loads(json_text)
|
| 106 |
except json.JSONDecodeError:
|
| 107 |
+
return "Error: Invalid JSON format", "{}"
|
| 108 |
|
| 109 |
# Process the data
|
| 110 |
message = process_any_json(data)
|
|
|
|
| 118 |
|
| 119 |
response_json = json.dumps(response, indent=2, ensure_ascii=False)
|
| 120 |
|
| 121 |
+
return f"Server Response:\n{message}\n\nYour JSON was received and returned", response_json
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
+
return f"Error: {str(e)}", "{}"
|
| 125 |
|
| 126 |
# Create Gradio interface
|
| 127 |
with gr.Blocks(title="JSON API Server") as demo:
|
| 128 |
gr.Markdown("# 馃摗 JSON API Server")
|
| 129 |
+
gr.Markdown("Send any JSON to the server and get a response!")
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
with gr.Column():
|
| 133 |
+
gr.Markdown("### Enter JSON:")
|
| 134 |
+
json_input = gr.Textbox(
|
| 135 |
label="JSON Input",
|
| 136 |
+
value='{"name": "John", "age": 30, "city": "Tel Aviv"}',
|
| 137 |
+
lines=5,
|
| 138 |
+
placeholder="Enter your JSON here..."
|
| 139 |
)
|
| 140 |
+
submit_btn = gr.Button("Send to Server", variant="primary")
|
| 141 |
|
| 142 |
with gr.Column():
|
| 143 |
+
gr.Markdown("### Server Response:")
|
| 144 |
output = gr.Textbox(
|
| 145 |
+
label="Message",
|
| 146 |
lines=4,
|
| 147 |
interactive=False
|
| 148 |
)
|
| 149 |
|
| 150 |
gr.Markdown("### JSON Response:")
|
| 151 |
+
json_output = gr.Textbox(
|
| 152 |
+
label="Complete Response",
|
|
|
|
| 153 |
interactive=False,
|
| 154 |
+
lines=8
|
| 155 |
)
|
| 156 |
|
| 157 |
submit_btn.click(
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
gradio
|
| 2 |
-
fastapi
|
| 3 |
-
uvicorn[standard]
|
| 4 |
-
pydantic
|
| 5 |
-
python-multipart
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
fastapi==0.104.1
|
| 3 |
+
uvicorn[standard]==0.24.0
|
| 4 |
+
pydantic==2.5.0
|
| 5 |
+
python-multipart==0.0.6
|