elinstallation commited on
Commit
586e7f3
·
verified ·
1 Parent(s): 370ba0e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +295 -0
app.py ADDED
@@ -0,0 +1,295 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #import libraries here
2
+ import gradio as gr
3
+ import random
4
+ from huggingface_hub import InferenceClient
5
+ #STEP 1: Import Sentence Transformer Library And Torch
6
+ from sentence_transformers import SentenceTransformer
7
+ import torch
8
+
9
+ with open("poverty_and_education.txt", "r", encoding="utf-8") as file:
10
+ # Read the entire contents of the file and store it in a variable
11
+ poverty_and_education = file.read()
12
+
13
+ with open("academic_tips_text.txt", "r", encoding="utf-8") as file:
14
+ # Read the entire contents of the file and store it in a variable
15
+ academic_tips_text = file.read()
16
+
17
+ with open("time_management.txt", "r", encoding="utf-8") as file:
18
+ # Read the entire contents of the file and store it in a variable
19
+ time_management = file.read()
20
+
21
+ with open("Extracurricular_ideas.txt", "r", encoding="utf-8") as file:
22
+ # Read the entire contents of the file and store it in a variable
23
+ extracurricular_ideas = file.read()
24
+
25
+ with open("financial_aid.txt", "r", encoding="utf-8") as file:
26
+ # Read the entire contents of the file and store it in a variable
27
+ financial_aid = file.read()
28
+
29
+ # Print the text below
30
+ print(academic_tips_text)
31
+
32
+ # ===== APPLY THE COMPLETE WORKFLOW =====
33
+
34
+ ### STEP 3
35
+ def preprocess_text(text):
36
+ # Strip extra whitespace from the beginning and the end of the text
37
+ cleaned_text = text.strip()
38
+
39
+ # Split the cleaned_text by every newline character (\n)
40
+ chunks = cleaned_text.split("\n")
41
+
42
+ # Create an empty list to store cleaned chunks
43
+ cleaned_chunks = []
44
+
45
+ # Write your for-in loop below to clean each chunk and add it to the cleaned_chunks list
46
+ for chunk in chunks:
47
+ stripped_chunk = chunk.strip()
48
+ if len(stripped_chunk) > 0:
49
+ cleaned_chunks.append(stripped_chunk)
50
+
51
+ # Print cleaned_chunks
52
+ print(cleaned_chunks)
53
+
54
+ # Print the length of cleaned_chunks
55
+ num_of_chunks = len(cleaned_chunks)
56
+ print(num_of_chunks)
57
+
58
+ print(f"There are {num_of_chunks} amount of chunks")
59
+ # Return the cleaned_chunks
60
+ return cleaned_chunks
61
+
62
+
63
+ # Load the pre-trained embedding model that converts text to vectors
64
+ model = SentenceTransformer('all-MiniLM-L6-v2')
65
+
66
+ ### STEP 4
67
+ def create_embeddings(text_chunks):
68
+ # Convert each text chunk into a vector embedding and store as a tensor
69
+ chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True) # Replace ... with the text_chunks list
70
+
71
+ # Print the chunk embeddings
72
+ print(chunk_embeddings)
73
+
74
+ # Print the shape of chunk_embeddings
75
+ print(chunk_embeddings.shape)
76
+
77
+ # Return the chunk_embeddings
78
+ return chunk_embeddings
79
+
80
+ # Call the create_embeddings function and store the result in a new chunk_embeddings variable
81
+ #chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line
82
+
83
+ ###STEP 5
84
+ # Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
85
+ def get_top_chunks(query, chunk_embeddings, text_chunks):
86
+ # Convert the query text into a vector embedding
87
+ query_embedding = model.encode(query, convert_to_tensor = True) # Complete this line
88
+
89
+ # Normalize the query embedding to unit length for accurate similarity comparison
90
+ query_embedding_normalized = query_embedding / query_embedding.norm()
91
+
92
+ # Normalize all chunk embeddings to unit length for consistent comparison
93
+ chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
94
+
95
+ # Calculate cosine similarity between query and all chunks using matrix multiplication
96
+ similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
97
+
98
+ # Print the similarities
99
+ print(similarities)
100
+
101
+ # Find the indices of the 3 chunks with highest similarity scores
102
+ top_indices = torch.topk(similarities, k=3).indices
103
+
104
+ # Print the top indices
105
+ print(top_indices)
106
+
107
+ # Create an empty list to store the most relevant chunks
108
+ top_chunks = []
109
+
110
+ # Loop through the top indices and retrieve the corresponding text chunks
111
+ for i in top_indices:
112
+ relevant_info = text_chunks[i]
113
+ top_chunks.append(relevant_info)
114
+
115
+ # Return the list of most relevant chunks
116
+ return top_chunks
117
+
118
+ # Print the top results
119
+ #print(top_results)
120
+ cleaned_chunks = preprocess_text(academic_tips_text)
121
+ cleaned_chunks2= preprocess_text(extracurricular_ideas)
122
+ cleaned_chunks3= preprocess_text(time_management)
123
+ cleaned_chunks4= preprocess_text(financial_aid)
124
+
125
+
126
+ chunk_embeddings = create_embeddings(cleaned_chunks)
127
+ chunk_embeddings2 = create_embeddings(cleaned_chunks2)
128
+ chunk_embeddings3 = create_embeddings(cleaned_chunks3)
129
+ chunk_embeddings4 = create_embeddings(cleaned_chunks4)
130
+
131
+
132
+ #AI API being used
133
+ client= InferenceClient("openai/gpt-oss-20b")
134
+ #defining role of AI and user
135
+
136
+ information=""
137
+ def respond(message,history):
138
+ topic_chunks=[]
139
+ if chatbot_topic=="Academia":
140
+
141
+ topic_chunks=get_top_chunks(message, chunk_embeddings, cleaned_chunks)
142
+ print(topic_chunks)
143
+
144
+ elif chatbot_topic=="Extracurriculars":
145
+ topic_chunks=get_top_chunks(message, chunk_embeddings2, cleaned_chunks2)
146
+ print(topic_chunks)
147
+
148
+
149
+ elif chatbot_topic=="Time Management":
150
+ topic_chunks=get_top_chunks(message, chunk_embeddings3, cleaned_chunks3)
151
+ print(topic_chunks)
152
+
153
+
154
+ elif chatbot_topic=="Financial Aid":
155
+ topic_chunks=get_top_chunks(message, chunk_embeddings4, cleaned_chunks4)
156
+ print(topic_chunks)
157
+
158
+ #return information
159
+ #return topic_chunks
160
+ if chatbot_mode=="Peer Mode":
161
+ messages = [{"role": "assistant", "content": f"You are a casual, sometimes funny chatbot who acts like a peer of the person who is asking the question. You relate to their situation and give them relevant advice. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. You respond clearly in under three complete bullet points under 250 characters. When you give advice, keep in mind the following information {topic_chunks}"}]
162
+
163
+ if chatbot_mode=="Guidance Counselor Mode":
164
+ messages = [{"role": "assistant", "content": f"You act as a helpful guidance counselor with an educated understanding of high school life and college admissions. You guide the student to consider their academic potential, while maintaining the passion and balance they need. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. When you give advice, keep in mind the following information {topic_chunks}"}]
165
+
166
+ if chatbot_mode=="Parent Mode":
167
+ messages = [{"role": "assistant", "content": f"You are a guiding, nurturing, and protective parent who wants their student to reach their fullest potential while learning to grow up with the proper physical, emotional, and social development. You want to build your student into a responsible adult, but also want them to pursue success in their life and establish a good future. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. When you give advice, keep in mind the following information {information}"}]
168
+
169
+ else:
170
+ messages = [{"role": "assistant", "content": f"You are a friendly, helpful chatbot that gives academic advice to disadvantaged students about their education based on their question. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. When you give advice, keep in mind the following information {topic_chunks}"}]
171
+
172
+ if history:
173
+ messages.extend(history) #keep adding history
174
+
175
+ messages.append({"role":"user","content": message})
176
+
177
+ response=client.chat_completion(messages, temperature=0.2)#capping how many words the LLM is allowed to generate as a respond (100 words)
178
+
179
+ return response['choices'][0]['message']['content'].strip() #storing value of response in a readable format to display
180
+
181
+ ### STEP 6
182
+ # Call the preprocess_text function and store the result in a cleaned_chunks variable
183
+ cleaned_chunks = preprocess_text(academic_tips_text) # Complete this line
184
+ top_results = get_top_chunks("How does poverty affect one's education?", chunk_embeddings, cleaned_chunks) # Complete this line
185
+
186
+ print(top_results)
187
+
188
+ #Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
189
+ #ChatInterface requires at least one parameter(a function)
190
+ chatbot = gr.ChatInterface(respond,type="messages", title="Accessible Intelligence Hub", theme="Taithrah/Minimal")
191
+
192
+ def save_chat_history(history, username):
193
+ if not username:
194
+ username = "anonymous"
195
+
196
+ timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
197
+ filename = f"chat_history_{username}_{timestamp}.txt"
198
+
199
+ with open(filename, "w", encoding="utf-8") as f:
200
+ f.write(f"Chat History for {username} - {timestamp}\n\n")
201
+ for exchange in history:
202
+ if isinstance(exchange, tuple) and len(exchange) == 2:
203
+ user_msg, bot_msg = exchange
204
+ f.write(f"User: {user_msg}\n")
205
+ f.write(f"Bot: {bot_msg}\n\n")
206
+ elif isinstance(exchange, dict):
207
+ # Handle dictionary format if needed
208
+ role = exchange.get("role", "unknown")
209
+ content = exchange.get("content", "")
210
+ f.write(f"{role.capitalize()}: {content}\n\n")
211
+
212
+ return filename
213
+
214
+ with gr.Blocks(
215
+ theme=gr.themes.Soft(
216
+ primary_hue="purple",
217
+ secondary_hue="fuchsia",
218
+ neutral_hue="gray",
219
+ text_size="lg"
220
+ ).set(
221
+ background_fill_primary='*neutral_200',
222
+ background_fill_secondary='neutral_100',
223
+ background_fill_secondary_dark='secondary_500',
224
+ border_color_accent='*secondary_400',
225
+ border_color_accent_dark='*secondary_800',
226
+ color_accent='*secondary_600',
227
+ color_accent_soft='*secondary_200',
228
+ color_accent_soft_dark='*secondary_800',
229
+ button_primary_background_fill='*secondary_400',
230
+ button_primary_background_fill_dark='*secondary_600',
231
+ button_primary_text_color='white',
232
+ button_primary_border_color='*secondary_700',
233
+ button_primary_border_color_dark='*secondary_900'
234
+ )
235
+ ) as demo:
236
+ with gr.Row(scale=1):
237
+ chatbot_topic=gr.CheckboxGroup(["Academia", "Extracurriculars", "Time Management", "Financial Aid"], label="What would you like advice about?")
238
+ with gr.Row(scale=1):
239
+ chatbot_mode=gr.CheckboxGroup(["Guidance Counselor Mode", "Peer Mode", "Parent Mode"], label="How would you like the chatbot to respond?")
240
+
241
+ #with gr.Row():
242
+ #save_button = gr.Button("💾 Save Chat History",
243
+ #variant="primary",
244
+ #size="sm")
245
+ #download_button = gr.File(interactive=True,
246
+ #visible=True,
247
+ #elem_classes=["download-btn"])
248
+
249
+ gr.ChatInterface(
250
+ fn=respond,
251
+ title="Accessible Intelligence Hub",
252
+ description="Ask about your education",
253
+ type="messages",
254
+
255
+
256
+ )
257
+ #with gr.Row(scale=1):
258
+ #chatbot_topic=gr.CheckboxGroup(["Academia", "Extracurriculars", "Time Management", "Financial Aid"], label="What would you like advice about?")
259
+ #with gr.Row(scale=1):
260
+ #chatbot_mode=gr.CheckboxGroup(["Guidance Counselor Mode", "Peer Mode", "Parent Mode"], label="How would you like the chatbot to respond?")
261
+
262
+ #demo.css = """
263
+ #.download-btn {
264
+ # min-width: 200px !important;
265
+ #}
266
+ #.download-btn .gr-button {
267
+ # background: var(--button-primary-background-fill) !important;
268
+ # color: var(--button-primary-text-color) !important;
269
+ #}
270
+ #"""
271
+
272
+
273
+ #with gr.Blocks() as demo:
274
+ #chatbot = gr.Chatbot()
275
+ username_input = gr.Textbox(label="Username")
276
+ save_button = gr.Button("Save Chat History")
277
+ download_button = gr.File(label="Download Chat History", visible=False)
278
+
279
+ save_button.click(
280
+ fn=save_chat_history,
281
+ inputs=[chatbot, username_input],
282
+ outputs=download_button
283
+ ).then(
284
+ fn=lambda: gr.update(visible=True),
285
+ outputs=download_button
286
+ )
287
+ save_button = gr.Button("💾 Save Chat History",
288
+ variant="primary",
289
+ size="sm")
290
+ download_button = gr.File(interactive=True,
291
+ visible=True,
292
+ elem_classes=["download-btn"])
293
+
294
+ #launching chatbot
295
+ demo.launch()