File size: 6,993 Bytes
71f20f2 95cd543 71f20f2 4044180 f35554a 4044180 71f20f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import os
import logging
from fastapi import FastAPI, File, UploadFile, HTTPException, APIRouter, Form, Request
from pydantic import BaseModel
from services.file_upload_service import FileHandler
from services.chat_service import ChatAgentService
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
import mysql.connector
# Create the FastAPI app
app = FastAPI(title="RedmindGen", description="Chat with your Data", version="1.0.0")
# Load environment variables from .env file
load_dotenv()
# Get the path to the vector database
vector_db_path_documents = os.getenv('VECTOR_DB_PATH_DOCUMENTS')
# Mount static files
app.mount("/static", StaticFiles(directory="static"), name="static")
# Jinja2 templates
templates = Jinja2Templates(directory="templates")
#log handling
LOG_PATH = os.getenv('LOG_PATH')
LOG_FILENAME = "redmindgen.log"
full_log_path = os.path.join(LOG_PATH, LOG_FILENAME)
# Configure logging
logging.basicConfig(filename="test.log", level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.info("File upload start")
# Configure CORS
origins = [
"http://localhost:8000", # Adjust this to the origins you want to allow
"http://127.0.0.1:8000", # Example: Frontend running on a different port
"http://167.71.75.10:8003/"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Allows specified origins (use ["*"] for all origins)
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@app.get("/")
async def read_root(request: Request):
"""
Root endpoint that returns the index.html template.
"""
return templates.TemplateResponse("index.html", {"request": request})
def verify_user(username: str, password: str):
"""
Function to verify user credentials by checking the username and password in the database.
Returns "success" if the user is logged in, otherwise returns "failure".
"""
# Connect to MySQL database
cnx = mysql.connector.connect(user='u852023448_redmindgpt', password='redmindGpt@123',
host='217.21.88.10',
database='u852023448_redmindgpt')
# Create a cursor object
cursor = cnx.cursor()
# Execute the query
query = "SELECT * FROM user_detail WHERE username = %s AND password = %s"
values = (username, password) # Replace with actual values from the front end
cursor.execute(query, values)
# Fetch the result
result = cursor.fetchone()
# Check if the result is not None
if result is not None:
# User is logged in
return "success"
else:
# User is not logged in
return "failure"
# Close the cursor and connection
cursor.close()
cnx.close()
@app.post("/validate-user")
async def validate_user(request: Request, username: str = Form(...), password: str = Form(...)):
"""
Endpoint to validate user credentials.
If the credentials are valid, it returns the dashboard.html template.
Otherwise, it returns the index.html template.
"""
status = verify_user(username, password)
if status == 'success':
return templates.TemplateResponse("dashboard.html", {"request": request, "username": username})
else:
return templates.TemplateResponse("index.html", {"request": request})
@app.get("/knowledgebase")
async def knowledgebase(request: Request):
"""
Endpoint for the knowledgebase page.
Returns the knowledgebase.html template.
"""
return templates.TemplateResponse("knowledgebase.html", {"request": request})
@app.get("/chatbot")
async def chatbot(request: Request):
"""
Endpoint for the knowledgebase page.
Returns the knowledgebase.html template.
"""
return templates.TemplateResponse("chatpage.html", {"request": request})
@app.get("/company_profile")
async def company_profile(request: Request):
"""
Endpoint for the company profile page.
Returns the company_profile.html template.
"""
return templates.TemplateResponse("company_profile.html", {"request": request})
@app.get("/data_connectors")
async def data_connectors(request: Request):
"""
Endpoint for the company profile page.
Returns the company_profile.html template.
"""
return templates.TemplateResponse("data_connectors.html", {"request": request})
@app.get("/API_connectors")
async def API_connectors(request: Request):
"""
Endpoint for the company profile page.
Returns the company_profile.html template.
"""
return templates.TemplateResponse("API_connectors.html", {"request": request})
@app.post("/upload_file/")
async def upload_file(
file: UploadFile = File(..., description="Upload a file"),
document_name: str = Form(..., description="The name of the document."),
document_description: str = Form(..., description="A brief description of the document."),
department: str = Form(..., description="The department associated with the document."),
version: str = Form(..., description="The version of the document."),
last_updated: str = Form(..., description="The date when the document was last updated.")
):
"""
Endpoint to handle file uploads.
It validates the file type and calls the FileHandler to handle the file upload.
Returns the result of the file upload."""
print("upload_file called")
allowed_extensions = {'txt', 'pdf', 'docx', 'csv', 'xlsx'}
if file.filename.split('.')[-1].lower() not in allowed_extensions:
raise HTTPException(status_code=400,
detail="Unsupported file type. Supported types: TXT, PDF, DOCX, CSV, XLSX.")
try:
file_handler = FileHandler(vector_db_path=vector_db_path_documents)
print(vector_db_path_documents)
result = await file_handler.handle_file_upload(file, document_name, document_description, department, version,
last_updated)
print("result")
return result
except Exception as e:
logging.error(f"Error handling file uploads: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/chat_with_agent")
async def chat_with_agent(request: Request, user_question: str = Form(...)):
"""
Endpoint to chat with the chat agent.
It calls the ChatAgentService to get the answer to the user's question.
Returns the answer.
"""
print(user_question)
chat_service = ChatAgentService()
try:
print("chat_with_agent called")
answer = chat_service.answer_question(user_question)
print("answer returned")
return answer
except Exception as e:
logging.error(f"Error in chat api: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
|