Raz Tsook commited on
Commit ยท
7ec55dc
1
Parent(s): d3c04e4
Complete rewrite - Pure FastAPI with HTML interface, no Gradio
Browse files- app.py +139 -86
- 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 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|