Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
|
| 5 |
+
class Data():
|
| 6 |
+
__init__(self, data):
|
| 7 |
+
self.data = [[data]]
|
| 8 |
+
update(self):
|
| 9 |
+
self.data.append([new_data])
|
| 10 |
+
get_data():
|
| 11 |
+
return self.data
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
data_for_app = new Data("initial data")
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Add CORS middleware
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"],
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Create a simple Gradio interface
|
| 27 |
+
def greet(name):
|
| 28 |
+
return f"Hello, {name}!"
|
| 29 |
+
|
| 30 |
+
gr.Blocks() as blocks:
|
| 31 |
+
gr.Dataframe(value=get_data, label="My Data")
|
| 32 |
+
|
| 33 |
+
# Mount Gradio app to root
|
| 34 |
+
app = gr.mount_gradio_app(app, interface, path="/")
|
| 35 |
+
|
| 36 |
+
# Custom POST endpoint
|
| 37 |
+
i = 0
|
| 38 |
+
@app.post("/endpoint")
|
| 39 |
+
async def custom_endpoint(data: dict):
|
| 40 |
+
i = i + 1
|
| 41 |
+
data_for_app.update(f"New data - item: {i}");
|
| 42 |
+
return {"message": "Received data", "data": data}
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
import uvicorn
|
| 46 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|