allyyy commited on
Commit
7eeea4f
·
verified ·
1 Parent(s): adadb5c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -138
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
- import openai
4
  import os
5
 
6
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
@@ -9,17 +9,18 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
9
  filename = "output_topic_details.txt" # Path to the file storing destress-specific details
10
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
11
 
12
- #openai.api_key = os.environ["OPENAI_API_KEY"]
 
 
13
 
14
- system_message = "You are a comfort chatbot specialized in providing information on therapy, destressing activites, and student opportunities."
15
- # Initial system message to set the behavior of the assistant
16
  messages = [{"role": "system", "content": system_message}]
17
  messages.append({
18
- "role": "system",
19
- "content": "Do not use Markdown Format. Do not include hashtags or asterisks"
20
  })
21
 
22
- # Attempt to load the necessary models and provide feedback on success or failure
23
  try:
24
  retrieval_model = SentenceTransformer(retrieval_model_name)
25
  print("Models loaded successfully.")
@@ -27,9 +28,6 @@ except Exception as e:
27
  print(f"Failed to load models: {e}")
28
 
29
  def load_and_preprocess_text(filename):
30
- """
31
- Load and preprocess text from a file, removing empty lines and stripping whitespace.
32
- """
33
  try:
34
  with open(filename, 'r', encoding='utf-8') as file:
35
  segments = [line.strip() for line in file if line.strip()]
@@ -42,56 +40,29 @@ def load_and_preprocess_text(filename):
42
  segments = load_and_preprocess_text(filename)
43
 
44
  def find_relevant_segment(user_query, segments):
45
- """
46
- Find the most relevant text segment for a user's query using cosine similarity among sentence embeddings.
47
- This version finds the best match based on the content of the query.
48
- """
49
  try:
50
- # Lowercase the query for better matching
51
  lower_query = user_query.lower()
52
-
53
- # Encode the query and the segments
54
  query_embedding = retrieval_model.encode(lower_query)
55
  segment_embeddings = retrieval_model.encode(segments)
56
-
57
- # Compute cosine similarities between the query and the segments
58
  similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
59
-
60
- # Find the index of the most similar segment
61
  best_idx = similarities.argmax()
62
-
63
- # Return the most relevant segment
64
  return segments[best_idx]
65
  except Exception as e:
66
  print(f"Error in finding relevant segment: {e}")
67
  return ""
68
 
69
  def generate_response(user_query, relevant_segment):
70
- """
71
- Generate a response emphasizing the bot's capability in providing therapy, destressing activites, and student opportunities information.
72
- """
73
  try:
74
  user_message = f"Here's the information on your request: {relevant_segment}"
75
-
76
- # Append user's message to messages list
77
  messages.append({"role": "user", "content": user_message})
78
-
79
- response = openai.ChatCompletion.create(
80
- model="gpt-3.5-turbo", # or "gpt-4" if you have access
81
- messages=messages,
82
- max_tokens=500,
83
- temperature=0.5,
84
- top_p=1,
85
- frequency_penalty=0.5,
86
- presence_penalty=0.5,
87
- )
88
-
89
- # Extract the response text
90
- output_text = response['choices'][0]['message']['content'].strip()
91
-
92
  # Append assistant's message to messages list for context
93
  messages.append({"role": "assistant", "content": output_text})
94
-
95
  return output_text
96
 
97
  except Exception as e:
@@ -99,9 +70,6 @@ def generate_response(user_query, relevant_segment):
99
  return f"Error in generating response: {e}"
100
 
101
  def query_model(question):
102
- """
103
- Process a question, find relevant information, and generate a response.
104
- """
105
  if question == "":
106
  return "Welcome to CalmConnect! Ask me anything about destressing strategies or student opportunities. Feel free to talk to our online therapist!"
107
  relevant_segment = find_relevant_segment(question, segments)
@@ -111,112 +79,33 @@ def query_model(question):
111
  return response
112
 
113
  # Define the HTML iframe content
114
- iframe = '''
115
- <iframe style="border-radius:12px" src="https://docs.google.com/spreadsheets/d/e/2PACX-1vRroWVBXq1Fa0x7SvRTzSBMHFIp59VtVEWCxeg8kWJU4ll1_o4yzBnt4ArT88s7g4TQrMKEXZUQAeHF/pubhtml?widget=true&amp;headers=false" width="100%" height="352" frameBorder="0" allowfullscreen="true" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>
116
- '''
117
-
118
- iframe2 = '''
119
- <iframe style="border-radius:12px" src="https://open.spotify.com/embed/playlist/6wwxTePuIKYMqt6RCytB7X?utm_source=generator" width="100%" height="300" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe>
120
- '''
121
-
122
 
123
  # Define the welcome message and specific topics the chatbot can provide information about
124
- welcome_message = """
125
- <span style="color:#718355; font-size:24px; font-weight:bold;"> 🪷 Welcome to CalmConnect! 🪷</span>
126
- """
127
-
128
- """
129
- ## Your AI-driven assistant for destressing and extracurricular opportunity queries. Created by Olivia W, Alice T, and Cindy W of the 2024 Kode With Klossy CITY Camp.
130
- """
131
-
132
- topics = """
133
- ### If you are interested in the following below, click on our Student Opportunities Database!
134
- - Engineering
135
- - Technology / Computer Science
136
- - Research : STEM
137
- - Finance
138
- - Law / Political Science / Debate
139
- - The Arts
140
- - Business / Leadership
141
- - Pyschology
142
- - Medicine / Biology
143
- - Literature / Writing
144
- - College Prep
145
- - Advocacy: Non-Profit, Environment or Identity
146
- - Volunteering
147
- - Study Abroad
148
- """
149
-
150
- topics2= """
151
- ### Feel Free to ask CalmBot (Our Therapist Bot) anything from the topics below!
152
- - Arts and Crafts (When asking for arts and crafts ideas, state whether you have 15 min, 30 min, 45 min, 1 hour, 1 hour and a half, 2 hours, 2 hours and a half, 3 hours or greater)
153
- - Destressing strategies (Breathing Exercises, stretches, etc.)
154
- - Mental Health
155
- - Identity (Sexual, Gender, etc.)
156
- - Bullying
157
- - Racism
158
- - Relationships (Family, Friends, etc.)
159
- - Abuse (Emotional, Physical, Sexual, Mental, etc.)
160
- - Support Resources
161
- """
162
-
163
- # Create a Gradio HTML component
164
- def display_iframe():
165
- return iframe
166
-
167
- def display_iframe2():
168
- return iframe2
169
-
170
-
171
- theme = gr.themes.Default(
172
- primary_hue="neutral",
173
- secondary_hue="neutral",
174
- ).set(
175
- background_fill_primary='#e3e9da',
176
- background_fill_primary_dark='#e3e9da',
177
- background_fill_secondary="#f8f1ea",
178
- background_fill_secondary_dark="#f8f1ea",
179
- border_color_accent="#f8f1ea",
180
- border_color_accent_dark="#e3e9da",
181
- border_color_accent_subdued="#f8f1ea",
182
- border_color_primary="#f8f1ea",
183
- block_border_color="#f8f1ea",
184
- button_primary_background_fill="#f8f1ea",
185
- button_primary_background_fill_dark="#f8f1ea"
186
- )
187
-
188
  # Setup the Gradio Blocks interface with custom layout components
189
- with gr.Blocks(theme=theme) as demo:
190
  gr.Image("CalmConnect.jpg", show_label=False, show_share_button=False, show_download_button=False)
191
- gr.Markdown(welcome_message) # Display the formatted welcome message
192
-
193
  with gr.Row():
194
  with gr.Column():
195
- gr.Markdown(topics) # Show the topics on the left side
196
- gr.HTML(iframe) # Embed the iframe on the left side
197
- gr.HTML(iframe2) # Embed the iframe on the right side
198
 
199
  with gr.Column():
200
- gr.Markdown(topics2) # Show the topics on the left side
201
  with gr.Row():
202
  with gr.Column():
203
  question = gr.Textbox(label="You", placeholder="What do you want to talk to CalmBot about?")
204
  answer = gr.Textbox(label="CalmBot's Response :D", placeholder="CalmBot will respond here..", interactive=False, lines=20)
205
  submit_button = gr.Button("Submit")
206
  submit_button.click(fn=query_model, inputs=question, outputs=answer)
207
-
208
- with gr.Row():
209
- big_block = gr.HTML("### <button><a href='https://www.headspace.com/teens'>FREE: HEADSPACE FOR TEENS </a></button>")
210
- big_block2 = gr.HTML("<button><a href='https://calmconnect-flower.replit.app/'>PLAY FLOWER GAME</a></button>")
211
- big_block3 = gr.HTML("<button><a href='https://www.nyc.gov/site/doh/health/health-topics/teenspace.page'>NYC: TEENSPACE (free services)</a></button>")
212
- big_block4 =gr.HTML("<button><a href='https://www.teenlife.com/blog/mental-health-resources-for-teens/'>TEEN MENTAL HEALTH RESOURCES (free services)</a></button>")
213
-
214
-
215
- demo.launch()
216
-
217
-
218
-
219
 
 
 
220
 
221
  # Launch the Gradio app to allow user interaction
222
- demo.launch(share=True)
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
  import os
5
 
6
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
 
9
  filename = "output_topic_details.txt" # Path to the file storing destress-specific details
10
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
11
 
12
+ # Load GPT-2 model and tokenizer
13
+ tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
14
+ model = GPT2LMHeadModel.from_pretrained("gpt2")
15
 
16
+ system_message = "You are a comfort chatbot specialized in providing information on therapy, destressing activities, and student opportunities."
 
17
  messages = [{"role": "system", "content": system_message}]
18
  messages.append({
19
+ "role": "system",
20
+ "content": "Do not use Markdown Format. Do not include hashtags or asterisks"
21
  })
22
 
23
+ # Load the retrieval model
24
  try:
25
  retrieval_model = SentenceTransformer(retrieval_model_name)
26
  print("Models loaded successfully.")
 
28
  print(f"Failed to load models: {e}")
29
 
30
  def load_and_preprocess_text(filename):
 
 
 
31
  try:
32
  with open(filename, 'r', encoding='utf-8') as file:
33
  segments = [line.strip() for line in file if line.strip()]
 
40
  segments = load_and_preprocess_text(filename)
41
 
42
  def find_relevant_segment(user_query, segments):
 
 
 
 
43
  try:
 
44
  lower_query = user_query.lower()
 
 
45
  query_embedding = retrieval_model.encode(lower_query)
46
  segment_embeddings = retrieval_model.encode(segments)
 
 
47
  similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
 
 
48
  best_idx = similarities.argmax()
 
 
49
  return segments[best_idx]
50
  except Exception as e:
51
  print(f"Error in finding relevant segment: {e}")
52
  return ""
53
 
54
  def generate_response(user_query, relevant_segment):
 
 
 
55
  try:
56
  user_message = f"Here's the information on your request: {relevant_segment}"
 
 
57
  messages.append({"role": "user", "content": user_message})
58
+
59
+ # Encode the input and generate a response
60
+ input_ids = tokenizer.encode(user_message, return_tensors='pt')
61
+ output = model.generate(input_ids, max_length=150, num_return_sequences=1)
62
+ output_text = tokenizer.decode(output[0], skip_special_tokens=True)
63
+
 
 
 
 
 
 
 
 
64
  # Append assistant's message to messages list for context
65
  messages.append({"role": "assistant", "content": output_text})
 
66
  return output_text
67
 
68
  except Exception as e:
 
70
  return f"Error in generating response: {e}"
71
 
72
  def query_model(question):
 
 
 
73
  if question == "":
74
  return "Welcome to CalmConnect! Ask me anything about destressing strategies or student opportunities. Feel free to talk to our online therapist!"
75
  relevant_segment = find_relevant_segment(question, segments)
 
79
  return response
80
 
81
  # Define the HTML iframe content
82
+ # (Your iframe content goes here)
 
 
 
 
 
 
 
83
 
84
  # Define the welcome message and specific topics the chatbot can provide information about
85
+ # (Your welcome message and topics go here)
86
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  # Setup the Gradio Blocks interface with custom layout components
88
+ with gr.Blocks() as demo:
89
  gr.Image("CalmConnect.jpg", show_label=False, show_share_button=False, show_download_button=False)
90
+ gr.Markdown(welcome_message)
91
+
92
  with gr.Row():
93
  with gr.Column():
94
+ gr.Markdown(topics)
95
+ gr.HTML(iframe)
96
+ gr.HTML(iframe2)
97
 
98
  with gr.Column():
99
+ gr.Markdown(topics2)
100
  with gr.Row():
101
  with gr.Column():
102
  question = gr.Textbox(label="You", placeholder="What do you want to talk to CalmBot about?")
103
  answer = gr.Textbox(label="CalmBot's Response :D", placeholder="CalmBot will respond here..", interactive=False, lines=20)
104
  submit_button = gr.Button("Submit")
105
  submit_button.click(fn=query_model, inputs=question, outputs=answer)
 
 
 
 
 
 
 
 
 
 
 
 
106
 
107
+ with gr.Row():
108
+ # (Your buttons go here)
109
 
110
  # Launch the Gradio app to allow user interaction
111
+ demo.launch(share=True)