SalexAI commited on
Commit
d107d5c
·
verified ·
1 Parent(s): bfc7017

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,20 +1,36 @@
1
  import json
2
- from fastapi import FastAPI, HTTPException
3
  import gradio as gr
 
4
  from pydantic import BaseModel
5
  from fastapi.responses import HTMLResponse
6
 
7
- app = FastAPI()
 
8
 
9
  # Define a model for the incoming request data
10
  class DataRequest(BaseModel):
11
  list_name: str
12
  items: list
13
 
14
- # Store the lists data
15
- lists = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # FastAPI endpoint for POST request to store data
18
  @app.post("/store_data")
19
  async def store_data(data: DataRequest):
20
  list_name = data.list_name
@@ -26,7 +42,12 @@ async def store_data(data: DataRequest):
26
  lists[list_name].append(items)
27
  return {"message": f"Data stored successfully for {list_name}"}
28
 
29
- # FastAPI default root to serve custom HTML
 
 
 
 
 
30
  @app.get("/", response_class=HTMLResponse)
31
  def read_root():
32
  return """
@@ -195,7 +216,7 @@ def read_root():
195
  }
196
 
197
  // Fetch data and populate the lists dynamically
198
- fetch('/gradio/data')
199
  .then(response => response.json())
200
  .then(data => {
201
  if (Object.keys(data).length > 0) {
@@ -217,22 +238,5 @@ def read_root():
217
  </html>
218
  """
219
 
220
- # FastAPI endpoint to get data
221
- @app.get("/gradio/data")
222
- async def get_data():
223
- return lists
224
-
225
- # Define Gradio interface
226
- def show_data():
227
- return json.dumps(lists, indent=4) # Return the data as formatted JSON
228
-
229
- # Create the Gradio interface
230
- gradio_app = gr.Interface(fn=show_data, inputs=[], outputs="text")
231
-
232
- # Mount the Gradio app to FastAPI
233
- app = gr.mount_gradio_app(app, gradio_app, path="/gradio")
234
-
235
- # Add this if you're running this script directly
236
- if __name__ == "__main__":
237
- import uvicorn
238
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
  import json
 
2
  import gradio as gr
3
+ from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
  from fastapi.responses import HTMLResponse
6
 
7
+ # Store the lists data (in-memory database)
8
+ lists = {}
9
 
10
  # Define a model for the incoming request data
11
  class DataRequest(BaseModel):
12
  list_name: str
13
  items: list
14
 
15
+ # For Hugging Face Spaces, we need to define the Gradio app first
16
+ with gr.Blocks() as demo:
17
+ # Create a simple interface to show the data
18
+ gr.Markdown("# UniShare Data Viewer")
19
+ output_text = gr.Textbox(label="Stored Data")
20
+
21
+ def show_data():
22
+ return json.dumps(lists, indent=4)
23
+
24
+ refresh_btn = gr.Button("Refresh Data")
25
+ refresh_btn.click(fn=show_data, inputs=[], outputs=[output_text])
26
+
27
+ # Initialize with current data
28
+ demo.load(fn=show_data, inputs=[], outputs=[output_text])
29
+
30
+ # Create FastAPI app
31
+ app = FastAPI()
32
 
33
+ # Endpoint for storing data
34
  @app.post("/store_data")
35
  async def store_data(data: DataRequest):
36
  list_name = data.list_name
 
42
  lists[list_name].append(items)
43
  return {"message": f"Data stored successfully for {list_name}"}
44
 
45
+ # Endpoint to get data
46
+ @app.get("/api/data")
47
+ async def get_data():
48
+ return lists
49
+
50
+ # Root endpoint serving HTML interface
51
  @app.get("/", response_class=HTMLResponse)
52
  def read_root():
53
  return """
 
216
  }
217
 
218
  // Fetch data and populate the lists dynamically
219
+ fetch('/api/data')
220
  .then(response => response.json())
221
  .then(data => {
222
  if (Object.keys(data).length > 0) {
 
238
  </html>
239
  """
240
 
241
+ # This is crucial for Hugging Face Spaces
242
+ app = gr.mount_gradio_app(app, demo)