import json import gradio as gr from fastapi import FastAPI, HTTPException from pydantic import BaseModel from fastapi.responses import HTMLResponse # Store the lists data (in-memory database) lists = {} # Define a model for the incoming request data class DataRequest(BaseModel): list_name: str items: list # For Hugging Face Spaces, we need to define the Gradio app first with gr.Blocks() as demo: # Create a simple interface to show the data gr.Markdown("# UniShare Data Viewer") output_text = gr.Textbox(label="Stored Data") def show_data(): return json.dumps(lists, indent=4) refresh_btn = gr.Button("Refresh Data") refresh_btn.click(fn=show_data, inputs=[], outputs=[output_text]) # Initialize with current data demo.load(fn=show_data, inputs=[], outputs=[output_text]) # Create FastAPI app app = FastAPI() # Endpoint for storing 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}"} # Endpoint to get data @app.get("/api/data") async def get_data(): return lists # HTML interface endpoint @app.get("/app", response_class=HTMLResponse) def read_root(): return """