Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,7 +12,7 @@ logger = logging.getLogger(__name__)
|
|
12 |
device = torch.device("cpu")
|
13 |
logger.info(f"Using device: {device}")
|
14 |
|
15 |
-
# Response cache with
|
16 |
response_cache = {
|
17 |
"hi": "Hello! I'm your financial advisor. How can I help with investing?",
|
18 |
"hello": "Hello! I'm your financial advisor. How can I help with investing?",
|
@@ -47,6 +47,15 @@ response_cache = {
|
|
47 |
"what is dollar-cost averaging?": (
|
48 |
"Dollar-cost averaging is investing a fixed amount regularly (e.g., $100 monthly) in assets like ETFs, "
|
49 |
"reducing risk by spreading purchases over time."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
)
|
51 |
}
|
52 |
|
@@ -64,8 +73,15 @@ except Exception as e:
|
|
64 |
logger.error(f"Error loading model/tokenizer: {e}")
|
65 |
raise
|
66 |
|
67 |
-
# Pre-tokenize
|
68 |
-
prompt_prefix =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
prefix_tokens = tokenizer(prompt_prefix, return_tensors="pt", truncation=True, max_length=512).to(device)
|
70 |
|
71 |
# Define chat function
|
@@ -91,8 +107,8 @@ def chat_with_model(message, history=None):
|
|
91 |
with torch.no_grad():
|
92 |
outputs = model.generate(
|
93 |
**inputs,
|
94 |
-
max_new_tokens=
|
95 |
-
do_sample=False,
|
96 |
pad_token_id=tokenizer.eos_token_id
|
97 |
)
|
98 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
@@ -107,9 +123,10 @@ logger.info("Initializing Gradio interface")
|
|
107 |
interface = gr.ChatInterface(
|
108 |
fn=chat_with_model,
|
109 |
title="Financial Advisor Chatbot (OPT-350m)",
|
110 |
-
description="Ask about investing! Powered by Meta AI's OPT-350m. Fast,
|
111 |
examples=[
|
112 |
"Hi",
|
|
|
113 |
"Hi, pretend you are a financial advisor. Now tell me how can I start investing in stock market?",
|
114 |
"Do you have a list of companies you recommend?",
|
115 |
"What's the difference between stocks and bonds?",
|
|
|
12 |
device = torch.device("cpu")
|
13 |
logger.info(f"Using device: {device}")
|
14 |
|
15 |
+
# Response cache with consolidated and new entries
|
16 |
response_cache = {
|
17 |
"hi": "Hello! I'm your financial advisor. How can I help with investing?",
|
18 |
"hello": "Hello! I'm your financial advisor. How can I help with investing?",
|
|
|
47 |
"what is dollar-cost averaging?": (
|
48 |
"Dollar-cost averaging is investing a fixed amount regularly (e.g., $100 monthly) in assets like ETFs, "
|
49 |
"reducing risk by spreading purchases over time."
|
50 |
+
),
|
51 |
+
"give me few investing idea": (
|
52 |
+
"Here are some investing ideas:\n"
|
53 |
+
"1. Open a brokerage account if you are 18 or older (e.g., Fidelity, Vanguard).\n"
|
54 |
+
"2. Deposit an initial amount you can afford (e.g., $100).\n"
|
55 |
+
"3. Buy a researched stock, ETF (e.g., VOO), or index fund.\n"
|
56 |
+
"4. Check your investments regularly and enable dividend reinvesting if desired.\n"
|
57 |
+
"5. Use dollar-cost averaging to buy the same investment regularly (e.g., monthly).\n"
|
58 |
+
"Consult a financial planner for personalized advice."
|
59 |
)
|
60 |
}
|
61 |
|
|
|
73 |
logger.error(f"Error loading model/tokenizer: {e}")
|
74 |
raise
|
75 |
|
76 |
+
# Pre-tokenize prompt prefix with few-shot example
|
77 |
+
prompt_prefix = (
|
78 |
+
"You are a financial advisor. Provide concise, actionable advice in a numbered list when asked for ideas or steps. "
|
79 |
+
"If no specific recommendations, suggest alternatives. Avoid vague statements.\n\n"
|
80 |
+
"Example:\n"
|
81 |
+
"Q: Give me some investing tips\n"
|
82 |
+
"A: 1. Open a brokerage account (e.g., Fidelity).\n2. Start with $100 in an ETF like VOO.\n3. Research investments on Yahoo Finance.\n4. Invest regularly using dollar-cost averaging.\n\n"
|
83 |
+
"Q: "
|
84 |
+
)
|
85 |
prefix_tokens = tokenizer(prompt_prefix, return_tensors="pt", truncation=True, max_length=512).to(device)
|
86 |
|
87 |
# Define chat function
|
|
|
107 |
with torch.no_grad():
|
108 |
outputs = model.generate(
|
109 |
**inputs,
|
110 |
+
max_new_tokens=30, # Increased for detailed lists
|
111 |
+
do_sample=False, # Greedy decoding for speed
|
112 |
pad_token_id=tokenizer.eos_token_id
|
113 |
)
|
114 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
123 |
interface = gr.ChatInterface(
|
124 |
fn=chat_with_model,
|
125 |
title="Financial Advisor Chatbot (OPT-350m)",
|
126 |
+
description="Ask about investing! Powered by Meta AI's OPT-350m. Fast, detailed answers.",
|
127 |
examples=[
|
128 |
"Hi",
|
129 |
+
"Give me few investing idea",
|
130 |
"Hi, pretend you are a financial advisor. Now tell me how can I start investing in stock market?",
|
131 |
"Do you have a list of companies you recommend?",
|
132 |
"What's the difference between stocks and bonds?",
|