Create appvector.py
Browse files- appvector.py +96 -0
appvector.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from typing import List, Tuple
|
4 |
+
import fitz # PyMuPDF
|
5 |
+
|
6 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
7 |
+
|
8 |
+
# Placeholder for the app's state
|
9 |
+
class MyApp:
|
10 |
+
def __init__(self) -> None:
|
11 |
+
self.documents = []
|
12 |
+
self.load_pdf("THEDIA1.pdf")
|
13 |
+
|
14 |
+
def load_pdf(self, file_path: str) -> None:
|
15 |
+
"""Extracts text from a PDF file and stores it in the app's documents."""
|
16 |
+
doc = fitz.open(file_path)
|
17 |
+
self.documents = []
|
18 |
+
for page_num in range(len(doc)):
|
19 |
+
page = doc[page_num]
|
20 |
+
text = page.get_text()
|
21 |
+
self.documents.append({"page": page_num + 1, "content": text})
|
22 |
+
print("PDF processed successfully!")
|
23 |
+
|
24 |
+
def search_documents(self, query: str, k: int = 3) -> List[str]:
|
25 |
+
"""Searches for relevant documents containing the query string."""
|
26 |
+
results = [doc["content"] for doc in self.documents if query.lower() in doc["content"].lower()]
|
27 |
+
return results[:k] if results else ["No relevant documents found."]
|
28 |
+
|
29 |
+
app = MyApp()
|
30 |
+
|
31 |
+
def respond(
|
32 |
+
message: str,
|
33 |
+
history: List[Tuple[str, str]],
|
34 |
+
system_message: str,
|
35 |
+
max_tokens: int,
|
36 |
+
temperature: float,
|
37 |
+
top_p: float,
|
38 |
+
):
|
39 |
+
system_message = "You are a knowledgeable DBT coach. Use relevant documents to guide users through DBT exercises and provide helpful information."
|
40 |
+
messages = [{"role": "system", "content": system_message}]
|
41 |
+
|
42 |
+
for val in history:
|
43 |
+
if val[0]:
|
44 |
+
messages.append({"role": "user", "content": val[0]})
|
45 |
+
if val[1]:
|
46 |
+
messages.append({"role": "assistant", "content": val[1]})
|
47 |
+
|
48 |
+
messages.append({"role": "user", "content": message})
|
49 |
+
|
50 |
+
# RAG - Retrieve relevant documents
|
51 |
+
retrieved_docs = app.search_documents(message)
|
52 |
+
context = "\n".join(retrieved_docs)
|
53 |
+
messages.append({"role": "system", "content": "Relevant documents: " + context})
|
54 |
+
|
55 |
+
response = ""
|
56 |
+
for message in client.chat_completion(
|
57 |
+
messages,
|
58 |
+
max_tokens=max_tokens,
|
59 |
+
stream=True,
|
60 |
+
temperature=temperature,
|
61 |
+
top_p=top_p,
|
62 |
+
):
|
63 |
+
token = message.choices[0].delta.content
|
64 |
+
response += token
|
65 |
+
yield response
|
66 |
+
|
67 |
+
demo = gr.Blocks()
|
68 |
+
|
69 |
+
with demo:
|
70 |
+
gr.Markdown("🧘♀️ **Dialectical Behaviour Therapy**")
|
71 |
+
gr.Markdown(
|
72 |
+
"Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. "
|
73 |
+
"We are not medical practitioners, and the use of this chatbot is at your own responsibility."
|
74 |
+
)
|
75 |
+
|
76 |
+
chatbot = gr.ChatInterface(
|
77 |
+
respond,
|
78 |
+
additional_inputs=[
|
79 |
+
gr.Textbox(value="You are a knowledgeable DBT coach. Use relevant documents to guide users through DBT exercises and provide helpful information.", label="System message"),
|
80 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
81 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
82 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
83 |
+
],
|
84 |
+
examples=[
|
85 |
+
["I feel overwhelmed with work."],
|
86 |
+
["Can you guide me through a quick meditation?"],
|
87 |
+
["How do I stop worrying about things I can't control?"],
|
88 |
+
["What are some DBT skills for managing anxiety?"],
|
89 |
+
["Can you explain mindfulness in DBT?"],
|
90 |
+
["What is radical acceptance?"]
|
91 |
+
],
|
92 |
+
title='DBT Coach 🧘♀️'
|
93 |
+
)
|
94 |
+
|
95 |
+
if __name__ == "__main__":
|
96 |
+
demo.launch()
|