Spaces:
Runtime error
Runtime error
| # utils/smart_naming.py | |
| from datetime import datetime | |
| import streamlit as st | |
| import time | |
| import json | |
| def generate_chat_name(chat_history, claude_service, max_retries=3, retry_delay=2): | |
| """Generate a smart name based on chat content with retry logic""" | |
| def extract_basic_name(): | |
| """Extract basic name from content if API fails""" | |
| for entry in chat_history: | |
| if entry.get('analysis'): | |
| # Look for patterns, tickers, or key terms | |
| analysis = entry['analysis'].lower() | |
| # Look for tickers (uppercase words 1-5 chars) | |
| words = analysis.split() | |
| tickers = [word.upper() for word in words | |
| if word.isupper() and 1 <= len(word) <= 5] | |
| # Look for common patterns | |
| patterns = ["bullish", "bearish", "trend", "support", "resistance", | |
| "breakout", "breakdown", "reversal"] | |
| found_patterns = [p for p in patterns if p in analysis] | |
| if tickers and found_patterns: | |
| return f"{tickers[0]} {found_patterns[0].title()} Analysis" | |
| elif tickers: | |
| return f"{tickers[0]} Technical Analysis" | |
| elif found_patterns: | |
| return f"{found_patterns[0].title()} Pattern Analysis" | |
| return None | |
| # First try API with retries | |
| for attempt in range(max_retries): | |
| try: | |
| # Combine all analyses into one text for summarization | |
| full_text = "" | |
| for entry in chat_history: | |
| if entry.get('analysis'): | |
| full_text += entry['analysis'] + "\n" | |
| if not full_text: | |
| return None | |
| prompt = """Based on this trading analysis, create a very short (3-6 words) title that captures the key focus. | |
| Make it specific but concise. Include any relevant tickers or patterns mentioned. | |
| Example formats: | |
| - "AAPL Bullish Breakout Analysis" | |
| - "SPY Multiple Timeframe Support" | |
| - "Crypto Markets Correlation Study" | |
| Analysis text: | |
| {text} | |
| """ | |
| summary = claude_service.generate_educational_content(prompt.format(text=full_text)) | |
| if summary: | |
| # Clean up the summary | |
| summary = summary.strip().strip('"').strip("'") | |
| # Add timestamp for uniqueness | |
| timestamp = datetime.now().strftime("%Y%m%d") | |
| return f"{summary}_{timestamp}" | |
| except Exception as e: | |
| if attempt < max_retries - 1: # Don't sleep on last attempt | |
| time.sleep(retry_delay) | |
| continue | |
| # If API fails, try basic name extraction | |
| basic_name = extract_basic_name() | |
| if basic_name: | |
| timestamp = datetime.now().strftime("%Y%m%d") | |
| return f"{basic_name}_{timestamp}" | |
| # Final fallback | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| return f"Trading_Analysis_{timestamp}" |