Rahatara commited on
Commit
50137a9
1 Parent(s): b0d40a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
6
+ import numpy as np
7
+ import faiss
8
+ from gtts import gTTS
9
+ import os
10
+ from PIL import Image
11
+ from moviepy.editor import ImageSequenceClip
12
+
13
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
14
+
15
+ class MyApp:
16
+ def __init__(self) -> None:
17
+ self.documents = []
18
+ self.embeddings = None
19
+ self.index = None
20
+ self.load_pdf("THEDIA1.pdf")
21
+ self.build_vector_db()
22
+
23
+ def load_pdf(self, file_path: str) -> None:
24
+ doc = fitz.open(file_path)
25
+ self.documents = []
26
+ for page_num in range(len(doc)):
27
+ page = doc[page_num]
28
+ text = page.get_text()
29
+ self.documents.append({"page": page_num + 1, "content": text})
30
+ print("PDF processed successfully!")
31
+
32
+ def build_vector_db(self) -> None:
33
+ model = SentenceTransformer('all-MiniLM-L6-v2')
34
+ self.embeddings = model.encode([doc["content"] for doc in self.documents], show_progress_bar=True)
35
+ self.index = faiss.IndexFlatL2(self.embeddings.shape[1])
36
+ self.index.add(np.array(self.embeddings))
37
+ print("Vector database built successfully!")
38
+
39
+ def search_documents(self, query: str, k: int = 3) -> List[str]:
40
+ model = SentenceTransformer('all-MiniLM-L6-v2')
41
+ query_embedding = model.encode([query], show_progress_bar=False)
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_response(response: str) -> str:
49
+ response = response.strip()
50
+ response = response.replace("\n\n", "\n")
51
+ response = response.replace(" ,", ",")
52
+ response = response.replace(" .", ".")
53
+ response = " ".join(response.split())
54
+ if not any(word in response.lower() for word in ["sorry", "apologize", "empathy"]):
55
+ response = "I'm here to help. " + response
56
+ return response
57
+
58
+ def shorten_response(response: str) -> str:
59
+ messages = [{"role": "system", "content": "Shorten and refine this response in a supportive and empathetic manner."}, {"role": "user", "content": response}]
60
+ result = client.chat_completion(messages, max_tokens=512, temperature=0.5, top_p=0.9)
61
+ return result.choices[0].message['content'].strip()
62
+
63
+ def text_to_speech(text: str, lang: str = 'en'):
64
+ tts = gTTS(text=text, lang=lang, slow=False)
65
+ tts.save("response.mp3")
66
+ return "response.mp3"
67
+
68
+ def create_speaking_avatar(image_path: str, audio_path: str):
69
+ # Use a simple way to generate a video where the image "speaks" the text
70
+ image = Image.open(image_path)
71
+ frames = [image] * 30 # 1 second at 30fps
72
+ clip = ImageSequenceClip([np.array(f) for f in frames], fps=30)
73
+ clip = clip.set_audio(audio_path)
74
+ output_path = "output.mp4"
75
+ clip.write_videofile(output_path, codec="libx264")
76
+ return output_path
77
+
78
+ def respond(message: str, history: List[Tuple[str, str]]):
79
+ system_message = "You are a supportive and 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."
80
+ messages = [{"role": "system", "content": system_message}]
81
+
82
+ for val in history:
83
+ if val[0]:
84
+ messages.append({"role": "user", "content": val[0]})
85
+ if val[1]:
86
+ messages.append({"role": "assistant", "content": val[1]})
87
+
88
+ messages.append({"role": "user", "content": message})
89
+
90
+ if any(keyword in message.lower() for keyword in ["exercise", "technique", "information", "guide", "help", "how to"]):
91
+ retrieved_docs = app.search_documents(message)
92
+ context = "\n".join(retrieved_docs)
93
+ if context.strip():
94
+ messages.append({"role": "system", "content": "Relevant documents: " + context})
95
+
96
+ response = client.chat_completion(messages, max_tokens=1024, temperature=0.7, top_p=0.9)
97
+ response_content = "".join([choice.message['content'] for choice in response.choices if 'content' in choice.message])
98
+
99
+ polished_response = preprocess_response(response_content)
100
+ shortened_response = shorten_response(polished_response)
101
+
102
+ history.append((message, shortened_response))
103
+
104
+ # Convert response text to speech and create the speaking avatar
105
+ audio_path = text_to_speech(shortened_response)
106
+ avatar_video_path = create_speaking_avatar("avatar.png", audio_path)
107
+
108
+ return history, "", audio_path, avatar_video_path
109
+
110
+ with gr.Blocks() as demo:
111
+ gr.Markdown("# 🧘‍♀️ **Dialectical Behaviour Therapy**")
112
+ gr.Markdown(
113
+ "‼️Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. "
114
+ "We are not medical practitioners, and the use of this chatbot is at your own responsibility."
115
+ )
116
+
117
+ chatbot = gr.Chatbot()
118
+
119
+ with gr.Row():
120
+ txt_input = gr.Textbox(
121
+ show_label=False,
122
+ placeholder="Type your message here...",
123
+ lines=1
124
+ )
125
+ submit_btn = gr.Button("Submit", scale=1)
126
+ refresh_btn = gr.Button("Refresh Chat", scale=1, variant="secondary")
127
+
128
+ example_questions = [
129
+ ["What are some techniques to handle distressing situations?"],
130
+ ["How does DBT help with emotional regulation?"],
131
+ ["Can you give me an example of an interpersonal effectiveness skill?"],
132
+ ["I want to practice mindfulness. Can you help me?"],
133
+ ]
134
+
135
+ gr.Examples(examples=example_questions, inputs=[txt_input])
136
+
137
+ submit_btn.click(fn=respond, inputs=[txt_input, chatbot], outputs=[chatbot, txt_input, gr.Audio(), gr.Video()])
138
+ refresh_btn.click(lambda: [], None, chatbot)
139
+
140
+ if __name__ == "__main__":
141
+ demo.launch()