Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -149,23 +149,32 @@ def verify_user(username: str, password: str):
|
|
149 |
|
150 |
@app.post("/validate-user")
|
151 |
async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):
|
152 |
-
status,role = verify_user(username, password)
|
153 |
-
if status == 'success':
|
154 |
-
logging.info(f"user role {role}is
|
|
|
|
|
155 |
response = RedirectResponse(url="/dashboard", status_code=302)
|
156 |
response.set_cookie(key="role", value=role)
|
157 |
response.set_cookie(key="username", value=username)
|
|
|
158 |
return response
|
159 |
-
|
160 |
else:
|
161 |
-
|
162 |
-
|
|
|
|
|
|
|
|
|
163 |
@app.post("/submit_company_profile")
|
164 |
async def submit_company_profile(request: Request,
|
165 |
company_name: str = Form(...),
|
166 |
company_code: str = Form(...),
|
167 |
domain: str = Form(...),
|
168 |
-
llm_tools: List[str] = Form(...)
|
|
|
|
|
|
|
169 |
logging.info("Received form submission for company profile")
|
170 |
logging.info(f"Form data - company_name: {company_name}, company_code: {company_code}, domain: {domain}, llm_tools: {llm_tools}")
|
171 |
|
@@ -176,6 +185,15 @@ async def submit_company_profile(request: Request,
|
|
176 |
values = (company_name, company_code, domain, ",".join(llm_tools))
|
177 |
logging.info(f"Executing query: {query} with values: {values}")
|
178 |
cursor.execute(query, values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
cnx.commit()
|
180 |
logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")
|
181 |
cursor.close()
|
@@ -201,37 +219,115 @@ async def get_companies():
|
|
201 |
except mysql.connector.Error as err:
|
202 |
logging.error(f"Database error: {err}")
|
203 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
204 |
@app.get("/dashboard")
|
205 |
async def dashboard(request: Request):
|
206 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
cnx = get_db_connection()
|
208 |
cursor = cnx.cursor()
|
209 |
|
210 |
-
|
|
|
211 |
all_tables = cursor.fetchall()
|
|
|
|
|
212 |
table_count_of_each_table = {}
|
213 |
-
# Fetch count of each table in the database
|
214 |
-
for table in all_tables:
|
215 |
-
query = f"SELECT COUNT(*) FROM {table[0]}"
|
216 |
-
cursor.execute(query)
|
217 |
-
table_count_of_each_table[table[0]] = cursor.fetchone()[0]
|
218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
cursor.close()
|
220 |
-
cnx.close()
|
221 |
|
|
|
222 |
logging.info(table_count_of_each_table)
|
223 |
-
|
|
|
224 |
return templates.TemplateResponse("dashboard.html", {
|
225 |
"request": request,
|
226 |
"title": "Dashboard",
|
227 |
-
"table_count_of_each_table": table_count_of_each_table
|
|
|
|
|
|
|
|
|
228 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
except mysql.connector.Error as err:
|
230 |
logging.error(f"Database error: {err}")
|
231 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
|
|
|
|
232 |
@app.get("/company_profile")
|
233 |
async def company_profile(request: Request):
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
@app.get("/api/company_id")
|
236 |
async def get_company_id(company_name: str):
|
237 |
print(f"Received company_name: {company_name}") # Debug statement
|
@@ -407,7 +503,23 @@ async def delete_company(company_id: int):
|
|
407 |
|
408 |
@app.get("/knowledgebase")
|
409 |
async def knowledgebase(request: Request):
|
410 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
411 |
|
412 |
#to insert into knowledgebase
|
413 |
@app.post("/upload_document")
|
@@ -707,8 +819,23 @@ async def delete_company(company_id: int):
|
|
707 |
|
708 |
@app.get("/data_connectors")
|
709 |
async def data_connectors(request: Request):
|
710 |
-
|
|
|
|
|
|
|
711 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
712 |
#to insert into data_connectors
|
713 |
@app.post("/save_data_connectors")
|
714 |
async def save_data_connectors( request: Request,
|
@@ -822,7 +949,23 @@ async def get_data_connectors(company_id: str = Query(...), company_name: str =
|
|
822 |
|
823 |
@app.get("/API_connectors")
|
824 |
async def API_connectors(request: Request):
|
825 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
826 |
#save api connectors
|
827 |
@app.post("/api/save_api_details")
|
828 |
async def API_saveconnectors(request: Request,
|
@@ -1081,7 +1224,22 @@ def delete_api_from_db(company_id: int) -> bool:
|
|
1081 |
|
1082 |
@app.get("/prompt_template")
|
1083 |
async def prompt_template(request: Request):
|
1084 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1085 |
# to insert into prompt templates
|
1086 |
@app.post("/api/save_prompt_details")
|
1087 |
async def prompt_saveconnectors(request: Request,
|
|
|
149 |
|
150 |
@app.post("/validate-user")
|
151 |
async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):
|
152 |
+
status, role ,company_id= verify_user(username, password)
|
153 |
+
if status == 'success' and role and company_id:
|
154 |
+
logging.info(f"user role {role} is returned")
|
155 |
+
|
156 |
+
# Set cookies and redirect to the dashboard
|
157 |
response = RedirectResponse(url="/dashboard", status_code=302)
|
158 |
response.set_cookie(key="role", value=role)
|
159 |
response.set_cookie(key="username", value=username)
|
160 |
+
response.set_cookie(key="company_id",value=company_id)
|
161 |
return response
|
|
|
162 |
else:
|
163 |
+
# If login fails, redirect back to the index page with an error message
|
164 |
+
return templates.TemplateResponse("index.html", {
|
165 |
+
"request": request,
|
166 |
+
"error": "Invalid username or password"
|
167 |
+
})
|
168 |
+
|
169 |
@app.post("/submit_company_profile")
|
170 |
async def submit_company_profile(request: Request,
|
171 |
company_name: str = Form(...),
|
172 |
company_code: str = Form(...),
|
173 |
domain: str = Form(...),
|
174 |
+
llm_tools: List[str] = Form(...),
|
175 |
+
username:str=Form(...),
|
176 |
+
password:str=Form(...),
|
177 |
+
role:str=Form(...)):
|
178 |
logging.info("Received form submission for company profile")
|
179 |
logging.info(f"Form data - company_name: {company_name}, company_code: {company_code}, domain: {domain}, llm_tools: {llm_tools}")
|
180 |
|
|
|
185 |
values = (company_name, company_code, domain, ",".join(llm_tools))
|
186 |
logging.info(f"Executing query: {query} with values: {values}")
|
187 |
cursor.execute(query, values)
|
188 |
+
# Retrieve the inserted company_id
|
189 |
+
company_id = cursor.lastrowid
|
190 |
+
logging.info(f"Company profile for {company_name} inserted successfully with company_id: {company_id}")
|
191 |
+
|
192 |
+
# Insert user details with the retrieved company_id
|
193 |
+
user_query = "INSERT INTO user_detail (company_id, username, password) VALUES (%s, %s, %s, %s)"
|
194 |
+
user_values = (company_id, username, password, role)
|
195 |
+
logging.info(f"Executing user detail query: {user_query} with values: {user_values}")
|
196 |
+
cursor.execute(user_query, user_values)
|
197 |
cnx.commit()
|
198 |
logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")
|
199 |
cursor.close()
|
|
|
219 |
except mysql.connector.Error as err:
|
220 |
logging.error(f"Database error: {err}")
|
221 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
222 |
+
|
223 |
@app.get("/dashboard")
|
224 |
async def dashboard(request: Request):
|
225 |
try:
|
226 |
+
# Retrieve cookies
|
227 |
+
role = request.cookies.get("role")
|
228 |
+
username = request.cookies.get("username")
|
229 |
+
company_id=request._cookies.get("company_id")
|
230 |
+
|
231 |
+
# Establish database connection
|
232 |
cnx = get_db_connection()
|
233 |
cursor = cnx.cursor()
|
234 |
|
235 |
+
# Fetch all table names
|
236 |
+
cursor.execute("SHOW TABLES")
|
237 |
all_tables = cursor.fetchall()
|
238 |
+
|
239 |
+
# Dictionary to hold the count of records for each table
|
240 |
table_count_of_each_table = {}
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
+
# Fetch count of records for each table
|
243 |
+
for table in all_tables:
|
244 |
+
table_name = table[0]
|
245 |
+
query = f"SELECT COUNT(*) FROM {table_name} WHERE company_id = %s"
|
246 |
+
cursor.execute(query, (company_id,))
|
247 |
+
|
248 |
+
count = cursor.fetchone()[0]
|
249 |
+
table_count_of_each_table[table_name] = count
|
250 |
+
query1=f"select company_name from company_detail where company_id = %s"
|
251 |
+
cursor.execute(query1,(company_id,))
|
252 |
+
company_name_result = cursor.fetchone()
|
253 |
+
|
254 |
+
# Check if company_name_result is not None
|
255 |
+
if company_name_result:
|
256 |
+
company_name = company_name_result[0]
|
257 |
+
else:
|
258 |
+
company_name = "Unknown" # Default
|
259 |
+
# Close cursor and connection
|
260 |
cursor.close()
|
261 |
+
cnx.close()
|
262 |
|
263 |
+
# Log the counts for debugging purposes
|
264 |
logging.info(table_count_of_each_table)
|
265 |
+
|
266 |
+
# Render the template with the data, role, and username
|
267 |
return templates.TemplateResponse("dashboard.html", {
|
268 |
"request": request,
|
269 |
"title": "Dashboard",
|
270 |
+
"table_count_of_each_table": table_count_of_each_table,
|
271 |
+
"role": role,
|
272 |
+
"username": username,
|
273 |
+
"company_id":company_id,
|
274 |
+
"company_name":company_name
|
275 |
})
|
276 |
+
except mysql.connector.Error as err:
|
277 |
+
# Log the error and raise an HTTPException
|
278 |
+
logging.error(f"Database error: {err}")
|
279 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
280 |
+
|
281 |
+
@app.get("/api/company_record_count/{company_id}")
|
282 |
+
async def get_company_record_count(company_id: int):
|
283 |
+
try:
|
284 |
+
# Establish database connection
|
285 |
+
cnx = get_db_connection()
|
286 |
+
cursor = cnx.cursor()
|
287 |
+
|
288 |
+
# List of tables to count records in
|
289 |
+
tables = ["knowledge_base", "data_connectors", "api_connectors", "prompt_templates"]
|
290 |
+
|
291 |
+
# Dictionary to hold the count of records for each table
|
292 |
+
table_counts = {}
|
293 |
+
|
294 |
+
# Fetch count of records for the selected company in each table
|
295 |
+
for table in tables:
|
296 |
+
query = f"SELECT COUNT(*) FROM {table} WHERE company_id = %s"
|
297 |
+
cursor.execute(query, (company_id,))
|
298 |
+
count = cursor.fetchone()[0]
|
299 |
+
table_counts[table] = count
|
300 |
+
|
301 |
+
# Close cursor and connection
|
302 |
+
cursor.close()
|
303 |
+
cnx.close()
|
304 |
+
|
305 |
+
return {"table_counts": table_counts}
|
306 |
+
|
307 |
except mysql.connector.Error as err:
|
308 |
logging.error(f"Database error: {err}")
|
309 |
raise HTTPException(status_code=500, detail="Internal Server Error")
|
310 |
+
|
311 |
+
|
312 |
@app.get("/company_profile")
|
313 |
async def company_profile(request: Request):
|
314 |
+
try:
|
315 |
+
# Retrieve cookies
|
316 |
+
role = request.cookies.get("role")
|
317 |
+
company_id = request.cookies.get("company_id")
|
318 |
+
|
319 |
+
# Render the template with the role and company_id
|
320 |
+
return templates.TemplateResponse("company_profile.html", {
|
321 |
+
"request": request,
|
322 |
+
"role": role,
|
323 |
+
"company_id": company_id,
|
324 |
+
"title":"Company Profile"
|
325 |
+
})
|
326 |
+
except Exception as e:
|
327 |
+
# Handle exceptions
|
328 |
+
logging.error(f"Error: {e}")
|
329 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
330 |
+
#return templates.TemplateResponse("company_profile.html", {"request": request,"title":"Company Profile"})
|
331 |
@app.get("/api/company_id")
|
332 |
async def get_company_id(company_name: str):
|
333 |
print(f"Received company_name: {company_name}") # Debug statement
|
|
|
503 |
|
504 |
@app.get("/knowledgebase")
|
505 |
async def knowledgebase(request: Request):
|
506 |
+
try:
|
507 |
+
# Retrieve cookies
|
508 |
+
role = request.cookies.get("role")
|
509 |
+
company_id = request.cookies.get("company_id")
|
510 |
+
|
511 |
+
# Render the template with the role and company_id
|
512 |
+
return templates.TemplateResponse("knowledgebase.html", {
|
513 |
+
"request": request,
|
514 |
+
"role": role,
|
515 |
+
"company_id": company_id,
|
516 |
+
"title":"KnowledgeBase"
|
517 |
+
})
|
518 |
+
except Exception as e:
|
519 |
+
# Handle exceptions
|
520 |
+
logging.error(f"Error: {e}")
|
521 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
522 |
+
|
523 |
|
524 |
#to insert into knowledgebase
|
525 |
@app.post("/upload_document")
|
|
|
819 |
|
820 |
@app.get("/data_connectors")
|
821 |
async def data_connectors(request: Request):
|
822 |
+
try:
|
823 |
+
# Retrieve cookies
|
824 |
+
role = request.cookies.get("role")
|
825 |
+
company_id = request.cookies.get("company_id")
|
826 |
|
827 |
+
# Render the template with the role and company_id
|
828 |
+
return templates.TemplateResponse("data_connectors.html", {
|
829 |
+
"request": request,
|
830 |
+
"role": role,
|
831 |
+
"company_id": company_id,
|
832 |
+
"title": "Data Connectors"
|
833 |
+
})
|
834 |
+
except Exception as e:
|
835 |
+
# Handle exceptions
|
836 |
+
logging.error(f"Error: {e}")
|
837 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
838 |
+
|
839 |
#to insert into data_connectors
|
840 |
@app.post("/save_data_connectors")
|
841 |
async def save_data_connectors( request: Request,
|
|
|
949 |
|
950 |
@app.get("/API_connectors")
|
951 |
async def API_connectors(request: Request):
|
952 |
+
try:
|
953 |
+
# Retrieve cookies
|
954 |
+
role = request.cookies.get("role")
|
955 |
+
company_id = request.cookies.get("company_id")
|
956 |
+
|
957 |
+
# Render the template with the role and company_id
|
958 |
+
return templates.TemplateResponse("API_connectors.html", {
|
959 |
+
"request": request,
|
960 |
+
"role": role,
|
961 |
+
"company_id": company_id,
|
962 |
+
"title":"API Connectors"
|
963 |
+
})
|
964 |
+
except Exception as e:
|
965 |
+
# Handle exceptions
|
966 |
+
logging.error(f"Error: {e}")
|
967 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
968 |
+
|
969 |
#save api connectors
|
970 |
@app.post("/api/save_api_details")
|
971 |
async def API_saveconnectors(request: Request,
|
|
|
1224 |
|
1225 |
@app.get("/prompt_template")
|
1226 |
async def prompt_template(request: Request):
|
1227 |
+
try:
|
1228 |
+
# Retrieve cookies
|
1229 |
+
role = request.cookies.get("role")
|
1230 |
+
company_id = request.cookies.get("company_id")
|
1231 |
+
|
1232 |
+
# Render the template with the role and company_id
|
1233 |
+
return templates.TemplateResponse("prompt_template.html", {
|
1234 |
+
"request": request,
|
1235 |
+
"role": role,
|
1236 |
+
"company_id": company_id,
|
1237 |
+
"title":"Prompt Templates"
|
1238 |
+
})
|
1239 |
+
except Exception as e:
|
1240 |
+
# Handle exceptions
|
1241 |
+
logging.error(f"Error: {e}")
|
1242 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
1243 |
# to insert into prompt templates
|
1244 |
@app.post("/api/save_prompt_details")
|
1245 |
async def prompt_saveconnectors(request: Request,
|