File size: 1,030 Bytes
4cb5616
 
 
 
a9e618c
 
4cb5616
a9e618c
63f6444
4cb5616
a6bd9d9
0409ba9
4cb5616
 
 
3412fa3
4cb5616
 
 
 
 
 
 
 
 
 
 
fb42686
 
63f6444
 
fb42686
05feaa2
0409ba9
4cb5616
 
e08a364
4cb5616
 
fb42686
4cb5616
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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=["*"],
)

@app.post("/endpoint")
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)