CoolFace
Apppublic

MTabishS/Text2SQL

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
main.py128 linesDownload Raw Back to root
1# import uvicorn2# import threading3# from typing import List4# from pydantic import BaseModel5# from fastapi import FastAPI, HTTPException6 7# # importing util packages8# from config import DATABASE_PATH9# from utils import get_groq_response, read_sql_query, prompt10 11# # define request body using Pydantic12# class QueryRequest(BaseModel):13#     question: str14 15# class QueryResponse(BaseModel):16#     sql_query: str17#     data: List18 19# app = FastAPI()20 21# # FastAPI Endpoint for text2SQL generation22# @app.post("/generate-sql",response_model=QueryResponse)23# def generate_sql(query: QueryRequest):24#     try:25#         # generating the SQL query.26#         sql_query=get_groq_response(query.question,prompt)27 28#         # Ensure a valid SQL query was returned29#         if not sql_query:30#             raise HTTPException(status_code=400,detail='No valid SQL query')31 32#         #executing the SQL query33#         db_path=DATABASE_PATH34#         data=read_sql_query(sql_query,db_path)35 36#         # if no data is found37#         if not data:38#             raise HTTPException(status_code=400,detail='No data found')39 40#         return QueryResponse(sql_query=sql_query,data=data)41    42#     except Exception as e:43#         print(f'Error Occurred: {e}')44#         raise HTTPException(status_code=500, detail=str(e))45 46# # GET route for testing47# @app.get("/") 48# def read_root():49#     return {'message':'Welcome to Text2SQL API!'}50 51# # Running FastAPI app with uvicorn52# def run_fastapi():53#     uvicorn.run(app,host='0.0.0.0',port=8000)54 55# fastapi_thread=threading.Thread(target=run_fastapi)56# fastapi_thread.start()57 58 59 60import uvicorn61import threading62from typing import List63from pydantic import BaseModel64from fastapi import FastAPI, HTTPException65 66# importing util packages67from config import DATABASE_PATH68from utils import get_groq_response, read_sql_query, prompt69 70# define request body using Pydantic71class QueryRequest(BaseModel):72    question: str73 74class QueryResponse(BaseModel):75    sql_query: str76    data: List77 78app = FastAPI()79 80# FastAPI Endpoint for text2SQL generation81@app.post("/generate-sql", response_model=QueryResponse)82def generate_sql_post(query: QueryRequest):83    return generate_sql(query.question)84 85@app.get("/generate-sql", response_model=QueryResponse)86def generate_sql_get(question: str):87    return generate_sql(question)88 89# Common logic to handle both GET and POST90def generate_sql(question: str):91    try:92        # generating the SQL query93        sql_query = get_groq_response(question, prompt)94 95        # Ensure a valid SQL query was returned96        if not sql_query:97            raise HTTPException(status_code=400, detail="No valid SQL query")98 99        # executing the SQL query100        db_path = DATABASE_PATH101        data = read_sql_query(sql_query, db_path)102 103        # if no data is found104        if not data:105            raise HTTPException(status_code=400, detail="No data found")106 107        return QueryResponse(sql_query=sql_query, data=data)108 109    except Exception as e:110        raise HTTPException(status_code=500, detail=str(e))111 112# # GET route for testing113# @app.get("/") 114# def read_root():115#     return {'message':'Welcome to Text2SQL API!'}116 117# Running FastAPI app with uvicorn118def run_fastapi():119    uvicorn.run(app,host='0.0.0.0',port=8000)120 121fastapi_thread=threading.Thread(target=run_fastapi)122fastapi_thread.start()123 124 125 126 127 128