Raz Tsook commited on
Commit
7ec55dc
ยท
1 Parent(s): d3c04e4

Complete rewrite - Pure FastAPI with HTML interface, no Gradio

Browse files
Files changed (2) hide show
  1. app.py +139 -86
  2. requirements.txt +1 -2
app.py CHANGED
@@ -1,5 +1,5 @@
1
- import gradio as gr
2
  from fastapi import FastAPI, HTTPException
 
3
  from pydantic import BaseModel
4
  import uvicorn
5
  import logging
@@ -44,29 +44,6 @@ def process_any_json(data: dict) -> str:
44
  async def api_process_json(request: AnyRequest):
45
  """
46
  API endpoint ืฉืžืงื‘ืœ ื›ืœ JSON ื•ืžื—ื–ื™ืจ ื”ื•ื“ืขื” ืขื ื”ื ืชื•ื ื™ื ืฉื”ืชืงื‘ืœื•.
47
-
48
- Usage:
49
- POST /api/process
50
- {
51
- "name": "John",
52
- "age": 30,
53
- "city": "Tel Aviv",
54
- "numbers": [1, 2, 3],
55
- "anything": "you want"
56
- }
57
-
58
- Returns:
59
- {
60
- "message": "Response from server (in English): Received your JSON data successfully!",
61
- "received_data": {
62
- "name": "John",
63
- "age": 30,
64
- "city": "Tel Aviv",
65
- "numbers": [1, 2, 3],
66
- "anything": "you want"
67
- },
68
- "status": "success"
69
- }
70
  """
71
  try:
72
  # Get all the data from the request
@@ -93,69 +70,145 @@ async def health_check():
93
  """Health check endpoint"""
94
  return {"status": "healthy", "message": "Server is running"}
95
 
96
- # Gradio interface for testing
97
- def gradio_interface(json_text):
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)
111
-
112
- # Create response
113
- response = {
114
- "message": message,
115
- "received_data": data,
116
- "status": "success"
117
- }
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 - Simple and stable
127
- def create_interface():
128
- with gr.Blocks() as demo:
129
- gr.Markdown("# ๐Ÿ“ก JSON API Server")
130
- gr.Markdown("Send any JSON to the server and get a response!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131
 
132
- json_input = gr.Textbox(
133
- label="JSON Input",
134
- value='{"name": "John", "age": 30, "city": "Tel Aviv"}',
135
- lines=3
136
- )
137
-
138
- submit_btn = gr.Button("Send to Server")
139
-
140
- output = gr.Textbox(label="Server Response", lines=3)
141
- json_output = gr.Textbox(label="Complete JSON Response", lines=5)
142
-
143
- submit_btn.click(
144
- fn=gradio_interface,
145
- inputs=json_input,
146
- outputs=[output, json_output]
147
- )
148
-
149
- gr.Markdown("### Example JSON:")
150
- gr.Markdown('`{"test": "hello", "number": 123}`')
151
-
152
- return demo
153
-
154
- demo = create_interface()
155
-
156
- # Mount Gradio app to FastAPI
157
- app = gr.mount_gradio_app(app, demo, path="/")
 
 
 
 
 
 
 
158
 
159
  if __name__ == "__main__":
160
  # For local development
161
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
1
  from fastapi import FastAPI, HTTPException
2
+ from fastapi.responses import HTMLResponse
3
  from pydantic import BaseModel
4
  import uvicorn
5
  import logging
 
44
  async def api_process_json(request: AnyRequest):
45
  """
46
  API endpoint ืฉืžืงื‘ืœ ื›ืœ JSON ื•ืžื—ื–ื™ืจ ื”ื•ื“ืขื” ืขื ื”ื ืชื•ื ื™ื ืฉื”ืชืงื‘ืœื•.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  """
48
  try:
49
  # Get all the data from the request
 
70
  """Health check endpoint"""
71
  return {"status": "healthy", "message": "Server is running"}
72
 
73
+ # Simple HTML interface instead of Gradio
74
+ @app.get("/", response_class=HTMLResponse)
75
+ async def read_root():
76
+ html_content = """
77
+ <!DOCTYPE html>
78
+ <html>
79
+ <head>
80
+ <title>JSON API Server</title>
81
+ <meta charset="utf-8">
82
+ <style>
83
+ body {
84
+ font-family: Arial, sans-serif;
85
+ max-width: 800px;
86
+ margin: 0 auto;
87
+ padding: 20px;
88
+ background-color: #f5f5f5;
89
+ }
90
+ .container {
91
+ background: white;
92
+ padding: 30px;
93
+ border-radius: 10px;
94
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
95
+ }
96
+ h1 {
97
+ color: #333;
98
+ text-align: center;
99
+ margin-bottom: 10px;
100
+ }
101
+ .subtitle {
102
+ text-align: center;
103
+ color: #666;
104
+ margin-bottom: 30px;
105
+ }
106
+ textarea {
107
+ width: 100%;
108
+ height: 120px;
109
+ padding: 15px;
110
+ border: 2px solid #ddd;
111
+ border-radius: 5px;
112
+ font-family: monospace;
113
+ font-size: 14px;
114
+ box-sizing: border-box;
115
+ }
116
+ button {
117
+ background: #007bff;
118
+ color: white;
119
+ border: none;
120
+ padding: 12px 24px;
121
+ border-radius: 5px;
122
+ cursor: pointer;
123
+ font-size: 16px;
124
+ margin: 10px 0;
125
+ }
126
+ button:hover {
127
+ background: #0056b3;
128
+ }
129
+ .response {
130
+ margin-top: 20px;
131
+ padding: 15px;
132
+ border: 2px solid #28a745;
133
+ border-radius: 5px;
134
+ background: #f8fff9;
135
+ white-space: pre-wrap;
136
+ font-family: monospace;
137
+ }
138
+ .error {
139
+ border-color: #dc3545;
140
+ background: #fff8f8;
141
+ }
142
+ .example {
143
+ background: #f8f9fa;
144
+ padding: 15px;
145
+ border-radius: 5px;
146
+ margin: 10px 0;
147
+ border-left: 4px solid #007bff;
148
+ }
149
+ </style>
150
+ </head>
151
+ <body>
152
+ <div class="container">
153
+ <h1>๐Ÿ“ก JSON API Server</h1>
154
+ <p class="subtitle">Send any JSON to the server and get a response!</p>
155
+
156
+ <div>
157
+ <label for="jsonInput"><strong>JSON Input:</strong></label>
158
+ <textarea id="jsonInput" placeholder='Enter your JSON here...'>{"name": "John", "age": 30, "city": "Tel Aviv"}</textarea>
159
+ </div>
160
+
161
+ <button onclick="sendRequest()">Send to Server</button>
162
+
163
+ <div id="response" style="display: none;"></div>
164
+
165
+ <div class="example">
166
+ <strong>Example JSON:</strong><br>
167
+ <code>{"test": "hello", "number": 123}</code><br>
168
+ <code>{"user": {"name": "John"}, "items": ["apple", "banana"]}</code>
169
+ </div>
170
+
171
+ <div class="example">
172
+ <strong>API Endpoint:</strong> <code>/api/process</code><br>
173
+ <strong>Method:</strong> POST<br>
174
+ <strong>Content-Type:</strong> application/json
175
+ </div>
176
+ </div>
177
 
178
+ <script>
179
+ async function sendRequest() {
180
+ const jsonInput = document.getElementById('jsonInput');
181
+ const responseDiv = document.getElementById('response');
182
+
183
+ try {
184
+ const jsonData = JSON.parse(jsonInput.value);
185
+
186
+ const response = await fetch('/api/process', {
187
+ method: 'POST',
188
+ headers: {
189
+ 'Content-Type': 'application/json',
190
+ },
191
+ body: JSON.stringify(jsonData)
192
+ });
193
+
194
+ const result = await response.json();
195
+
196
+ responseDiv.className = 'response';
197
+ responseDiv.style.display = 'block';
198
+ responseDiv.textContent = JSON.stringify(result, null, 2);
199
+
200
+ } catch (error) {
201
+ responseDiv.className = 'response error';
202
+ responseDiv.style.display = 'block';
203
+ responseDiv.textContent = 'Error: ' + error.message;
204
+ }
205
+ }
206
+ </script>
207
+ </body>
208
+ </html>
209
+ """
210
+ return html_content
211
 
212
  if __name__ == "__main__":
213
  # For local development
214
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -1,5 +1,4 @@
1
- gradio==4.16.0
2
  fastapi==0.104.1
3
  uvicorn[standard]==0.24.0
4
  pydantic==2.5.0
5
- python-multipart>=0.0.9
 
 
1
  fastapi==0.104.1
2
  uvicorn[standard]==0.24.0
3
  pydantic==2.5.0
4
+ python-multipart>=0.0.9