muhammadhasnain100 commited on
Commit
19a8026
·
verified ·
1 Parent(s): 6a57857

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -22
app.py CHANGED
@@ -1,24 +1,26 @@
1
- from chatbot import Comsatsbot
2
  import uuid
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
- from langchain_groq import ChatGroq
6
- from langchain_community.embeddings import HuggingFaceBgeEmbeddings
7
- import os
8
  from pymongo import MongoClient
9
  import urllib.parse
10
-
 
 
 
11
  # FastAPI app setup
12
  app = FastAPI()
 
13
  model_name = "BAAI/bge-small-en"
14
  model_kwargs = {"device": "cpu"}
15
  encode_kwargs = {"normalize_embeddings": True}
16
  hf = HuggingFaceBgeEmbeddings(
17
  model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs
18
  )
 
19
  os.environ['GROQ_API_KEY'] = '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'
20
  api_key = os.environ.get("GROQ_API_KEY")
21
  llm = ChatGroq(temperature=0, groq_api_key=api_key, model_name="llama3-70b-8192")
 
22
  # MongoDB setup
23
  username = 'hasnainnaseer987'
24
  password = 'Hasnain'
@@ -26,20 +28,25 @@ encoded_username = urllib.parse.quote_plus(username)
26
  encoded_password = urllib.parse.quote_plus(password)
27
  MONGODB_ATLAS_CLUSTER_URI = f'mongodb+srv://{encoded_username}:{encoded_password}@cluster0.jdfp3.mongodb.net/'
28
  client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
 
 
29
  paths = ['/content/FYP Supervisor Feedback.csv', '/content/urdu_data.csv', '/content/english_data.csv']
30
- chatbot = Comsatsbot(hf, llm, api_key, client, paths)
31
-
32
 
 
33
 
34
  # Endpoint for creating a new chat ID
35
- @app.post("/new_chat")
36
- def new_chat():
37
  try:
38
  chat_id = str(uuid.uuid4())
39
- message = chatbot.new_chat(id)
40
  return {"chat_id": chat_id, "message": "Successfully created new chat."}
41
- except HTTPException as e:
42
- raise e
 
 
 
 
43
  # Request model for response endpoint
44
  class ChatRequest(BaseModel):
45
  chat_id: str
@@ -49,28 +56,37 @@ class ChatRequest(BaseModel):
49
  @app.get("/get_chat/{chat_id}")
50
  def get_chat(chat_id: str):
51
  try:
52
- chat_history = chatbot.load_chat(chat_id)
53
- return {"chat_id": chat_id, "history": chat_history}
54
- except HTTPException as e:
55
- raise e # Raise if chat ID is not found
 
 
 
56
 
57
  # Endpoint for deleting a chat by chat ID
58
  @app.delete("/delete_chat/{chat_id}")
59
  def delete_chat(chat_id: str):
60
  try:
61
  message = chatbot.delete_chat(chat_id)
62
- return {"message": 'Successfully deleted chat.'}
63
- except HTTPException as e:
64
- raise e # Raise if chat ID is not found
 
 
 
65
 
66
  # Endpoint for getting a response based on chat ID and question
67
  @app.post("/response")
68
  def response(request: ChatRequest):
69
  chat_id = request.chat_id
70
  question = request.question
71
- answer = chatbot.response(question, chat_id)
72
 
73
  try:
 
 
74
  return {"answer": answer}
75
- except HTTPException as e:
76
- raise e
 
 
 
 
1
  import uuid
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
 
 
 
4
  from pymongo import MongoClient
5
  import urllib.parse
6
+ import os
7
+ from langchain_groq import ChatGroq
8
+ from langchain_community.embeddings import HuggingFaceBgeEmbeddings
9
+ from chatbot import Comsatsbot
10
  # FastAPI app setup
11
  app = FastAPI()
12
+
13
  model_name = "BAAI/bge-small-en"
14
  model_kwargs = {"device": "cpu"}
15
  encode_kwargs = {"normalize_embeddings": True}
16
  hf = HuggingFaceBgeEmbeddings(
17
  model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs
18
  )
19
+
20
  os.environ['GROQ_API_KEY'] = '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'
21
  api_key = os.environ.get("GROQ_API_KEY")
22
  llm = ChatGroq(temperature=0, groq_api_key=api_key, model_name="llama3-70b-8192")
23
+
24
  # MongoDB setup
25
  username = 'hasnainnaseer987'
26
  password = 'Hasnain'
 
28
  encoded_password = urllib.parse.quote_plus(password)
29
  MONGODB_ATLAS_CLUSTER_URI = f'mongodb+srv://{encoded_username}:{encoded_password}@cluster0.jdfp3.mongodb.net/'
30
  client = MongoClient(MONGODB_ATLAS_CLUSTER_URI)
31
+ db = client.get_database('chat_db') # Assume this is your database
32
+ chats_collection = db.get_collection('chats') # Collection to store chats
33
  paths = ['/content/FYP Supervisor Feedback.csv', '/content/urdu_data.csv', '/content/english_data.csv']
 
 
34
 
35
+ chatbot = Comsatsbot(hf, llm, api_key, chats_collection, paths)
36
 
37
  # Endpoint for creating a new chat ID
38
+ @app.post("/get_new_chat")
39
+ def create_new_chat():
40
  try:
41
  chat_id = str(uuid.uuid4())
42
+ message = chatbot.new_chat(chat_id)
43
  return {"chat_id": chat_id, "message": "Successfully created new chat."}
44
+ except KeyError:
45
+ raise HTTPException(status_code=404, detail="Chat ID already exist try again plz...")
46
+ except Exception as e:
47
+ raise HTTPException(status_code=500, detail=str(e))
48
+
49
+
50
  # Request model for response endpoint
51
  class ChatRequest(BaseModel):
52
  chat_id: str
 
56
  @app.get("/get_chat/{chat_id}")
57
  def get_chat(chat_id: str):
58
  try:
59
+ history = chatbot.load_chat(chat_id)
60
+ return {"chat_id": chat_id, "history": history}
61
+ except KeyError:
62
+ raise HTTPException(status_code=404, detail="Chat ID not found")
63
+ except Exception as e:
64
+ raise HTTPException(status_code=500, detail=str(e))
65
+
66
 
67
  # Endpoint for deleting a chat by chat ID
68
  @app.delete("/delete_chat/{chat_id}")
69
  def delete_chat(chat_id: str):
70
  try:
71
  message = chatbot.delete_chat(chat_id)
72
+ return {"message": f"Chat with ID {chat_id} has been deleted successfully."}
73
+ except KeyError:
74
+ raise HTTPException(status_code=404, detail="Chat ID not found")
75
+ except Exception as e:
76
+ raise HTTPException(status_code=500, detail=str(e))
77
+
78
 
79
  # Endpoint for getting a response based on chat ID and question
80
  @app.post("/response")
81
  def response(request: ChatRequest):
82
  chat_id = request.chat_id
83
  question = request.question
 
84
 
85
  try:
86
+ answer = chatbot.response(question, chat_id)
87
+
88
  return {"answer": answer}
89
+ except KeyError:
90
+ raise HTTPException(status_code=404, detail="Chat ID not found")
91
+ except Exception as e:
92
+ raise HTTPException(status_code=500, detail=str(e))