Spaces:
Runtime error
Runtime error
import gradio as gr | |
from fastapi import FastAPI | |
from fastapi.middleware.cors import CORSMiddleware | |
class Data: | |
def __init__(self, data): | |
self.data = [[data]] | |
def update(self): | |
new_data = f"New data - item: {len(self.data) + 1}" | |
self.data.append([new_data]) | |
return new_data | |
def get_data(self): | |
return self.data | |
app = FastAPI() | |
data_for_app = Data("initial data") | |
# Add CORS middleware | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
async def custom_endpoint(data: dict): | |
new = data_for_app.update(); | |
return {"message": "Received data", "data": new} | |
with gr.Blocks() as blocks: | |
gr.Dataframe(value=data_for_app.get_data, label="My Data") | |
# Mount Gradio app to root | |
app = gr.mount_gradio_app(app, blocks, path="/", ssr_mode=False) | |
# Custom POST endpoint | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=7860) |