Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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,
|
| 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 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 186 |
-
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
# Handle dictionary format if needed
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 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
|
| 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",
|