Spaces:
Sleeping
Sleeping
File size: 1,713 Bytes
007a3ba 035d91e 007a3ba 035d91e 007a3ba 8557221 ad90944 007a3ba 5c6b733 007a3ba 035d91e 8557221 035d91e 007a3ba 035d91e 007a3ba | 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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()
@app.post("/form/")
async def store_data(item: Item, db: Session = Depends(get_db)):
db.add(item)
db.commit()
db.refresh(item)
return item
@app.get("/visualization/", response_class=HTMLResponse)
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
|