AnilNiraula commited on
Commit
be2620c
·
verified ·
1 Parent(s): 7d2616f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -20
app.py CHANGED
@@ -1,10 +1,9 @@
1
  import logging
2
  import os
3
- import time # Added for timing logs
4
  import torch
5
  import gradio as gr
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
7
- import difflib
8
  import json
9
 
10
  # Set up logging
@@ -54,6 +53,46 @@ response_cache = {
54
  "Invest what you can afford after expenses and an emergency fund. Start with $100-$500 monthly "
55
  "in ETFs like VOO using dollar-cost averaging. Consult a financial planner."
56
  ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  }
58
 
59
  # Load persistent cache
@@ -88,7 +127,7 @@ prompt_prefix = (
88
  "Avoid vague or unrelated topics. Use a numbered list format where appropriate and explain each step.\n\n"
89
  "Example 1:\n"
90
  "Q: How can I start investing with $100 a month?\n"
91
- "A: Here’s a step-by point-by-step guide:\n"
92
  "1. Open a brokerage account with a platform like Fidelity or Robinhood. They offer low fees and no minimums.\n"
93
  "2. Deposit your $100 monthly. You can set up automatic transfers.\n"
94
  "3. Choose a low-cost ETF like VOO, which tracks the S&P 500.\n"
@@ -101,23 +140,20 @@ prompt_prefix = (
101
  "Q: "
102
  )
103
 
104
- # Fuzzy matching for cache
105
- def get_closest_cache_key(message, cache_keys, threshold=0.7):
106
- matches = difflib.get_close_matches(message, cache_keys, n=1, cutoff=threshold)
107
- return matches[0] if matches else None
108
-
109
- # Define chat function with optimized generation parameters
110
  def chat_with_model(user_input, history=None):
111
  try:
112
- start_time = time.time() # Start timing
113
  logger.info(f"Processing user input: {user_input}")
114
 
115
- cache_key = user_input.lower().strip()
116
- cache_keys = list(response_cache.keys())
117
- closest_key = cache_key if cache_key in response_cache else get_closest_cache_key(cache_key, cache_keys)
118
- if closest_key:
119
- logger.info(f"Cache hit for: {closest_key}")
120
- response = response_cache[closest_key]
 
 
121
  logger.info(f"Chatbot response: {response}")
122
  history = history or []
123
  history.append({"role": "user", "content": user_input})
@@ -141,12 +177,12 @@ def chat_with_model(user_input, history=None):
141
  inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512).to(device)
142
 
143
  with torch.inference_mode():
144
- gen_start_time = time.time() # Start generation timing
145
  outputs = model.generate(
146
  **inputs,
147
- max_new_tokens=75, # Reduced for faster generation
148
  min_length=20,
149
- do_sample=False, # Use greedy decoding for speed
150
  repetition_penalty=1.2,
151
  pad_token_id=tokenizer.eos_token_id
152
  )
@@ -157,7 +193,8 @@ def chat_with_model(user_input, history=None):
157
  response = response[len(full_prompt):].strip() if response.startswith(full_prompt) else response
158
  logger.info(f"Chatbot response: {response}")
159
 
160
- response_cache[cache_key] = response
 
161
  logger.info("Cache miss, added to in-memory cache")
162
 
163
  history = history or []
 
1
  import logging
2
  import os
3
+ import time
4
  import torch
5
  import gradio as gr
6
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
7
  import json
8
 
9
  # Set up logging
 
53
  "Invest what you can afford after expenses and an emergency fund. Start with $100-$500 monthly "
54
  "in ETFs like VOO using dollar-cost averaging. Consult a financial planner."
55
  ),
56
+ "how to start investing": (
57
+ "Here’s how to start investing:\n"
58
+ "1. Educate yourself using resources like Investopedia.\n"
59
+ "2. Open a brokerage account with a platform like Fidelity.\n"
60
+ "3. Deposit an initial amount, such as $100, after building an emergency fund.\n"
61
+ "4. Choose a low-cost ETF like VOO.\n"
62
+ "5. Invest regularly using dollar-cost averaging.\n"
63
+ "Consult a financial planner for personalized advice."
64
+ ),
65
+ "best brokerage accounts": (
66
+ "The best brokerage accounts for beginners include Fidelity, Vanguard, Charles Schwab, and Robinhood. "
67
+ "They offer low fees, no minimums, and user-friendly platforms."
68
+ ),
69
+ "investing for beginners": (
70
+ "Here’s a beginner’s guide to investing:\n"
71
+ "1. Learn the basics from Investopedia or books like 'The Intelligent Investor.'\n"
72
+ "2. Set clear investment goals and assess your risk tolerance.\n"
73
+ "3. Open a brokerage account with a platform like Fidelity or Robinhood.\n"
74
+ "4. Start with low-cost ETFs like VOO or index funds.\n"
75
+ "5. Invest regularly using dollar-cost averaging.\n"
76
+ "6. Monitor your investments quarterly.\n"
77
+ "Consult a financial planner for tailored advice."
78
+ ),
79
+ "steps to start investing": (
80
+ "Here are the steps to start investing:\n"
81
+ "1. Educate yourself on investing basics.\n"
82
+ "2. Open a brokerage account with a beginner-friendly platform.\n"
83
+ "3. Deposit an initial amount you can afford.\n"
84
+ "4. Choose a diversified investment like an ETF.\n"
85
+ "5. Invest consistently over time.\n"
86
+ "Consult a financial planner for more guidance."
87
+ ),
88
+ "recommended etfs": (
89
+ "Recommended ETFs for beginners include VOO (tracks S&P 500), QQQ (tech-focused), and VT (global market exposure). "
90
+ "They offer diversification and low fees."
91
+ ),
92
+ "how much to invest": (
93
+ "The amount to invest depends on your financial situation. Start with what you can afford after covering expenses and an emergency fund. "
94
+ "A common starting point is $100-$500 monthly in low-cost ETFs like VOO. Consult a financial planner for personalized advice."
95
+ ),
96
  }
97
 
98
  # Load persistent cache
 
127
  "Avoid vague or unrelated topics. Use a numbered list format where appropriate and explain each step.\n\n"
128
  "Example 1:\n"
129
  "Q: How can I start investing with $100 a month?\n"
130
+ "A: Here’s a step-by-step guide:\n"
131
  "1. Open a brokerage account with a platform like Fidelity or Robinhood. They offer low fees and no minimums.\n"
132
  "2. Deposit your $100 monthly. You can set up automatic transfers.\n"
133
  "3. Choose a low-cost ETF like VOO, which tracks the S&P 500.\n"
 
140
  "Q: "
141
  )
142
 
143
+ # Define chat function with substring matching and reduced max_new_tokens
 
 
 
 
 
144
  def chat_with_model(user_input, history=None):
145
  try:
146
+ start_time = time.time()
147
  logger.info(f"Processing user input: {user_input}")
148
 
149
+ user_input_lower = user_input.lower().strip()
150
+
151
+ # Substring matching for cache
152
+ matching_keys = [key for key in response_cache if key in user_input_lower]
153
+ if matching_keys:
154
+ longest_key = max(matching_keys, key=len)
155
+ logger.info(f"Cache hit for: {longest_key}")
156
+ response = response_cache[longest_key]
157
  logger.info(f"Chatbot response: {response}")
158
  history = history or []
159
  history.append({"role": "user", "content": user_input})
 
177
  inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512).to(device)
178
 
179
  with torch.inference_mode():
180
+ gen_start_time = time.time()
181
  outputs = model.generate(
182
  **inputs,
183
+ max_new_tokens=50, # Reduced for faster generation
184
  min_length=20,
185
+ do_sample=False, # Greedy decoding for speed
186
  repetition_penalty=1.2,
187
  pad_token_id=tokenizer.eos_token_id
188
  )
 
193
  response = response[len(full_prompt):].strip() if response.startswith(full_prompt) else response
194
  logger.info(f"Chatbot response: {response}")
195
 
196
+ # Update cache with exact user input as key
197
+ response_cache[user_input_lower] = response
198
  logger.info("Cache miss, added to in-memory cache")
199
 
200
  history = history or []