import json from fastapi import FastAPI, HTTPException import gradio as gr from pydantic import BaseModel from fastapi.responses import HTMLResponse app = FastAPI() # Define a model for the incoming request data class DataRequest(BaseModel): list_name: str items: list # Store the lists data lists = {} # FastAPI endpoint for POST request to store data @app.post("/store_data") async def store_data(data: DataRequest): list_name = data.list_name items = data.items if list_name not in lists: lists[list_name] = [] lists[list_name].append(items) return {"message": f"Data stored successfully for {list_name}"} # FastAPI default root to serve custom HTML @app.get("/", response_class=HTMLResponse) def read_root(): return """ UniShare
UniShare
""" # FastAPI endpoint to get data @app.get("/gradio/data") async def get_data(): return lists # Define Gradio interface def show_data(): return json.dumps(lists, indent=4) # Return the data as formatted JSON # Create the Gradio interface gradio_app = gr.Interface(fn=show_data, inputs=[], outputs="text") # Mount the Gradio app to FastAPI app = gr.mount_gradio_app(app, gradio_app, path="/gradio") # Add this if you're running this script directly if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)