Spaces:
Runtime error
Runtime error
Update app.py
Browse files148 unmodified lines1491501511521531541551561571581591601611621631641651661671681691701714 unmodified lines17617717817918018119 unmodified lines201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237169 unmodified lines407408409410411412413293 unmodified lines707708709710711712713714107 unmodified lines822823824825826827828252 unmodified lines1081108210831084108510861087148 unmodified lines@app.post("/validate-user")async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):status,role = verify_user(username, password)if status == 'success':logging.info(f"user role {role}is rerturned")response = RedirectResponse(url="/dashboard", status_code=302)response.set_cookie(key="role", value=role)response.set_cookie(key="username", value=username)return responseelse:return templates.TemplateResponse("index.html", {"request": request})@app.post("/submit_company_profile")async def submit_company_profile(request: Request,company_name: str = Form(...),company_code: str = Form(...),domain: str = Form(...),llm_tools: List[str] = Form(...)):logging.info("Received form submission for company profile")logging.info(f"Form data - company_name: {company_name}, company_code: {company_code}, domain: {domain}, llm_tools: {llm_tools}")4 unmodified linesvalues = (company_name, company_code, domain, ",".join(llm_tools))logging.info(f"Executing query: {query} with values: {values}")cursor.execute(query, values)cnx.commit()logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")cursor.close()19 unmodified linesexcept mysql.connector.Error as err:logging.error(f"Database error: {err}")raise HTTPException(status_code=500, detail="Internal Server Error")@app.get("/dashboard")async def dashboard(request: Request):try:cnx = get_db_connection()cursor = cnx.cursor()cursor.execute("show tables")all_tables = cursor.fetchall()table_count_of_each_table = {}# Fetch count of each table in the databasefor table in all_tables:query = f"SELECT COUNT(*) FROM {table[0]}"cursor.execute(query)table_count_of_each_table[table[0]] = cursor.fetchone()[0]cursor.close()cnx.close()logging.info(table_count_of_each_table)# return templates.TemplateResponse("dashboard.html", {"request": request,"title":"Dashboard" , "table_count_of_each_table": table_count_of_each_table})return templates.TemplateResponse("dashboard.html", {"request": request,"title": "Dashboard","table_count_of_each_table": table_count_of_each_table})except mysql.connector.Error as err:logging.error(f"Database error: {err}")raise HTTPException(status_code=500, detail="Internal Server Error")@app.get("/company_profile")async def company_profile(request: Request):return templates.TemplateResponse("company_profile.html", {"request": request,"title":"Company Profile"})@app.get("/api/company_id")async def get_company_id(company_name: str):print(f"Received company_name: {company_name}") # Debug statement169 unmodified lines@app.get("/knowledgebase")async def knowledgebase(request: Request):return templates.TemplateResponse("knowledgebase.html", {"request": request,"title":"KnowledgeBase"})#to insert into knowledgebase@app.post("/upload_document")293 unmodified lines@app.get("/data_connectors")async def data_connectors(request: Request):return templates.TemplateResponse("data_connectors.html", {"request": request, "title": "Data Connectors"})#to insert into data_connectors@app.post("/save_data_connectors")async def save_data_connectors( request: Request,107 unmodified lines@app.get("/API_connectors")async def API_connectors(request: Request):return templates.TemplateResponse("API_connectors.html", {"request": request,"title":"API Connectors"})#save api connectors@app.post("/api/save_api_details")async def API_saveconnectors(request: Request,252 unmodified lines@app.get("/prompt_template")async def prompt_template(request: Request):return templates.TemplateResponse("prompt_template.html", {"request": request,"title":"Prompt Templates"})# to insert into prompt templates@app.post("/api/save_prompt_details")async def prompt_saveconnectors(request: Request,148 unmodified lines1491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791804 unmodified lines18518618718818919019119219319419519619719819919 unmodified lines219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333169 unmodified lines503504505506507508509510511512513514515516517518519520521522523524525293 unmodified lines819820821822823824825826827828829830831832833834835836837838839840841107 unmodified lines949950951952953954955956957958959960961962963964965966967968969970971252 unmodified lines1224122512261227122812291230123112321233123412351236123712381239124012411242124312441245148 unmodified lines@app.post("/validate-user")async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):status, role ,company_id= verify_user(username, password)if status == 'success' and role and company_id:logging.info(f"user role {role} is returned")# Set cookies and redirect to the dashboardresponse = RedirectResponse(url="/dashboard", status_code=302)response.set_cookie(key="role", value=role)response.set_cookie(key="username", value=username)response.set_cookie(key="company_id",value=company_id)return responseelse:# If login fails, redirect back to the index page with an error messagereturn templates.TemplateResponse("index.html", {"request": request,"error": "Invalid username or password"})@app.post("/submit_company_profile")async def submit_company_profile(request: Request,company_name: str = Form(...),company_code: str = Form(...),domain: str = Form(...),llm_tools: List[str] = Form(...),username:str=Form(...),password:str=Form(...),role:str=Form(...)):logging.info("Received form submission for company profile")logging.info(f"Form data - company_name: {company_name}, company_code: {company_code}, domain: {domain}, llm_tools: {llm_tools}")4 unmodified linesvalues = (company_name, company_code, domain, ",".join(llm_tools))logging.info(f"Executing query: {query} with values: {values}")cursor.execute(query, values)# Retrieve the inserted company_idcompany_id = cursor.lastrowidlogging.info(f"Company profile for {company_name} inserted successfully with company_id: {company_id}")# Insert user details with the retrieved company_iduser_query = "INSERT INTO user_detail (company_id, username, password) VALUES (%s, %s, %s, %s)"user_values = (company_id, username, password, role)logging.info(f"Executing user detail query: {user_query} with values: {user_values}")cursor.execute(user_query, user_values)cnx.commit()logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")cursor.close()19 unmodified linesexcept mysql.connector.Error as err:logging.error(f"Database error: {err}")raise HTTPException(status_code=500, detail="Internal Server Error")@app.get("/dashboard")async def dashboard(request: Request):try:# Retrieve cookiesrole = request.cookies.get("role")username = request.cookies.get("username")company_id=request._cookies.get("company_id")# Establish database connectioncnx = get_db_connection()cursor = cnx.cursor()# Fetch all table namescursor.execute("SHOW TABLES")all_tables = cursor.fetchall()# Dictionary to hold the count of records for each tabletable_count_of_each_table = {}# Fetch count of records for each tablefor table in all_tables:table_name = table[0]query = f"SELECT COUNT(*) FROM {table_name} WHERE company_id = %s"cursor.execute(query, (company_id,))count = cursor.fetchone()[0]table_count_of_each_table[table_name] = countquery1=f"select company_name from company_detail where company_id = %s"cursor.execute(query1,(company_id,))company_name_result = cursor.fetchone()# Check if company_name_result is not Noneif company_name_result:company_name = company_name_result[0]else:company_name = "Unknown" # Default# Close cursor and connectioncursor.close()cnx.close()# Log the counts for debugging purposeslogging.info(table_count_of_each_table)# Render the template with the data, role, and usernamereturn templates.TemplateResponse("dashboard.html", {"request": request,"title": "Dashboard","table_count_of_each_table": table_count_of_each_table,"role": role,"username": username,"company_id":company_id,"company_name":company_name})except mysql.connector.Error as err:# Log the error and raise an HTTPExceptionlogging.error(f"Database error: {err}")raise HTTPException(status_code=500, detail="Internal Server Error")@app.get("/api/company_record_count/{company_id}")async def get_company_record_count(company_id: int):try:# Establish database connectioncnx = get_db_connection()cursor = cnx.cursor()# List of tables to count records intables = ["knowledge_base", "data_connectors", "api_connectors", "prompt_templates"]# Dictionary to hold the count of records for each tabletable_counts = {}# Fetch count of records for the selected company in each tablefor table in tables:query = f"SELECT COUNT(*) FROM {table} WHERE company_id = %s"cursor.execute(query, (company_id,))count = cursor.fetchone()[0]table_counts[table] = count# Close cursor and connectioncursor.close()cnx.close()return {"table_counts": table_counts}except mysql.connector.Error as err:logging.error(f"Database error: {err}")raise HTTPException(status_code=500, detail="Internal Server Error")@app.get("/company_profile")async def company_profile(request: Request):try:# Retrieve cookiesrole = request.cookies.get("role")company_id = request.cookies.get("company_id")# Render the template with the role and company_idreturn templates.TemplateResponse("company_profile.html", {"request": request,"role": role,"company_id": company_id,"title":"Company Profile"})except Exception as e:# Handle exceptionslogging.error(f"Error: {e}")raise HTTPException(status_code=500, detail="Internal Server Error")#return templates.TemplateResponse("company_profile.html", {"request": request,"title":"Company Profile"})@app.get("/api/company_id")async def get_company_id(company_name: str):print(f"Received company_name: {company_name}") # Debug statement169 unmodified lines@app.get("/knowledgebase")async def knowledgebase(request: Request):try:# Retrieve cookiesrole = request.cookies.get("role")company_id = request.cookies.get("company_id")# Render the template with the role and company_idreturn templates.TemplateResponse("knowledgebase.html", {"request": request,"role": role,"company_id": company_id,"title":"KnowledgeBase"})except Exception as e:# Handle exceptionslogging.error(f"Error: {e}")raise HTTPException(status_code=500, detail="Internal Server Error")#to insert into knowledgebase@app.post("/upload_document")293 unmodified lines@app.get("/data_connectors")async def data_connectors(request: Request):try:# Retrieve cookiesrole = request.cookies.get("role")company_id = request.cookies.get("company_id")# Render the template with the role and company_idreturn templates.TemplateResponse("data_connectors.html", {"request": request,"role": role,"company_id": company_id,"title": "Data Connectors"})except Exception as e:# Handle exceptionslogging.error(f"Error: {e}")raise HTTPException(status_code=500, detail="Internal Server Error")#to insert into data_connectors@app.post("/save_data_connectors")async def save_data_connectors( request: Request,107 unmodified lines@app.get("/API_connectors")async def API_connectors(request: Request):try:# Retrieve cookiesrole = request.cookies.get("role")company_id = request.cookies.get("company_id")# Render the template with the role and company_idreturn templates.TemplateResponse("API_connectors.html", {"request": request,"role": role,"company_id": company_id,"title":"API Connectors"})except Exception as e:# Handle exceptionslogging.error(f"Error: {e}")raise HTTPException(status_code=500, detail="Internal Server Error")#save api connectors@app.post("/api/save_api_details")async def API_saveconnectors(request: Request,252 unmodified lines@app.get("/prompt_template")async def prompt_template(request: Request):try:# Retrieve cookiesrole = request.cookies.get("role")company_id = request.cookies.get("company_id")# Render the template with the role and company_idreturn templates.TemplateResponse("prompt_template.html", {"request": request,"role": role,"company_id": company_id,"title":"Prompt Templates"})except Exception as e:# Handle exceptionslogging.error(f"Error: {e}")raise HTTPException(status_code=500, detail="Internal Server Error")# to insert into prompt templates@app.post("/api/save_prompt_details")async def prompt_saveconnectors(request: Request,
app.py+179-21