Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from services.chat_service import ChatAgentService
|
| 7 |
from fastapi.staticfiles import StaticFiles
|
| 8 |
from fastapi.templating import Jinja2Templates
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from dotenv import load_dotenv
|
| 11 |
-
|
| 12 |
import mysql.connector
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
app = FastAPI(title="RedmindGen", description="Chat with your Data", version="1.0.0")
|
| 16 |
-
|
| 17 |
-
# Load environment variables from .env file
|
| 18 |
load_dotenv()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Mount static files
|
| 24 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
@@ -26,170 +34,303 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
| 26 |
# Jinja2 templates
|
| 27 |
templates = Jinja2Templates(directory="templates")
|
| 28 |
|
| 29 |
-
#log handling
|
| 30 |
-
LOG_PATH = os.getenv('LOG_PATH')
|
| 31 |
-
LOG_FILENAME = "redmindgen.log"
|
| 32 |
-
full_log_path = os.path.join(LOG_PATH, LOG_FILENAME)
|
| 33 |
-
# Configure logging
|
| 34 |
-
logging.basicConfig(filename="test.log", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 35 |
-
logging.info("File upload start")
|
| 36 |
-
|
| 37 |
# Configure CORS
|
| 38 |
origins = [
|
| 39 |
-
"http://localhost:8000",
|
| 40 |
-
"http://127.0.0.1:8000",
|
| 41 |
"http://167.71.75.10:8003/"
|
| 42 |
]
|
| 43 |
|
| 44 |
app.add_middleware(
|
| 45 |
CORSMiddleware,
|
| 46 |
-
allow_origins=origins,
|
| 47 |
allow_credentials=True,
|
| 48 |
-
allow_methods=["*"],
|
| 49 |
-
allow_headers=["*"],
|
| 50 |
)
|
| 51 |
|
| 52 |
@app.get("/")
|
| 53 |
async def read_root(request: Request):
|
| 54 |
-
"""
|
| 55 |
-
Root endpoint that returns the index.html template.
|
| 56 |
-
"""
|
| 57 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 58 |
|
| 59 |
def verify_user(username: str, password: str):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
"""
|
| 64 |
-
# Connect to MySQL database
|
| 65 |
-
cnx = mysql.connector.connect(user='u852023448_redmindgpt', password='redmindGpt@123',
|
| 66 |
host='217.21.88.10',
|
| 67 |
database='u852023448_redmindgpt')
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# User is not logged in
|
| 86 |
return "failure"
|
| 87 |
|
| 88 |
-
# Close the cursor and connection
|
| 89 |
-
cursor.close()
|
| 90 |
-
cnx.close()
|
| 91 |
-
|
| 92 |
@app.post("/validate-user")
|
| 93 |
async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):
|
| 94 |
-
"""
|
| 95 |
-
Endpoint to validate user credentials.
|
| 96 |
-
If the credentials are valid, it returns the dashboard.html template.
|
| 97 |
-
Otherwise, it returns the index.html template.
|
| 98 |
-
"""
|
| 99 |
status = verify_user(username, password)
|
| 100 |
if status == 'success':
|
| 101 |
return templates.TemplateResponse("dashboard.html", {"request": request, "username": username})
|
| 102 |
else:
|
| 103 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 104 |
|
| 105 |
-
@app.
|
| 106 |
-
async def
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
@app.get("/company_profile")
|
| 122 |
async def company_profile(request: Request):
|
| 123 |
-
"""
|
| 124 |
-
Endpoint for the company profile page.
|
| 125 |
-
Returns the company_profile.html template.
|
| 126 |
-
"""
|
| 127 |
return templates.TemplateResponse("company_profile.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
@app.get("/data_connectors")
|
| 129 |
async def data_connectors(request: Request):
|
| 130 |
-
"""
|
| 131 |
-
Endpoint for the company profile page.
|
| 132 |
-
Returns the company_profile.html template.
|
| 133 |
-
"""
|
| 134 |
return templates.TemplateResponse("data_connectors.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
@app.get("/API_connectors")
|
| 136 |
async def API_connectors(request: Request):
|
| 137 |
-
"""
|
| 138 |
-
Endpoint for the company profile page.
|
| 139 |
-
Returns the company_profile.html template.
|
| 140 |
-
"""
|
| 141 |
return templates.TemplateResponse("API_connectors.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
-
@app.post("/upload_file/")
|
| 144 |
-
async def upload_file(
|
| 145 |
-
file: UploadFile = File(..., description="Upload a file"),
|
| 146 |
-
document_name: str = Form(..., description="The name of the document."),
|
| 147 |
-
document_description: str = Form(..., description="A brief description of the document."),
|
| 148 |
-
department: str = Form(..., description="The department associated with the document."),
|
| 149 |
-
version: str = Form(..., description="The version of the document."),
|
| 150 |
-
last_updated: str = Form(..., description="The date when the document was last updated.")
|
| 151 |
-
):
|
| 152 |
-
|
| 153 |
-
"""
|
| 154 |
-
Endpoint to handle file uploads.
|
| 155 |
-
It validates the file type and calls the FileHandler to handle the file upload.
|
| 156 |
-
Returns the result of the file upload."""
|
| 157 |
-
print("upload_file called")
|
| 158 |
-
allowed_extensions = {'txt', 'pdf', 'docx', 'csv', 'xlsx'}
|
| 159 |
-
if file.filename.split('.')[-1].lower() not in allowed_extensions:
|
| 160 |
-
raise HTTPException(status_code=400,
|
| 161 |
-
detail="Unsupported file type. Supported types: TXT, PDF, DOCX, CSV, XLSX.")
|
| 162 |
-
|
| 163 |
-
try:
|
| 164 |
-
|
| 165 |
-
file_handler = FileHandler(vector_db_path=vector_db_path_documents)
|
| 166 |
-
print(vector_db_path_documents)
|
| 167 |
-
result = await file_handler.handle_file_upload(file, document_name, document_description, department, version,
|
| 168 |
-
last_updated)
|
| 169 |
-
print("result")
|
| 170 |
-
return result
|
| 171 |
-
except Exception as e:
|
| 172 |
-
logging.error(f"Error handling file uploads: {str(e)}")
|
| 173 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 174 |
-
|
| 175 |
-
@app.post("/chat_with_agent")
|
| 176 |
-
async def chat_with_agent(request: Request, user_question: str = Form(...)):
|
| 177 |
-
"""
|
| 178 |
-
Endpoint to chat with the chat agent.
|
| 179 |
-
It calls the ChatAgentService to get the answer to the user's question.
|
| 180 |
-
Returns the answer.
|
| 181 |
-
"""
|
| 182 |
-
print(user_question)
|
| 183 |
-
|
| 184 |
-
chat_service = ChatAgentService()
|
| 185 |
-
try:
|
| 186 |
-
print("chat_with_agent called")
|
| 187 |
-
answer = chat_service.answer_question(user_question)
|
| 188 |
-
print("answer returned")
|
| 189 |
-
return answer
|
| 190 |
-
except Exception as e:
|
| 191 |
-
logging.error(f"Error in chat api: {str(e)}")
|
| 192 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 193 |
|
| 194 |
|
| 195 |
|
|
|
|
| 1 |
+
import json
|
| 2 |
import os
|
| 3 |
import logging
|
| 4 |
+
import shutil
|
| 5 |
+
from fastapi import FastAPI, File, Query,Form, Request, HTTPException, UploadFile
|
| 6 |
+
from fastapi.responses import JSONResponse, RedirectResponse
|
|
|
|
| 7 |
from fastapi.staticfiles import StaticFiles
|
| 8 |
from fastapi.templating import Jinja2Templates
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from dotenv import load_dotenv
|
|
|
|
| 11 |
import mysql.connector
|
| 12 |
+
from typing import List
|
| 13 |
|
| 14 |
+
# Load environment variables
|
|
|
|
|
|
|
|
|
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
+
# Configure logging
|
| 18 |
+
logging.basicConfig(
|
| 19 |
+
level=logging.INFO,
|
| 20 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 21 |
+
handlers=[
|
| 22 |
+
logging.FileHandler("redmindgen.log"),
|
| 23 |
+
logging.StreamHandler() # This ensures logging to console
|
| 24 |
+
]
|
| 25 |
+
)
|
| 26 |
+
logging.info("Application startup")
|
| 27 |
+
|
| 28 |
+
# Create the FastAPI app
|
| 29 |
+
app = FastAPI(title="RedmindGen", description="Chat with your Data", version="1.0.0")
|
| 30 |
|
| 31 |
# Mount static files
|
| 32 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
| 34 |
# Jinja2 templates
|
| 35 |
templates = Jinja2Templates(directory="templates")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Configure CORS
|
| 38 |
origins = [
|
| 39 |
+
"http://localhost:8000",
|
| 40 |
+
"http://127.0.0.1:8000",
|
| 41 |
"http://167.71.75.10:8003/"
|
| 42 |
]
|
| 43 |
|
| 44 |
app.add_middleware(
|
| 45 |
CORSMiddleware,
|
| 46 |
+
allow_origins=origins,
|
| 47 |
allow_credentials=True,
|
| 48 |
+
allow_methods=["*"],
|
| 49 |
+
allow_headers=["*"],
|
| 50 |
)
|
| 51 |
|
| 52 |
@app.get("/")
|
| 53 |
async def read_root(request: Request):
|
|
|
|
|
|
|
|
|
|
| 54 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 55 |
|
| 56 |
def verify_user(username: str, password: str):
|
| 57 |
+
try:
|
| 58 |
+
# Connect to MySQL database
|
| 59 |
+
cnx = mysql.connector.connect(user='u852023448_redmindgpt', password='redmindGpt@123',
|
|
|
|
|
|
|
|
|
|
| 60 |
host='217.21.88.10',
|
| 61 |
database='u852023448_redmindgpt')
|
| 62 |
|
| 63 |
+
#cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 64 |
+
cursor = cnx.cursor()
|
| 65 |
+
query = "SELECT * FROM user_detail WHERE username = %s AND password = %s"
|
| 66 |
+
values = (username, password)
|
| 67 |
+
cursor.execute(query, values)
|
| 68 |
+
result = cursor.fetchone()
|
| 69 |
+
cursor.close()
|
| 70 |
+
cnx.close()
|
| 71 |
+
if result is not None:
|
| 72 |
+
logging.info(f"User {username} logged in successfully")
|
| 73 |
+
return "success"
|
| 74 |
+
else:
|
| 75 |
+
logging.info(f"User {username} login failed")
|
| 76 |
+
return "failure"
|
| 77 |
+
except mysql.connector.Error as err:
|
| 78 |
+
logging.error(f"Database error: {err}")
|
|
|
|
| 79 |
return "failure"
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
@app.post("/validate-user")
|
| 82 |
async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
status = verify_user(username, password)
|
| 84 |
if status == 'success':
|
| 85 |
return templates.TemplateResponse("dashboard.html", {"request": request, "username": username})
|
| 86 |
else:
|
| 87 |
return templates.TemplateResponse("index.html", {"request": request})
|
| 88 |
|
| 89 |
+
@app.post("/submit_company_profile")
|
| 90 |
+
async def submit_company_profile(request: Request,
|
| 91 |
+
company_name: str = Form(...),
|
| 92 |
+
company_code: str = Form(...),
|
| 93 |
+
domain: str = Form(...),
|
| 94 |
+
llm_tools: List[str] = Form(...)):
|
| 95 |
+
logging.info("Received form submission for company profile")
|
| 96 |
+
logging.info(f"Form data - company_name: {company_name}, company_code: {company_code}, domain: {domain}, llm_tools: {llm_tools}")
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
# Connect to MySQL database
|
| 100 |
+
cnx = mysql.connector.connect(user='u852023448_redmindgpt', password='redmindGpt@123',
|
| 101 |
+
host='217.21.88.10',
|
| 102 |
+
database='u852023448_redmindgpt')
|
| 103 |
+
|
| 104 |
+
#cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 105 |
+
cursor = cnx.cursor()
|
| 106 |
+
query = "INSERT INTO company_detail (company_name, company_code, domain, llm_tools) VALUES (%s, %s, %s, %s)"
|
| 107 |
+
values = (company_name, company_code, domain, ",".join(llm_tools))
|
| 108 |
+
logging.info(f"Executing query: {query} with values: {values}")
|
| 109 |
+
cursor.execute(query, values)
|
| 110 |
+
cnx.commit()
|
| 111 |
+
logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")
|
| 112 |
+
cursor.close()
|
| 113 |
+
cnx.close()
|
| 114 |
+
logging.info(f"Company profile for {company_name} inserted successfully")
|
| 115 |
+
RedirectResponse(url="/company_profile?message=Data saved successfully", status_code=302)
|
| 116 |
+
except mysql.connector.Error as err:
|
| 117 |
+
logging.error(f"Database error: {err}")
|
| 118 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 119 |
|
| 120 |
+
|
| 121 |
+
@app.get("/api/companies")
|
| 122 |
+
async def get_companies():
|
| 123 |
+
try:
|
| 124 |
+
cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 125 |
+
cursor = cnx.cursor()
|
| 126 |
+
query = "SELECT company_name FROM company_detail "
|
| 127 |
+
cursor.execute(query)
|
| 128 |
+
companies = cursor.fetchall()
|
| 129 |
+
cursor.close()
|
| 130 |
+
cnx.close()
|
| 131 |
+
return {"companies": [{"name": company[0]} for company in companies]}
|
| 132 |
+
except mysql.connector.Error as err:
|
| 133 |
+
logging.error(f"Database error: {err}")
|
| 134 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 135 |
|
| 136 |
@app.get("/company_profile")
|
| 137 |
async def company_profile(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
return templates.TemplateResponse("company_profile.html", {"request": request})
|
| 139 |
+
@app.get("/api/company_id")
|
| 140 |
+
async def get_company_id(company_name: str):
|
| 141 |
+
print(f"Received company_name: {company_name}") # Debug statement
|
| 142 |
+
logging.info(f"Received request for company name: {company_name}")
|
| 143 |
+
try:
|
| 144 |
+
cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 145 |
+
cursor = cnx.cursor()
|
| 146 |
+
query = "SELECT company_id FROM company_detail WHERE company_name = %s"
|
| 147 |
+
cursor.execute(query, (company_name,))
|
| 148 |
+
result = cursor.fetchone()
|
| 149 |
+
cursor.close()
|
| 150 |
+
cnx.close()
|
| 151 |
+
|
| 152 |
+
if result:
|
| 153 |
+
return {"company_id": result[0]}
|
| 154 |
+
else:
|
| 155 |
+
logging.error(f"Company not found for name: {company_name}")
|
| 156 |
+
raise HTTPException(status_code=404, detail="Company not found")
|
| 157 |
+
except mysql.connector.Error as err:
|
| 158 |
+
logging.error(f"Database error: {err}")
|
| 159 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 160 |
+
|
| 161 |
+
@app.get("/knowledgebase")
|
| 162 |
+
async def knowledgebase(request: Request):
|
| 163 |
+
return templates.TemplateResponse("knowledgebase.html", {"request": request})
|
| 164 |
+
@app.post("/upload_document")
|
| 165 |
+
async def upload_document(
|
| 166 |
+
request: Request,
|
| 167 |
+
company_id:str=Form(...),
|
| 168 |
+
uploadFile: UploadFile = File(...),
|
| 169 |
+
documentName: str = Form(...),
|
| 170 |
+
documentDescription: str = Form(...),
|
| 171 |
+
department: str = Form(...),
|
| 172 |
+
version: str = Form(...),
|
| 173 |
+
lastUpdated: str = Form(...)
|
| 174 |
+
):
|
| 175 |
+
try:
|
| 176 |
+
# Save the uploaded file
|
| 177 |
+
upload_folder = "uploads/"
|
| 178 |
+
os.makedirs(upload_folder, exist_ok=True)
|
| 179 |
+
file_path = os.path.join(upload_folder, uploadFile.filename)
|
| 180 |
+
|
| 181 |
+
with open(file_path, "wb") as buffer:
|
| 182 |
+
shutil.copyfileobj(uploadFile.file, buffer)
|
| 183 |
+
|
| 184 |
+
# Save the details to the database
|
| 185 |
+
cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 186 |
+
cursor = cnx.cursor()
|
| 187 |
+
query = """
|
| 188 |
+
INSERT INTO knowledge_base (company_id,file_path, document_name, document_desc, department, version, last_updated)
|
| 189 |
+
VALUES (%s,%s, %s, %s, %s, %s, %s)
|
| 190 |
+
"""
|
| 191 |
+
values = (company_id,file_path, documentName, documentDescription, department, version, lastUpdated)
|
| 192 |
+
cursor.execute(query, values)
|
| 193 |
+
cnx.commit()
|
| 194 |
+
cursor.close()
|
| 195 |
+
cnx.close()
|
| 196 |
+
|
| 197 |
+
logging.info(f"Document {documentName} uploaded successfully")
|
| 198 |
+
return RedirectResponse(url="/", status_code=302)
|
| 199 |
+
except mysql.connector.Error as err:
|
| 200 |
+
logging.error(f"Database error: {err}")
|
| 201 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 202 |
+
|
| 203 |
+
@app.get("/api/document_upload")
|
| 204 |
+
async def get_document(company_id: str = Query(...), company_name: str = Query(...)):
|
| 205 |
+
print(f"Received companyId and name: {company_id},{company_name}") # Log rec
|
| 206 |
+
#async def get_data_connectors(company_id: str, company_name: str):
|
| 207 |
+
logging.info(f"Received request for company_id and company_id: {company_id},{company_name}")
|
| 208 |
+
try:
|
| 209 |
+
cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 210 |
+
cursor = cnx.cursor()
|
| 211 |
+
query = """
|
| 212 |
+
SELECT kb.company_id, kb.file_path, kb.document_name, kb.document_desc,kb.department,kb.version,kb.last_updated
|
| 213 |
+
FROM redmind_gpt.knowledge_base kb
|
| 214 |
+
JOIN redmind_gpt.company_detail cd ON kb.company_id = cd.company_id
|
| 215 |
+
WHERE kb.company_id = %s and cd.company_name=%s
|
| 216 |
+
"""
|
| 217 |
+
logging.info(f"Executing query: {query} with company_id: {company_id}")
|
| 218 |
+
params = (company_id,company_name)
|
| 219 |
+
logging.info(f"Query parameters: {params}")
|
| 220 |
+
print(f"Query parameters: {params}")
|
| 221 |
+
|
| 222 |
+
cursor.execute(query, params) # Pa
|
| 223 |
+
result = cursor.fetchone()
|
| 224 |
+
logging.info(f"Query result: {result}")
|
| 225 |
+
cursor.close
|
| 226 |
+
cnx.close()
|
| 227 |
+
|
| 228 |
+
if result:
|
| 229 |
+
logging.info(f"Document found for company_id: {company_id}")
|
| 230 |
+
return {
|
| 231 |
+
"company_id": result[0],
|
| 232 |
+
# "file_path":result[1],
|
| 233 |
+
"document_name": result[2],
|
| 234 |
+
"document_desc": result[3],
|
| 235 |
+
"department": result[4],
|
| 236 |
+
"version": result[5],
|
| 237 |
+
"last_updated": result[6]
|
| 238 |
+
}
|
| 239 |
+
else:
|
| 240 |
+
logging.warning(f"No document found for company_id: {company_id}")
|
| 241 |
+
raise HTTPException(status_code=404, detail="Data document not found")
|
| 242 |
+
except mysql.connector.Error as err:
|
| 243 |
+
logging.error(f"Database error: {err}")
|
| 244 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 245 |
+
|
| 246 |
@app.get("/data_connectors")
|
| 247 |
async def data_connectors(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
return templates.TemplateResponse("data_connectors.html", {"request": request})
|
| 249 |
+
@app.post("/save_data_connectors")
|
| 250 |
+
async def save_data_connectors( request: Request,
|
| 251 |
+
company_id: int = Form(...),
|
| 252 |
+
database: List[str] = Form(...),
|
| 253 |
+
server: str = Form(...),
|
| 254 |
+
port: str = Form(...),
|
| 255 |
+
databaseName: List[str] = Form(...),
|
| 256 |
+
username: str=Form(...),
|
| 257 |
+
password: str=Form(...),
|
| 258 |
+
tables: List[str] = Form(...)):
|
| 259 |
+
logging.info(f"Received form submission for database_connectors")
|
| 260 |
+
try:
|
| 261 |
+
cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 262 |
+
cursor = cnx.cursor()
|
| 263 |
+
#databasetype_json=json.dumps(database)
|
| 264 |
+
query = "INSERT INTO data_connectors(company_id,databasetype, serverip, port, database_name,username,password,dbtablename) VALUES (%s,%s, %s, %s, %s,%s,%s,%s)"
|
| 265 |
+
values = (company_id, ",".join(database), server, port, ",".join(databaseName),username,password, ",".join(tables))
|
| 266 |
+
logging.info(f"Executing query: {query} with values: {values}")
|
| 267 |
+
cursor.execute(query, values)
|
| 268 |
+
cnx.commit()
|
| 269 |
+
logging.info(f"Query executed successfully, {cursor.rowcount} row(s) affected")
|
| 270 |
+
cursor.close()
|
| 271 |
+
cnx.close()
|
| 272 |
+
logging.info(f"Data_connectors for {database} inserted successfully")
|
| 273 |
+
return RedirectResponse(url="/data_connectors", status_code=302)
|
| 274 |
+
except mysql.connector.Error as err:
|
| 275 |
+
logging.error(f"Database error: {err}")
|
| 276 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 277 |
+
|
| 278 |
+
@app.get("/api/check_data_connectors")
|
| 279 |
+
async def get_data_connectors(company_id: str = Query(...), company_name: str = Query(...)):
|
| 280 |
+
print(f"Received companyId and name: {company_id},{company_name}") # Log rec
|
| 281 |
+
#async def get_data_connectors(company_id: str, company_name: str):
|
| 282 |
+
logging.info(f"Received request for company_id and company_id: {company_id},{company_name}")
|
| 283 |
+
try:
|
| 284 |
+
cnx = mysql.connector.connect(user='root', password='', host='localhost', database='redmind_gpt')
|
| 285 |
+
cursor = cnx.cursor()
|
| 286 |
+
query = """
|
| 287 |
+
SELECT dc.company_id, dc.databasetype, dc.serverip, dc.port,dc.database_name, dc.username, dc.password ,dc.dbtablename
|
| 288 |
+
FROM redmind_gpt.data_connectors dc
|
| 289 |
+
JOIN redmind_gpt.company_detail cd ON dc.company_id = cd.company_id
|
| 290 |
+
WHERE dc.company_id = %s and cd.company_name=%s
|
| 291 |
+
"""
|
| 292 |
+
logging.info(f"Executing query: {query} with company_id: {company_id}")
|
| 293 |
+
params = (company_id,company_name)
|
| 294 |
+
logging.info(f"Query parameters: {params}")
|
| 295 |
+
print(f"Query parameters: {params}")
|
| 296 |
+
|
| 297 |
+
cursor.execute(query, params) # Pa
|
| 298 |
+
result = cursor.fetchone()
|
| 299 |
+
logging.info(f"Query result: {result}")
|
| 300 |
+
cursor.close()
|
| 301 |
+
cnx.close()
|
| 302 |
+
|
| 303 |
+
if result:
|
| 304 |
+
logging.info(f"Data found for company_id: {company_id}")
|
| 305 |
+
return {
|
| 306 |
+
"company_id": result[0],
|
| 307 |
+
"databasetype":result[1],
|
| 308 |
+
"serverip": result[2],
|
| 309 |
+
"port": result[3],
|
| 310 |
+
"database_name": result[4],
|
| 311 |
+
"username": result[5],
|
| 312 |
+
"password": result[6],
|
| 313 |
+
"dbtablename": result[7]
|
| 314 |
+
}
|
| 315 |
+
else:
|
| 316 |
+
logging.warning(f"No data found for company_id: {company_id}")
|
| 317 |
+
raise HTTPException(status_code=404, detail="Data connector not found")
|
| 318 |
+
except mysql.connector.Error as err:
|
| 319 |
+
logging.error(f"Database error: {err}")
|
| 320 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
|
| 324 |
@app.get("/API_connectors")
|
| 325 |
async def API_connectors(request: Request):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
return templates.TemplateResponse("API_connectors.html", {"request": request})
|
| 327 |
+
@app.get("/prompt_template")
|
| 328 |
+
async def prompt_template(request: Request):
|
| 329 |
+
return templates.TemplateResponse("prompt_template.html", {"request": request})
|
| 330 |
+
@app.get("/chatbot")
|
| 331 |
+
async def chatbot(request: Request):
|
| 332 |
+
return templates.TemplateResponse("chatbot.html", {"request": request})
|
| 333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
|
| 336 |
|