from fastapi import FastAPI, HTTPException, Depends from fastapi.middleware.cors import CORSMiddleware from model import generate_answer, generate_questions import logging import socket import json app = FastAPI() # Add logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s") handler = logging.StreamHandler() handler.setFormatter(formatter) logger.addHandler(handler) # Add CORS origins = ["*"] # origins = [ # "https://gdscsou.github.io", # # Add any other origins that should be allowed to access your API # ] # app.add_middleware( # CORSMiddleware, # allow_origins=origins, # allow_credentials=True, # allow_methods=["*"], # allow_headers=["*"], # ) app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["GET", "POST", "PUT", "DELETE"], allow_headers=["*"], ) @app.get("/") def read_root(): return { "message": "API running successfully", "endpoints": [ "/chat/" ] } @app.post("/chat/") def chat(q: str, chathistory: str): hostname = socket.gethostname() ip_address = socket.gethostbyname(hostname) logger.info('User IP: %s', ip_address) gen_answer = generate_answer(q, chathistory) # gen_questions = generate_questions(q, chathistory) chat_data = { 'host_name': hostname, 'user_ip': ip_address, 'question': q, 'response': gen_answer['answer'], } # with open('chatData/chat_data.json', 'a') as json_file: # json.dump(chat_data, json_file) # return {"gen_answer": gen_answer, "gen_questions": gen_questions} return {"gen_answer": gen_answer}