Rahatara commited on
Commit
0e4142f
1 Parent(s): 4040e28

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -153
app.py DELETED
@@ -1,153 +0,0 @@
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
- # 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)):
25
- page = doc[page_num]
26
- text = page.get_text()
27
- self.documents.append({"page": page_num + 1, "content": text})
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])
35
- self.index.add(np.array(self.embeddings))
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)
43
- results = [self.documents[i]["content"] for i in I[0]]
44
- return results if results else ["No relevant documents found."]
45
-
46
- app = MyApp()
47
-
48
- def preprocess_input(user_input: str) -> str:
49
- """Preprocesses user input to enhance it for better context."""
50
- if "therapy" or "excercise" in user_input.lower():
51
- return "I am looking for guidance on therapy. Can you help me with some exercises or techniques to manage my stress and emotions?"
52
- # Add more rules as needed
53
- return user_input
54
-
55
- def preprocess_response(response: str) -> str:
56
- """Preprocesses the response to make it more polished."""
57
- response = response.strip()
58
- response = response.replace("\n\n", "\n")
59
- response = response.replace(" ,", ",")
60
- response = response.replace(" .", ".")
61
- response = " ".join(response.split())
62
- return response
63
-
64
- def shorten_response(response: str) -> str:
65
- """Uses the Zephyr model to shorten and refine the response."""
66
- messages = [{"role": "system", "content": "Shorten and refine this response in bullet list."}, {"role": "user", "content": response}]
67
- result = client.chat_completion(messages, max_tokens=512, temperature=0.98, top_p=0.9)
68
- return result.choices[0].message['content'].strip()
69
-
70
- def respond(message: str, history: List[Tuple[str, str]]):
71
- system_message = "You are a concisely speaking empathetic Dialectical Behaviour Therapist assistant. You politely guide users through DBT exercises based on the given DBT book. You must say one thing at a time and ask follow-up questions to continue the chat."
72
- messages = [{"role": "system", "content": system_message}]
73
-
74
- for val in history:
75
- if val[0]:
76
- messages.append({"role": "user", "content": val[0]})
77
- if val[1]:
78
- messages.append({"role": "assistant", "content": val[1]})
79
-
80
- # Preprocess user input
81
- preprocessed_message = preprocess_input(message)
82
- messages.append({"role": "user", "content": preprocessed_message})
83
-
84
- # RAG - Retrieve relevant documents
85
- retrieved_docs = app.search_documents(preprocessed_message)
86
- context = "\n".join(retrieved_docs)
87
- if context.strip():
88
- messages.append({"role": "system", "content": "Relevant documents: " + context})
89
-
90
- response = client.chat_completion(messages, max_tokens=1024, temperature=0.7, top_p=0.9)
91
- response_content = "".join([choice.message['content'] for choice in response.choices if 'content' in choice.message])
92
-
93
- polished_response = preprocess_response(response_content)
94
- shortened_response = shorten_response(polished_response)
95
-
96
- history.append((message, shortened_response))
97
- return history, ""
98
-
99
- with gr.Blocks() as demo:
100
- gr.Markdown("# 🧘‍♀️ **Dialectical Behaviour Therapy**")
101
- gr.Markdown(
102
- "‼️Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. "
103
- "We are not medical practitioners, and the use of this chatbot is at your own responsibility."
104
- )
105
-
106
- chatbot = gr.Chatbot()
107
-
108
- with gr.Row():
109
- txt_input = gr.Textbox(
110
- show_label=False,
111
- placeholder="Type your message here...",
112
- lines=1
113
- )
114
- submit_btn = gr.Button("Submit", scale=1)
115
- refresh_btn = gr.Button("Refresh Chat", scale=1, variant="secondary")
116
-
117
- example_questions = [
118
- ["What are some techniques to handle distressing situations?"],
119
- ["How does DBT help with emotional regulation?"],
120
- ["Can you give me an example of an interpersonal effectiveness skill?"],
121
- ["I want to practice mindfulness. Can you help me?"],
122
- ["I want to practice distraction techniques. What can I do?"],
123
- ["How do I plan self-accommodation?"],
124
- ["What are some distress tolerance skills?"],
125
- ["Can you help me with emotional regulation techniques?"],
126
- ["How can I improve my interpersonal effectiveness?"],
127
- ["What are some ways to cope with stress using DBT?"],
128
- ["Can you guide me through a grounding exercise?"],
129
- ["How do I use DBT skills to handle intense emotions?"],
130
- ["What are some self-soothing techniques I can practice?"],
131
- ["How can I create a sensory-friendly safe space?"],
132
- ["Can you help me create a personal crisis plan?"],
133
- ["What are some affirmations for neurodivergent individuals?"],
134
- ["How can I manage rejection sensitive dysphoria?"],
135
- ["Can you guide me through observing with my senses?"],
136
- ["What are some accessible mindfulness exercises?"],
137
- ["How do I engage my wise mind?"],
138
- ["What are some values that I can identify with?"],
139
- ["How can I practice mindful appreciation?"],
140
- ["What is the STOP skill in distress tolerance?"],
141
- ["How can I use the TIPP skill to manage distress?"],
142
- ["What are some tips for managing meltdowns?"],
143
- ["Can you provide a list of stims that I can use?"],
144
- ["How do I improve my environment to reduce distress?"]
145
- ]
146
-
147
- gr.Examples(examples=example_questions, inputs=[txt_input])
148
-
149
- submit_btn.click(respond, [txt_input, chatbot], [chatbot, txt_input])
150
- refresh_btn.click(lambda: [], None, chatbot)
151
-
152
- if __name__ == "__main__":
153
- demo.launch()