elinstallation commited on
Commit
eba1eb3
·
verified ·
1 Parent(s): 67961ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -35
app.py CHANGED
@@ -139,7 +139,7 @@ client= InferenceClient("openai/gpt-oss-20b")
139
 
140
  information=""
141
 
142
- def respond(message, history, chatbot_topic_values, chatbot_mode_values, user_id=1):
143
  topic_chunks = []
144
  if chatbot_topic_values and "Helping Charities" in chatbot_topic_values:
145
  topic_chunks = get_top_chunks(message, charity_embeddings, cleaned_charities)
@@ -147,22 +147,28 @@ def respond(message, history, chatbot_topic_values, chatbot_mode_values, user_id
147
  topic_chunks = get_top_chunks(message, finance_embeddings, cleaned_finance)
148
 
149
  csv_advice = get_advice(user_id)
150
-
151
- if chatbot_mode_values and "General Advice" in chatbot_mode_values:
152
- role_message = (
153
- "You are a helpful and insightful chatbot who acts like a financial "
154
- "advisor of a university student. Respond in under five bullet points, "
155
- f"under 500 characters, using this context: {topic_chunks}"
156
- )
157
- elif chatbot_mode_values and "Personal Advice" in chatbot_mode_values:
158
- role_message = (
159
- "You are a helpful and insightful chatbot who acts like a financial "
160
- "DO NOT ask the user for additional numbers or input"
161
- f"Use the following spending data from the CSV file to provide advice {csv_advice}"
162
- )
163
- else:
164
- role_message = f"You are a helpful chatbot. Use this context: {topic_chunks}"
165
- messages = [{"role": "assistant", "content": role_message}]
 
 
 
 
 
 
166
  if history:
167
  messages.extend(history)
168
  messages.append({"role": "user", "content": message})
@@ -181,27 +187,27 @@ top_results = get_top_chunks("What financial advice you give me?", finance_embed
181
  #ChatInterface requires at least one parameter(a function)
182
  chatbot = gr.ChatInterface(respond,type="messages", title="Finance Management Hub", theme="Taithrah/Minimal")
183
 
184
- def save_chat_history(history, username):
185
- if not username:
186
- username = "anonymous"
187
 
188
- timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
189
- filename = f"chat_history_{username}_{timestamp}.txt"
190
 
191
- with open(filename, "w", encoding="utf-8") as f:
192
- f.write(f"Chat History for {username} - {timestamp}\n\n")
193
- for exchange in history:
194
- if isinstance(exchange, tuple) and len(exchange) == 2:
195
- user_msg, bot_msg = exchange
196
- f.write(f"User: {user_msg}\n")
197
- f.write(f"Bot: {bot_msg}\n\n")
198
- elif isinstance(exchange, dict):
199
  # Handle dictionary format if needed
200
- role = exchange.get("role", "unknown")
201
- content = exchange.get("content", "")
202
- f.write(f"{role.capitalize()}: {content}\n\n")
203
 
204
- return filename
205
 
206
  with gr.Blocks(
207
  theme=gr.themes.Soft(
@@ -232,7 +238,7 @@ with gr.Blocks(
232
  chatbot_mode=gr.CheckboxGroup(["General Advice", "Personal Advice"], label="How would you like the chatbot to respond?")
233
 
234
  gr.ChatInterface(
235
- fn=lambda msg, hist, topic_vals, mode_vals: respond(msg, hist, topic_vals, mode_vals),
236
  title="Finance Management Hub",
237
  description="Ask about your personal finance",
238
  type="messages",
 
139
 
140
  information=""
141
 
142
+ def respond(message, history, chatbot_topic_values, user_id=1):
143
  topic_chunks = []
144
  if chatbot_topic_values and "Helping Charities" in chatbot_topic_values:
145
  topic_chunks = get_top_chunks(message, charity_embeddings, cleaned_charities)
 
147
  topic_chunks = get_top_chunks(message, finance_embeddings, cleaned_finance)
148
 
149
  csv_advice = get_advice(user_id)
150
+ role_message = (
151
+ "You are a helpful and insightful chatbot who acts like a financial "
152
+ "advisor for a university student. "
153
+ "DO NOT ask the user for additional numbers or input. "
154
+ f"Use the following spending data from the CSV file to provide advice: {csv_advice}. "
155
+ f"Also consider this context from your knowledge base: {topic_chunks}"
156
+ )
157
+ #if chatbot_mode_values and "General Advice" in chatbot_mode_values:
158
+ # role_message = (
159
+ # "You are a helpful and insightful chatbot who acts like a financial "
160
+ # "advisor of a university student. Respond in under five bullet points, "
161
+ # f"under 500 characters, using this context: {topic_chunks}"
162
+ #)
163
+ #elif chatbot_mode_values and "Personal Advice" in chatbot_mode_values:
164
+ # role_message = (
165
+ # "You are a helpful and insightful chatbot who acts like a financial "
166
+ # "DO NOT ask the user for additional numbers or input"
167
+ # f"Use the following spending data from the CSV file to provide advice {csv_advice}"
168
+ #)
169
+ #else:
170
+ # role_message = f"You are a helpful chatbot. Use this context: {topic_chunks}"
171
+ messages = [{"role": "system", "content": role_message}]
172
  if history:
173
  messages.extend(history)
174
  messages.append({"role": "user", "content": message})
 
187
  #ChatInterface requires at least one parameter(a function)
188
  chatbot = gr.ChatInterface(respond,type="messages", title="Finance Management Hub", theme="Taithrah/Minimal")
189
 
190
+ #def save_chat_history(history, username):
191
+ # if not username:
192
+ # username = "anonymous"
193
 
194
+ # timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
195
+ # filename = f"chat_history_{username}_{timestamp}.txt"
196
 
197
+ # with open(filename, "w", encoding="utf-8") as f:
198
+ # f.write(f"Chat History for {username} - {timestamp}\n\n")
199
+ # for exchange in history:
200
+ # if isinstance(exchange, tuple) and len(exchange) == 2:
201
+ # user_msg, bot_msg = exchange
202
+ # f.write(f"User: {user_msg}\n")
203
+ # f.write(f"Bot: {bot_msg}\n\n")
204
+ # elif isinstance(exchange, dict):
205
  # Handle dictionary format if needed
206
+ # role = exchange.get("role", "unknown")
207
+ # content = exchange.get("content", "")
208
+ # f.write(f"{role.capitalize()}: {content}\n\n")
209
 
210
+ #return filename
211
 
212
  with gr.Blocks(
213
  theme=gr.themes.Soft(
 
238
  chatbot_mode=gr.CheckboxGroup(["General Advice", "Personal Advice"], label="How would you like the chatbot to respond?")
239
 
240
  gr.ChatInterface(
241
+ fn=lambda msg, hist, topic_vals: respond(msg, hist, topic_vals),
242
  title="Finance Management Hub",
243
  description="Ask about your personal finance",
244
  type="messages",