Spaces:
Sleeping
Sleeping
| 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 | |
| ) | |
| 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() | |
| 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}) | |
| async def knowledgebase(request: Request): | |
| """ | |
| Endpoint for the knowledgebase page. | |
| Returns the knowledgebase.html template. | |
| """ | |
| return templates.TemplateResponse("knowledgebase.html", {"request": request}) | |
| async def chatbot(request: Request): | |
| """ | |
| Endpoint for the knowledgebase page. | |
| Returns the knowledgebase.html template. | |
| """ | |
| return templates.TemplateResponse("chatpage.html", {"request": request}) | |
| 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}) | |
| 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}) | |
| 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}) | |
| 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)) | |
| 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)) | |