Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,11 +12,21 @@ 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?",
|
19 |
"hey": "Hi there! Ready to discuss investment goals?",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
"hi, pretend you are a financial advisor. now tell me how can i start investing in stock market?": (
|
21 |
"Here’s a guide to start investing in the stock market:\n"
|
22 |
"1. **Learn**: Use Investopedia or 'The Intelligent Investor' by Benjamin Graham.\n"
|
@@ -73,13 +83,17 @@ except Exception as e:
|
|
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
|
79 |
-
"
|
80 |
"Example:\n"
|
81 |
-
"Q: Give me
|
82 |
-
"A: 1. Open a brokerage account
|
|
|
|
|
|
|
|
|
83 |
"Q: "
|
84 |
)
|
85 |
prefix_tokens = tokenizer(prompt_prefix, return_tensors="pt", truncation=True, max_length=512).to(device)
|
@@ -107,8 +121,12 @@ def chat_with_model(message, history=None):
|
|
107 |
with torch.no_grad():
|
108 |
outputs = model.generate(
|
109 |
**inputs,
|
110 |
-
max_new_tokens=
|
111 |
-
|
|
|
|
|
|
|
|
|
112 |
pad_token_id=tokenizer.eos_token_id
|
113 |
)
|
114 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
@@ -126,8 +144,8 @@ interface = gr.ChatInterface(
|
|
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?",
|
133 |
"How much should I invest?"
|
@@ -135,12 +153,7 @@ interface = gr.ChatInterface(
|
|
135 |
)
|
136 |
|
137 |
# Launch interface (conditional for Spaces)
|
138 |
-
if __name__ == "__main__
|
139 |
logger.info("Launching Gradio interface locally")
|
140 |
try:
|
141 |
-
interface.launch(
|
142 |
-
except Exception as e:
|
143 |
-
logger.error(f"Error launching interface: {e}")
|
144 |
-
raise
|
145 |
-
else:
|
146 |
-
logger.info("Running in Hugging Face Spaces, interface defined but not launched")
|
|
|
12 |
device = torch.device("cpu")
|
13 |
logger.info(f"Using device: {device}")
|
14 |
|
15 |
+
# Response cache with new entry for step-by-step advice
|
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?",
|
19 |
"hey": "Hi there! Ready to discuss investment goals?",
|
20 |
+
"hi, give me step-by-step investing advice": (
|
21 |
+
"Here’s a step-by-step guide to start investing:\n"
|
22 |
+
"1. **Open a Brokerage Account**: If you’re 18 or older, sign up with a platform like Fidelity, Vanguard, or Robinhood.\n"
|
23 |
+
"2. **Deposit Funds**: Add an initial amount you can afford, such as $100, after securing an emergency fund.\n"
|
24 |
+
"3. **Research and Buy**: Choose a stock, ETF (e.g., VOO for S&P 500), or index fund based on research from Yahoo Finance or Morningstar.\n"
|
25 |
+
"4. **Monitor Investments**: Check your portfolio regularly and enable dividend reinvesting for compounding returns.\n"
|
26 |
+
"5. **Use Dollar-Cost Averaging**: Invest a fixed amount (e.g., $100 monthly) consistently to reduce market timing risks.\n"
|
27 |
+
"6. **Diversify**: Spread investments across sectors to manage risk.\n"
|
28 |
+
"Consult a certified financial planner for personalized advice."
|
29 |
+
),
|
30 |
"hi, pretend you are a financial advisor. now tell me how can i start investing in stock market?": (
|
31 |
"Here’s a guide to start investing in the stock market:\n"
|
32 |
"1. **Learn**: Use Investopedia or 'The Intelligent Investor' by Benjamin Graham.\n"
|
|
|
83 |
logger.error(f"Error loading model/tokenizer: {e}")
|
84 |
raise
|
85 |
|
86 |
+
# Pre-tokenize prompt prefix with improved few-shot example
|
87 |
prompt_prefix = (
|
88 |
+
"You are a financial advisor. Provide concise, actionable advice in a numbered list for step-by-step or idea prompts. "
|
89 |
+
"Avoid repetition and vague statements. Use varied, specific steps.\n\n"
|
90 |
"Example:\n"
|
91 |
+
"Q: Give me step-by-step investing advice\n"
|
92 |
+
"A: 1. Open a brokerage account with Fidelity or Vanguard if 18 or older.\n"
|
93 |
+
"2. Deposit an affordable amount, like $100, after building an emergency fund.\n"
|
94 |
+
"3. Research and buy an ETF like VOO using Yahoo Finance data.\n"
|
95 |
+
"4. Check investments monthly and enable dividend reinvesting.\n"
|
96 |
+
"5. Invest regularly with dollar-cost averaging to reduce risk.\n\n"
|
97 |
"Q: "
|
98 |
)
|
99 |
prefix_tokens = tokenizer(prompt_prefix, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
|
121 |
with torch.no_grad():
|
122 |
outputs = model.generate(
|
123 |
**inputs,
|
124 |
+
max_new_tokens=100, # Increased for detailed lists
|
125 |
+
min_length=20, # Encourage substantive responses
|
126 |
+
do_sample=True, # Sampling to avoid repetition
|
127 |
+
temperature=0.7, # Balanced diversity
|
128 |
+
top_p=0.9, # Nucleus sampling
|
129 |
+
no_repeat_ngram_size=2, # Prevent repetitive phrases
|
130 |
pad_token_id=tokenizer.eos_token_id
|
131 |
)
|
132 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
144 |
description="Ask about investing! Powered by Meta AI's OPT-350m. Fast, detailed answers.",
|
145 |
examples=[
|
146 |
"Hi",
|
147 |
+
"Hi, give me step-by-step investing advice",
|
148 |
"Give me few investing idea",
|
|
|
149 |
"Do you have a list of companies you recommend?",
|
150 |
"What's the difference between stocks and bonds?",
|
151 |
"How much should I invest?"
|
|
|
153 |
)
|
154 |
|
155 |
# Launch interface (conditional for Spaces)
|
156 |
+
if __name__ == "__main__ and not os.getenv("HF_SPACE"):
|
157 |
logger.info("Launching Gradio interface locally")
|
158 |
try:
|
159 |
+
interface.launch(
|
|
|
|
|
|
|
|
|
|