Spaces:
Runtime error
Runtime error
Update utils/smart_naming.py
Browse files- utils/smart_naming.py +70 -30
utils/smart_naming.py
CHANGED
|
@@ -2,40 +2,80 @@
|
|
| 2 |
|
| 3 |
from datetime import datetime
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def generate_chat_name(chat_history, claude_service):
|
| 7 |
-
"""Generate a smart name based on chat content"""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
for entry in chat_history:
|
| 12 |
if entry.get('analysis'):
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if summary:
|
| 32 |
-
# Clean up the summary
|
| 33 |
-
summary = summary.strip().strip('"').strip("'")
|
| 34 |
-
# Add timestamp for uniqueness
|
| 35 |
-
timestamp = datetime.now().strftime("%Y%m%d")
|
| 36 |
-
return f"{summary}_{timestamp}"
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from datetime import datetime
|
| 4 |
import streamlit as st
|
| 5 |
+
import time
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
+
def generate_chat_name(chat_history, claude_service, max_retries=3, retry_delay=2):
|
| 9 |
+
"""Generate a smart name based on chat content with retry logic"""
|
| 10 |
+
|
| 11 |
+
def extract_basic_name():
|
| 12 |
+
"""Extract basic name from content if API fails"""
|
| 13 |
for entry in chat_history:
|
| 14 |
if entry.get('analysis'):
|
| 15 |
+
# Look for patterns, tickers, or key terms
|
| 16 |
+
analysis = entry['analysis'].lower()
|
| 17 |
+
|
| 18 |
+
# Look for tickers (uppercase words 1-5 chars)
|
| 19 |
+
words = analysis.split()
|
| 20 |
+
tickers = [word.upper() for word in words
|
| 21 |
+
if word.isupper() and 1 <= len(word) <= 5]
|
| 22 |
+
|
| 23 |
+
# Look for common patterns
|
| 24 |
+
patterns = ["bullish", "bearish", "trend", "support", "resistance",
|
| 25 |
+
"breakout", "breakdown", "reversal"]
|
| 26 |
+
found_patterns = [p for p in patterns if p in analysis]
|
| 27 |
+
|
| 28 |
+
if tickers and found_patterns:
|
| 29 |
+
return f"{tickers[0]} {found_patterns[0].title()} Analysis"
|
| 30 |
+
elif tickers:
|
| 31 |
+
return f"{tickers[0]} Technical Analysis"
|
| 32 |
+
elif found_patterns:
|
| 33 |
+
return f"{found_patterns[0].title()} Pattern Analysis"
|
| 34 |
+
|
| 35 |
+
return None
|
| 36 |
|
| 37 |
+
# First try API with retries
|
| 38 |
+
for attempt in range(max_retries):
|
| 39 |
+
try:
|
| 40 |
+
# Combine all analyses into one text for summarization
|
| 41 |
+
full_text = ""
|
| 42 |
+
for entry in chat_history:
|
| 43 |
+
if entry.get('analysis'):
|
| 44 |
+
full_text += entry['analysis'] + "\n"
|
| 45 |
|
| 46 |
+
if not full_text:
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
prompt = """Based on this trading analysis, create a very short (3-6 words) title that captures the key focus.
|
| 50 |
+
Make it specific but concise. Include any relevant tickers or patterns mentioned.
|
| 51 |
+
Example formats:
|
| 52 |
+
- "AAPL Bullish Breakout Analysis"
|
| 53 |
+
- "SPY Multiple Timeframe Support"
|
| 54 |
+
- "Crypto Markets Correlation Study"
|
| 55 |
+
|
| 56 |
+
Analysis text:
|
| 57 |
+
{text}
|
| 58 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
summary = claude_service.generate_educational_content(prompt.format(text=full_text))
|
| 61 |
+
if summary:
|
| 62 |
+
# Clean up the summary
|
| 63 |
+
summary = summary.strip().strip('"').strip("'")
|
| 64 |
+
# Add timestamp for uniqueness
|
| 65 |
+
timestamp = datetime.now().strftime("%Y%m%d")
|
| 66 |
+
return f"{summary}_{timestamp}"
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
if attempt < max_retries - 1: # Don't sleep on last attempt
|
| 70 |
+
time.sleep(retry_delay)
|
| 71 |
+
continue
|
| 72 |
+
|
| 73 |
+
# If API fails, try basic name extraction
|
| 74 |
+
basic_name = extract_basic_name()
|
| 75 |
+
if basic_name:
|
| 76 |
+
timestamp = datetime.now().strftime("%Y%m%d")
|
| 77 |
+
return f"{basic_name}_{timestamp}"
|
| 78 |
|
| 79 |
+
# Final fallback
|
| 80 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 81 |
+
return f"Trading_Analysis_{timestamp}"
|