Spaces:
Running
Running
| from ast import Not | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from fastapi.middleware.cors import CORSMiddleware | |
| import pymysql | |
| import os | |
| from openai import OpenAI | |
| from langchain_openai import ChatOpenAI | |
| from fastapi.staticfiles import StaticFiles | |
| from AskQuestion1 import ask_question | |
| from manabUtils import responseFromManualGPC | |
| app = FastAPI() | |
| # Enable CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # or ["http://localhost:5173"] | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| class PromptRequest(BaseModel): | |
| text: str | |
| # Database connection | |
| # def get_db_connection(): | |
| # conn = pymysql.connect( | |
| # host="sql.freedb.tech", | |
| # user="freedb_manabb", | |
| # password="%YZN6TrPbM2k$ef", | |
| # database="freedb_manabdatabase", | |
| # port=3306, | |
| # autocommit=True | |
| # ) | |
| # return conn | |
| def get_db_connection(): | |
| conn = pymysql.connect( | |
| host="103.191.208.227", | |
| user="ypccjqbi_manab", | |
| password="&cAipZvL;c0A-19j", | |
| database="ypccjqbi_manabcommercial", | |
| port=3306, | |
| autocommit=True | |
| ) | |
| return conn | |
| #conn = get_db_connection() | |
| #cursor = conn.cursor() | |
| # conn = sqlite3.connect("manabDatabase.db", check_same_thread=False) | |
| # cursor = conn.cursor() | |
| # Create table | |
| #cursor.execute(""" | |
| #CREATE TABLE IF NOT EXISTS manabCommercial1 ( | |
| # id INT AUTO_INCREMENT PRIMARY KEY, | |
| # EFileNo VARCHAR(255) UNIQUE, | |
| # PRNo VARCHAR(255), | |
| # status TEXT, | |
| # completed TEXT | |
| #) | |
| #""") | |
| # cursor.execute(""" | |
| # CREATE TABLE IF NOT EXISTS manabCommercial1 ( | |
| # id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| # EFileNo TEXT UNIQUE, | |
| # PRNo TEXT, | |
| # status TEXT, | |
| # completed TEXT | |
| # ) | |
| # """) | |
| #conn.commit() | |
| #cursor.close() | |
| #conn.close() | |
| # Request model | |
| class RecordRequest(BaseModel): | |
| EFileNo: str | |
| PRNo: str | |
| status: str | |
| completed: str | |
| class StatusUpdate(BaseModel): | |
| status: str | |
| class CompletedUpdate(BaseModel): | |
| completed: str | |
| class ChatRequest(BaseModel): | |
| message: str | |
| class ManualGPCReq(BaseModel): | |
| question:str | |
| history:str | |
| choice:str | |
| def manab_respond(request: PromptRequest): | |
| try: | |
| response = client.responses.create( | |
| model="gpt-4o-mini", | |
| input=request.text, | |
| temperature=0.1 | |
| ) | |
| # SAFE ACCESS | |
| answer = response.output_text | |
| return {"answer": answer} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| #For chat bot | |
| def chat(req: ChatRequest, password:str): | |
| if password != "nrl@1234": | |
| return { | |
| "reply": "Sorry, you are not authorized to search.." | |
| } | |
| #raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| return { | |
| # "reply": f"You said: {req.message}" | |
| "reply": ask_question(req.message) | |
| } | |
| #Ask manual or GPC | |
| def resFromManualGPC(req: ManualGPCReq): | |
| try: | |
| answer, history = responseFromManualGPC( | |
| req.question, req.history, req.choice | |
| ) | |
| return { | |
| "reply": answer, | |
| "history": history | |
| } | |
| except Exception as error: | |
| return { | |
| "reply": f"Error: {error}", | |
| "history": [] | |
| } | |
| #Add record | |
| def add(data: RecordRequest, password: str): | |
| if password != "rani1111": | |
| return {"message": "Sorry, you are not authorized to add records."} | |
| # raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| # print(data) | |
| if len(data.status.split()) > 9000: | |
| return {"message": "Not updated, Status cannot exceed 9000 words."} | |
| # raise HTTPException(status_code=400, detail="Status cannot exceed 9000 words") | |
| try: | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| # 'INSERT INTO manabCommercial1 (EFileNo,PRNo,status,completed) VALUES (?,?,?,?)', | |
| 'INSERT INTO manabCommercial1 (EFileNo,PRNo,status,completed) VALUES (%s,%s,%s,%s)', | |
| (data.EFileNo, data.PRNo, data.status, data.completed) | |
| ) | |
| conn.commit() | |
| cursor.close() | |
| conn.close() | |
| except pymysql.IntegrityError: | |
| return {"message": "Not updated, FileNo already exists.."} | |
| # raise HTTPException(status_code=400, detail="EFileNo already exists") | |
| return {"message": "Inserted"} | |
| #Get all records | |
| def get(password: str): | |
| if password != "rani1111": | |
| return {"message": "Sorry, you are not authorized to view records."} | |
| # raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute('SELECT id,EFileNo,PRNo,status,completed FROM manabCommercial1') | |
| conn.commit() | |
| rows = cursor.fetchall() | |
| records = [] | |
| for r in rows: | |
| records.append({ | |
| "id": r[0], | |
| "EFileNo": r[1], | |
| "PRNo": r[2], | |
| "status": r[3], | |
| "completed": r[4] | |
| }) | |
| cursor.close() | |
| conn.close() | |
| return {"records": records} | |
| # Delete record | |
| def delete(record_id: int, password: str): | |
| if password != "rani1111": | |
| return {"message": "Sorry, you are not authorized to add records."} | |
| # raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| # cursor.execute("DELETE FROM manabCommercial1 WHERE id = ?", (record_id,)) | |
| cursor.execute("DELETE FROM manabCommercial1 WHERE id = %s", (record_id,)) | |
| conn.commit() | |
| if cursor.rowcount == 0: | |
| return {"message": "Error, Record not found."} | |
| # raise HTTPException(status_code=404, detail="Record not found") | |
| cursor.close() | |
| conn.close() | |
| return {"message": "Deleted"} | |
| #updateAll | |
| def update_all(record_id: int, password: str, data: RecordRequest): | |
| if password != "rani1111": | |
| return {"message": "Sorry, you are not authorized to add or updateAll records."} | |
| # raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| if len(data.status.split()) > 9000: | |
| return {"message": "Error, Status cannot exceed 9000 words."} | |
| # raise HTTPException(status_code=400, detail="Status cannot exceed 9000 words") | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| "UPDATE manabCommercial1 SET EFileNo = %s, PRNo = %s, status = %s, completed = %s WHERE id = %s", | |
| (data.EFileNo, data.PRNo, data.status, data.completed, record_id) | |
| ) | |
| conn.commit() | |
| if cursor.rowcount == 0: | |
| return {"message": "Error, Record not found."} | |
| # raise HTTPException(status_code=404, detail="Record not found") | |
| cursor.close() | |
| conn.close() | |
| return {"message": "Updated"} | |
| # Update status | |
| def update(EFileNo: str, password: str, data: StatusUpdate): | |
| if password != "rani1111": | |
| return {"message": "Sorry, you are not authorized to update status."} | |
| # raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| if len(data.status.split()) > 9000: | |
| return {"message": "Error, Status cannot exceed 9000 words."} | |
| # raise HTTPException(status_code=400, detail="Status cannot exceed 9000 words") | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| "UPDATE manabCommercial1 SET status = %s WHERE EFileNo = %s", | |
| (data.status, EFileNo) | |
| ) | |
| conn.commit() | |
| if cursor.rowcount == 0: | |
| return {"message": "Error, Record not found."} | |
| # raise HTTPException(status_code=404, detail="Record not found") | |
| cursor.close() | |
| conn.close() | |
| return {"message": "Updated"} | |
| # Update completed | |
| def update_completed(record_id: int, password: str, data: CompletedUpdate): | |
| if password != "rani1111": | |
| return {"message": "Sorry, you are not authorized to update records."} | |
| # raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| if len(data.completed.split()) > 9000: | |
| return {"message": "Error, Completed cannot exceed 9000 words."} | |
| #raise HTTPException(status_code=400, detail="Completed cannot exceed 9000 words") | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| "UPDATE manabCommercial1 SET completed = %s WHERE id = %s", | |
| (data.completed, record_id) | |
| ) | |
| conn.commit() | |
| if cursor.rowcount == 0: | |
| return {"message": "Error, Record not found....."} | |
| # raise HTTPException(status_code=404, detail="Record not found") | |
| cursor.close() | |
| conn.close() | |
| return {"message": "Updated"} | |
| # Search status text | |
| def searchInTable(query: str, password: str): | |
| if password != "rani1111": | |
| #return {"message": "Sorry, you are not authorized to search."} | |
| raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| f"{query}" | |
| ) | |
| conn.commit() | |
| rows = cursor.fetchall() | |
| records = [] | |
| for r in rows: | |
| records.append({ | |
| "id": r[0], | |
| "EFileNo": r[1], | |
| "PRNo": r[2], | |
| "status": r[3], | |
| "completed": r[4] | |
| }) | |
| cursor.close() | |
| conn.close() | |
| return {"records": records} | |
| def searchForCurrentFiles(password: str): | |
| if password != "nrl@1234": | |
| #return {"message": "Sorry, you are not authorized to search."} | |
| raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| query="SELECT * FROM manabCommercial1 WHERE completed='NotCompleted';" | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| f"{query}" | |
| ) | |
| conn.commit() | |
| rows = cursor.fetchall() | |
| records = [] | |
| for r in rows: | |
| records.append({ | |
| "id": r[0], | |
| "EFileNo": r[1], | |
| "PRNo": r[2], | |
| "status": r[3], | |
| "completed": r[4] | |
| }) | |
| cursor.close() | |
| conn.close() | |
| return {"records": records} | |
| def searchForCompletedFiles(password: str): | |
| if password != "nrl@1234": | |
| #return {"message": "Sorry, you are not authorized to search."} | |
| raise HTTPException(status_code=401, detail="Unauthorized") | |
| else: | |
| query="SELECT * FROM manabCommercial1 WHERE completed='Completed';" | |
| conn = get_db_connection() | |
| cursor = conn.cursor() | |
| cursor.execute( | |
| f"{query}" | |
| ) | |
| conn.commit() | |
| rows = cursor.fetchall() | |
| records = [] | |
| for r in rows: | |
| records.append({ | |
| "id": r[0], | |
| "EFileNo": r[1], | |
| "PRNo": r[2], | |
| "status": r[3], | |
| "completed": r[4] | |
| }) | |
| cursor.close() | |
| conn.close() | |
| return {"records": records} | |
| app.mount("/", StaticFiles(directory="dist", html=True), name="static") |