Spaces:
Sleeping
Sleeping
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?",
|
|
@@ -83,7 +83,7 @@ except Exception as e:
|
|
| 83 |
logger.error(f"Error loading model/tokenizer: {e}")
|
| 84 |
raise
|
| 85 |
|
| 86 |
-
# Pre-tokenize prompt prefix with
|
| 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"
|
|
@@ -121,12 +121,12 @@ def chat_with_model(message, history=None):
|
|
| 121 |
with torch.no_grad():
|
| 122 |
outputs = model.generate(
|
| 123 |
**inputs,
|
| 124 |
-
max_new_tokens=100,
|
| 125 |
-
min_length=20,
|
| 126 |
-
do_sample=True,
|
| 127 |
-
temperature=0.7,
|
| 128 |
-
top_p=0.9,
|
| 129 |
-
no_repeat_ngram_size=2,
|
| 130 |
pad_token_id=tokenizer.eos_token_id
|
| 131 |
)
|
| 132 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
@@ -153,7 +153,12 @@ interface = gr.ChatInterface(
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
device = torch.device("cpu")
|
| 13 |
logger.info(f"Using device: {device}")
|
| 14 |
|
| 15 |
+
# Response cache with 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?",
|
|
|
|
| 83 |
logger.error(f"Error loading model/tokenizer: {e}")
|
| 84 |
raise
|
| 85 |
|
| 86 |
+
# Pre-tokenize prompt prefix with 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"
|
|
|
|
| 121 |
with torch.no_grad():
|
| 122 |
outputs = model.generate(
|
| 123 |
**inputs,
|
| 124 |
+
max_new_tokens=100,
|
| 125 |
+
min_length=20,
|
| 126 |
+
do_sample=True,
|
| 127 |
+
temperature=0.7,
|
| 128 |
+
top_p=0.9,
|
| 129 |
+
no_repeat_ngram_size=2,
|
| 130 |
pad_token_id=tokenizer.eos_token_id
|
| 131 |
)
|
| 132 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 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(share=False, debug=True)
|
| 160 |
+
except Exception as e:
|
| 161 |
+
logger.error(f"Error launching interface: {e}")
|
| 162 |
+
raise
|
| 163 |
+
else:
|
| 164 |
+
logger.info("Running in Hugging Face Spaces, interface defined but not launched")
|