Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, Body, Depends | |
| from sqlalchemy import create_engine, Column, Integer, String | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker | |
| from fastapi.responses import HTMLResponse | |
| from fastapi import Depends | |
| import gradio as gr | |
| import os | |
| import logging | |
| app = FastAPI() | |
| # Use an absolute path for the SQLite database file | |
| database_path = os.path.join(os.getcwd(), "test.db") | |
| if not os.path.exists(database_path): | |
| open(database_path, 'w').close() | |
| SQLALCHEMY_DATABASE_URL = f"sqlite:///{database_path}" | |
| # Set up logging for debugging | |
| logging.basicConfig(level=logging.DEBUG) | |
| engine = create_engine(SQLALCHEMY_DATABASE_URL) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| class Item(Base): | |
| __tablename__ = "items" | |
| id = Column(Integer, primary_key=True, index=True) | |
| category = Column(String, index=True) | |
| value = Column(Integer) | |
| Base.metadata.create_all(bind=engine) | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| def create_gr_chart(data): | |
| chart = gr.LineChart(data=data) | |
| return chart.launch() | |
| async def store_data(item: Item, db: Session = Depends(get_db)): | |
| db.add(item) | |
| db.commit() | |
| db.refresh(item) | |
| return item | |
| async def show_visualization(): | |
| # Fetch data from the database and create a Gradio chart | |
| data = get_data_from_db() # Replace with your function to fetch data from the database | |
| chart = create_gr_chart(data) # Replace with your function to create a Gradio chart | |
| return chart | |