Harsh4334632 commited on
Commit
408cf90
·
verified ·
1 Parent(s): a3ed049

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -8,15 +8,17 @@ import faiss
8
 
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
 
11
  class MyApp:
12
  def __init__(self) -> None:
13
  self.documents = []
14
  self.embeddings = None
15
  self.index = None
16
- self.load_pdf("YOURPDFFILE")
17
  self.build_vector_db()
18
 
19
  def load_pdf(self, file_path: str) -> None:
 
20
  doc = fitz.open(file_path)
21
  self.documents = []
22
  for page_num in range(len(doc)):
@@ -26,6 +28,7 @@ class MyApp:
26
  print("PDF processed successfully!")
27
 
28
  def build_vector_db(self) -> None:
 
29
  model = SentenceTransformer('all-MiniLM-L6-v2')
30
  self.embeddings = model.encode([doc["content"] for doc in self.documents])
31
  self.index = faiss.IndexFlatL2(self.embeddings.shape[1])
@@ -33,6 +36,7 @@ class MyApp:
33
  print("Vector database built successfully!")
34
 
35
  def search_documents(self, query: str, k: int = 3) -> List[str]:
 
36
  model = SentenceTransformer('all-MiniLM-L6-v2')
37
  query_embedding = model.encode([query])
38
  D, I = self.index.search(np.array(query_embedding), k)
@@ -49,7 +53,7 @@ def respond(
49
  temperature: float,
50
  top_p: float,
51
  ):
52
- system_message = "You are a knowledgeable and compassionate dentist. You always greet patients warmly and provide clear, concise, and helpful information about dental care. You answer one question at a time, ensuring that your responses are easy to understand and informative. Remember to be respectful, patient, and empathetic, considering that patients may be anxious or in pain. You guide patients through dental procedures, offer advice on oral hygiene, and provide recommendations for common dental issues. If a patient mentions severe pain or an emergency situation, you advise them to contact their dentist immediately or go to the nearest emergency room. Your goal is to help patients maintain good oral health and feel comfortable during their dental visits."
53
  messages = [{"role": "system", "content": system_message}]
54
 
55
  for val in history:
@@ -60,6 +64,7 @@ def respond(
60
 
61
  messages.append({"role": "user", "content": message})
62
 
 
63
  retrieved_docs = app.search_documents(message)
64
  context = "\n".join(retrieved_docs)
65
  messages.append({"role": "system", "content": "Relevant documents: " + context})
@@ -67,10 +72,10 @@ def respond(
67
  response = ""
68
  for message in client.chat_completion(
69
  messages,
70
- max_tokens=max_tokens,
71
  stream=True,
72
- temperature=temperature,
73
- top_p=top_p,
74
  ):
75
  token = message.choices[0].delta.content
76
  response += token
@@ -79,25 +84,24 @@ def respond(
79
  demo = gr.Blocks()
80
 
81
  with demo:
82
- gr.Markdown("🦷 **Ask Your Dentist**")
83
  gr.Markdown(
84
- "‼️Disclaimer: This chatbot provides general dental information and should not be considered as professional medical advice. For specific dental concerns, please consult your dentist directly.‼️"
85
  )
86
 
87
  chatbot = gr.ChatInterface(
88
  respond,
89
  examples=[
90
- ["What should I do about a toothache?"],
91
- ["Can you explain the process of getting a dental implant?"],
92
- ["How often should I get my teeth cleaned?"],
93
- ["What are the best practices for maintaining oral hygiene?"],
94
- ["Can you tell me about the benefits of fluoride?"],
95
- ["I'm experiencing sensitivity in my teeth. What could be the cause?"],
96
- ["What should I do if I have a dental emergency?"],
97
- ["How can I prevent cavities?"]
98
  ],
99
- title='Ask Your Dentist 🦷'
100
  )
101
 
102
  if __name__ == "__main__":
103
- demo.launch()
 
8
 
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
+ # Placeholder for the app's state
12
  class MyApp:
13
  def __init__(self) -> None:
14
  self.documents = []
15
  self.embeddings = None
16
  self.index = None
17
+ self.load_pdf("THEDIA1.pdf")
18
  self.build_vector_db()
19
 
20
  def load_pdf(self, file_path: str) -> None:
21
+ """Extracts text from a PDF file and stores it in the app's documents."""
22
  doc = fitz.open(file_path)
23
  self.documents = []
24
  for page_num in range(len(doc)):
 
28
  print("PDF processed successfully!")
29
 
30
  def build_vector_db(self) -> None:
31
+ """Builds a vector database using the content of the PDF."""
32
  model = SentenceTransformer('all-MiniLM-L6-v2')
33
  self.embeddings = model.encode([doc["content"] for doc in self.documents])
34
  self.index = faiss.IndexFlatL2(self.embeddings.shape[1])
 
36
  print("Vector database built successfully!")
37
 
38
  def search_documents(self, query: str, k: int = 3) -> List[str]:
39
+ """Searches for relevant documents using vector similarity."""
40
  model = SentenceTransformer('all-MiniLM-L6-v2')
41
  query_embedding = model.encode([query])
42
  D, I = self.index.search(np.array(query_embedding), k)
 
53
  temperature: float,
54
  top_p: float,
55
  ):
56
+ system_message = "You are a knowledgeable DBT coach. You always talk about one options at at a time. you add greetings and you ask questions like real counsellor. Remember you are helpful and a good listener. You are concise and never ask multiple questions, or give long response. You response like a human counsellor accurately and correctly. consider the users as your client. and practice verbal cues only where needed. Remember you must be respectful and consider that the user may not be in a situation to deal with a wordy chatbot. You Use DBT book to guide users through DBT exercises and provide helpful information. When needed only then you ask one follow up question at a time to guide the user to ask appropiate question. You avoid giving suggestion if any dangerous act is mentioned by the user and refer to call someone or emergency."
57
  messages = [{"role": "system", "content": system_message}]
58
 
59
  for val in history:
 
64
 
65
  messages.append({"role": "user", "content": message})
66
 
67
+ # RAG - Retrieve relevant documents
68
  retrieved_docs = app.search_documents(message)
69
  context = "\n".join(retrieved_docs)
70
  messages.append({"role": "system", "content": "Relevant documents: " + context})
 
72
  response = ""
73
  for message in client.chat_completion(
74
  messages,
75
+ max_tokens=100,
76
  stream=True,
77
+ temperature=0.98,
78
+ top_p=0.7,
79
  ):
80
  token = message.choices[0].delta.content
81
  response += token
 
84
  demo = gr.Blocks()
85
 
86
  with demo:
 
87
  gr.Markdown(
88
+ "‼️Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. and just to test RAG implementation.‼️"
89
  )
90
 
91
  chatbot = gr.ChatInterface(
92
  respond,
93
  examples=[
94
+ ["I feel overwhelmed with work."],
95
+ ["Can you guide me through a quick meditation?"],
96
+ ["How do I stop worrying about things I can't control?"],
97
+ ["What are some DBT skills for managing anxiety?"],
98
+ ["Can you explain mindfulness in DBT?"],
99
+ ["I am interested in DBT excercises"],
100
+ ["I feel restless. Please help me."],
101
+ ["I have destructive thoughts coming to my mind repetatively."]
102
  ],
103
+ title='Dialectical Behaviour Therapy Assistant👩‍⚕️🧘‍♀️'
104
  )
105
 
106
  if __name__ == "__main__":
107
+ demo.launch()