elinstallation commited on
Commit
8dd212a
·
verified ·
1 Parent(s): 54b64fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -57
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  import random
3
  from huggingface_hub import InferenceClient
4
 
5
- #STEP 1: Import Sentence Transformer Library And Torch
6
  from sentence_transformers import SentenceTransformer
7
  import torch
8
 
@@ -17,9 +16,9 @@ charities_text = load_files("charities.txt")
17
  financial_advice_text = load_files("financial_advice.txt")
18
 
19
 
20
- # ===== APPLY THE COMPLETE WORKFLOW =====
21
 
22
- ### STEP 3
23
  def preprocess_text(text):
24
  # Strip extra whitespace from the beginning and the end of the text
25
  cleaned_text = text.strip()
@@ -36,15 +35,12 @@ def preprocess_text(text):
36
  if len(stripped_chunk) > 0:
37
  cleaned_chunks.append(stripped_chunk)
38
 
39
- # Print cleaned_chunks
40
- #print(cleaned_chunks)
41
-
42
  # Print the length of cleaned_chunks
43
  num_of_chunks = len(cleaned_chunks)
44
  # print(num_of_chunks)
45
 
46
- # print(f"There are {num_of_chunks} amount of chunks")
47
- # Return the cleaned_chunks
48
  return cleaned_chunks
49
 
50
  cleaned_charities = preprocess_text(charities_text)
@@ -69,8 +65,6 @@ def create_embeddings(text_chunks):
69
 
70
  charity_embeddings = create_embeddings(cleaned_charities)
71
  finance_embeddings = create_embeddings(cleaned_finance)
72
- # Call the create_embeddings function and store the result in a new chunk_embeddings variable
73
- #chunk_embeddings = create_embeddings(cleaned_chunks) # Complete this line
74
 
75
  ###STEP 5
76
  # Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
@@ -87,15 +81,11 @@ def get_top_chunks(query, chunk_embeddings, text_chunks):
87
  # Calculate cosine similarity between query and all chunks using matrix multiplication
88
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
89
 
90
- # Print the similarities
91
- #print(similarities)
92
 
93
  # Find the indices of the 3 chunks with highest similarity scores
94
  top_indices = torch.topk(similarities, k=3).indices
95
 
96
- # Print the top indices
97
- #print(top_indices)
98
-
99
  # Create an empty list to store the most relevant chunks
100
  top_chunks = []
101
 
@@ -139,38 +129,12 @@ def respond(message, history, chatbot_topic_values, chatbot_mode_values):
139
  response = client.chat_completion(messages, temperature=0.2)
140
  return response['choices'][0]['message']['content'].strip()
141
 
142
- #def respond(message,history):
143
- # topic_chunks = []
144
- # if chatbot_topic and "Helping Charities" in chatbot_topic:
145
- # topic_chunks = get_top_chunks(message, charity_embeddings, cleaned_charities)
146
- #elif chatbot_topic and "Financial Aid" in chatbot_topic:
147
- # topic_chunks = get_top_chunks(message, finance_embeddings, cleaned_finance)
148
-
149
-
150
- #return information
151
- #return topic_chunks
152
- #chatbot_mode=="Advice Mode"
153
- #messages = [{"role": "assistant", "content": f"You are a helpful and insightful chatbot who acts like a financial advisor of a university student who wants to learn to manage their personal finances. You analyse their situation and give relevant advice and insights. You only answer in complete sentences with correct grammar, punctuation, and complete ideas. You respond clearly in under five complete bullet points under 500 characters. When you give advice, keep in mind the following information {topic_chunks}"}]
154
-
155
- # if chatbot_mode == "Advice Mode":
156
- # role_message = ( "You are a helpful and insightful chatbot who acts like a financial " "advisor of a university student. Respond in under five bullet points, " f"under 500 characters, using this context: {topic_chunks}" )
157
- # else: role_message = f"You are a helpful chatbot. Use this context: {topic_chunks}"
158
- #
159
- # if history:
160
- # messages.extend(history) #keep adding history
161
-
162
- # messages.append({"role":"user","content": message})
163
-
164
- #response=client.chat_completion(messages, temperature=0.2)#capping how many words the LLM is allowed to generate as a respond (100 words)
165
-
166
- #return response['choices'][0]['message']['content'].strip() #storing value of response in a readable format to display
167
 
168
  ### STEP 6
169
  # Call the preprocess_text function and store the result in a cleaned_chunks variable
170
  cleaned_chunks = preprocess_text(financial_advice_text) # Complete this line
171
  top_results = get_top_chunks("What financial advice you give me?", finance_embeddings, cleaned_finance)
172
 
173
- #print(top_results)
174
 
175
  #Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
176
  #ChatInterface requires at least one parameter(a function)
@@ -200,10 +164,11 @@ def save_chat_history(history, username):
200
 
201
  with gr.Blocks(
202
  theme=gr.themes.Soft(
203
- primary_hue="purple",
204
  secondary_hue="fuchsia",
205
  neutral_hue="gray",
206
- text_size="lg"
 
207
  ).set(
208
  background_fill_primary='*neutral_200',
209
  background_fill_secondary='neutral_100',
@@ -225,20 +190,6 @@ with gr.Blocks(
225
  with gr.Row(scale=1):
226
  chatbot_mode=gr.CheckboxGroup(["Advice Mode"], label="How would you like the chatbot to respond?")
227
 
228
- #with gr.Row():
229
- #save_button = gr.Button("💾 Save Chat History",
230
- #variant="primary",
231
- #size="sm")
232
- #download_button = gr.File(interactive=True,
233
- #visible=True,
234
- #elem_classes=["download-btn"])
235
-
236
- # gr.ChatInterface(
237
- # fn=lambda msg, hist: respond(msg, hist, chatbot_topic.value, chatbot_mode.value),
238
- # title="Finance Management Hub",
239
- # description="Ask about your personal finance",
240
- # type="messages",
241
- #)
242
  gr.ChatInterface(
243
  fn=lambda msg, hist, topic_vals, mode_vals: respond(msg, hist, topic_vals, mode_vals),
244
  title="Finance Management Hub",
 
2
  import random
3
  from huggingface_hub import InferenceClient
4
 
 
5
  from sentence_transformers import SentenceTransformer
6
  import torch
7
 
 
16
  financial_advice_text = load_files("financial_advice.txt")
17
 
18
 
19
+ #
20
 
21
+ ###
22
  def preprocess_text(text):
23
  # Strip extra whitespace from the beginning and the end of the text
24
  cleaned_text = text.strip()
 
35
  if len(stripped_chunk) > 0:
36
  cleaned_chunks.append(stripped_chunk)
37
 
38
+
 
 
39
  # Print the length of cleaned_chunks
40
  num_of_chunks = len(cleaned_chunks)
41
  # print(num_of_chunks)
42
 
43
+
 
44
  return cleaned_chunks
45
 
46
  cleaned_charities = preprocess_text(charities_text)
 
65
 
66
  charity_embeddings = create_embeddings(cleaned_charities)
67
  finance_embeddings = create_embeddings(cleaned_finance)
 
 
68
 
69
  ###STEP 5
70
  # Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
 
81
  # Calculate cosine similarity between query and all chunks using matrix multiplication
82
  similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) # Complete this line
83
 
84
+
 
85
 
86
  # Find the indices of the 3 chunks with highest similarity scores
87
  top_indices = torch.topk(similarities, k=3).indices
88
 
 
 
 
89
  # Create an empty list to store the most relevant chunks
90
  top_chunks = []
91
 
 
129
  response = client.chat_completion(messages, temperature=0.2)
130
  return response['choices'][0]['message']['content'].strip()
131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  ### STEP 6
134
  # Call the preprocess_text function and store the result in a cleaned_chunks variable
135
  cleaned_chunks = preprocess_text(financial_advice_text) # Complete this line
136
  top_results = get_top_chunks("What financial advice you give me?", finance_embeddings, cleaned_finance)
137
 
 
138
 
139
  #Defining chatbot giving user a UI to interact, see their conversation history, and see new messages using built in gr feature
140
  #ChatInterface requires at least one parameter(a function)
 
164
 
165
  with gr.Blocks(
166
  theme=gr.themes.Soft(
167
+ primary_hue="blue",
168
  secondary_hue="fuchsia",
169
  neutral_hue="gray",
170
+ text_size="lg",
171
+ text_font="Roboto"
172
  ).set(
173
  background_fill_primary='*neutral_200',
174
  background_fill_secondary='neutral_100',
 
190
  with gr.Row(scale=1):
191
  chatbot_mode=gr.CheckboxGroup(["Advice Mode"], label="How would you like the chatbot to respond?")
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  gr.ChatInterface(
194
  fn=lambda msg, hist, topic_vals, mode_vals: respond(msg, hist, topic_vals, mode_vals),
195
  title="Finance Management Hub",