Harsh4334632 commited on
Commit
a3ed049
·
verified ·
1 Parent(s): 0d1eac9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -38
app.py CHANGED
@@ -1,21 +1,55 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
  ):
18
- system_message = "Your Bharatnatyam dance skills are exceptional. You gracefully interpret stories through movement, portraying rich cultural narratives. If you ever need to unwind or refocus, feel free to share your thoughts or ask for a quick relaxation exercise."
19
  messages = [{"role": "system", "content": system_message}]
20
 
21
  for val in history:
@@ -26,8 +60,11 @@ def respond(
26
 
27
  messages.append({"role": "user", "content": message})
28
 
29
- response = ""
 
 
30
 
 
31
  for message in client.chat_completion(
32
  messages,
33
  max_tokens=max_tokens,
@@ -36,36 +73,31 @@ def respond(
36
  top_p=top_p,
37
  ):
38
  token = message.choices[0].delta.content
39
-
40
  response += token
41
  yield response
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value = "Your Bharatnatyam dance skills are exceptional. You gracefully interpret stories through movement, portraying rich cultural narratives. If you ever need to unwind or refocus, feel free to share your thoughts or ask for a quick relaxation exercise.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
-
61
- examples = [
62
- ["I am a beginner Bharatnatyam dancer."],
63
- ["Do you provide some tips to learn this dance form quickly?"],
64
- ["How do I can improve my dance skills?"]
65
- ],
66
- title = 'A Bharatnatyam Dancer'
67
- )
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  if __name__ == "__main__":
71
- demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from typing import List, Tuple
4
+ import fitz # PyMuPDF
5
+ from sentence_transformers import SentenceTransformer, util
6
+ import numpy as np
7
+ 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)):
23
+ page = doc[page_num]
24
+ text = page.get_text()
25
+ self.documents.append({"page": page_num + 1, "content": text})
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])
32
+ self.index.add(np.array(self.embeddings))
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)
39
+ results = [self.documents[i]["content"] for i in I[0]]
40
+ return results if results else ["No relevant documents found."]
41
+
42
+ app = MyApp()
43
 
44
  def respond(
45
+ message: str,
46
+ history: List[Tuple[str, str]],
47
+ system_message: str,
48
+ max_tokens: int,
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
 
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})
66
 
67
+ response = ""
68
  for message in client.chat_completion(
69
  messages,
70
  max_tokens=max_tokens,
 
73
  top_p=top_p,
74
  ):
75
  token = message.choices[0].delta.content
 
76
  response += token
77
  yield response
78
 
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()