Spaces:
Sleeping
Sleeping
File size: 4,010 Bytes
4003576 62cdadc 4003576 7a9dc13 4003576 fd39b4a 4003576 74d44d3 4003576 708680f 6460437 4003576 9381b77 a5b5af1 4003576 edb96d7 b0a67e4 edb96d7 4003576 edb96d7 4003576 48c0f45 61cd3e5 4003576 5a91f55 4003576 f1af40d 4003576 5a91f55 4003576 5a91f55 4003576 | 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 | import uuid
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from pymongo import MongoClient
import urllib.parse
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
from langchain_groq import ChatGroq
from langchain_huggingface import HuggingFaceEmbeddings
from chatbot import Comsatsbot
# FastAPI app setup
app = FastAPI()
model_name = "BAAI/bge-base-en"
model_kwargs = {"device": "cpu"}
encode_kwargs = {"normalize_embeddings": True}
hf = HuggingFaceEmbeddings(
model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs
)
api_keys = [
'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',
'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',
'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',
'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',
'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',
'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',
]
llm = ChatGroq(temperature=0, groq_api_key=api_keys[2], model_name="llama3-70b-8192")
# MongoDB setup
username = 'laiba'
password = 'VRwiQ8padD_gN8t'
encoded_username = urllib.parse.quote_plus(username)
encoded_password = urllib.parse.quote_plus(password)
#MONGODB_ATLAS_CLUSTER_URI = f'mongodb+srv://{encoded_username}:{encoded_password}@comsatsbot.04y7a.mongodb.net/'
#client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
MONGODB_ATLAS_CLUSTER_URI = (
f"mongodb+srv://{encoded_username}:{encoded_password}"
f"@comsatsbot.04y7a.mongodb.net/?appName=ComsatsBot&retryWrites=true&w=majority"
)
client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
db = client.get_database('chat_db') # Assume this is your database
chats_collection = db.get_collection('chats') # Collection to store chats
paths = ['FYP Supervisor Feedback.csv', 'urdu_data.csv', 'english_data.csv']
chatbot = Comsatsbot(hf, llm, api_keys, chats_collection, paths)
# Endpoint for creating a new chat ID
@app.post("/get_new_chat")
def create_new_chat():
try:
chat_id = str(uuid.uuid4())
message = chatbot.new_chat(chat_id)
return {"chat_id": chat_id, "message": "Successfully created new chat."}
except KeyError:
raise HTTPException(status_code=404, detail="Chat ID already exist try again plz...")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Request model for response endpoint
class ChatRequest(BaseModel):
chat_id: str
question: str
# Endpoint for retrieving chat history by chat ID
@app.get("/get_chat/{chat_id}")
def get_chat(chat_id: str):
try:
history = chatbot.load_chat(chat_id)
return {"chat_id": chat_id, "history": history}
except KeyError:
raise HTTPException(status_code=404, detail="Chat ID not found")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Endpoint for deleting a chat by chat ID
@app.delete("/delete_chat/{chat_id}")
def delete_chat(chat_id: str):
try:
message = chatbot.delete_chat(chat_id)
return {"message": f"Chat with ID {chat_id} has been deleted successfully."}
except KeyError:
raise HTTPException(status_code=404, detail="Chat ID not found")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# Endpoint for getting a response based on chat ID and question
@app.post("/response")
def response(request: ChatRequest):
chat_id = request.chat_id
question = request.question
print(f"Received question: {question} with chat_id: {chat_id}") # Debug line
try:
answer = chatbot.response(question, chat_id)
print(f"Generated answer: {answer}")
return {"answer": answer}
except KeyError:
print("Chat ID not found!")
raise HTTPException(status_code=404, detail="Chat ID not found")
except Exception as e:
print(f"Exception: {e}")
raise HTTPException(status_code=500, detail="GPU error, try after some time...")
|