Spaces:
Runtime error
Runtime error
Create services/chart_analysis.py
Browse files- services/chart_analysis.py +67 -0
services/chart_analysis.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# src/services/chart_analysis.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from utils.file_handlers import save_chat_history
|
| 6 |
+
from utils.prompts import create_analysis_prompt
|
| 7 |
+
|
| 8 |
+
class ChartAnalysisService:
|
| 9 |
+
def __init__(self, claude_service):
|
| 10 |
+
self.claude_service = claude_service
|
| 11 |
+
|
| 12 |
+
def analyze_chart(self, image_data, patterns, indicators):
|
| 13 |
+
"""Analyze chart and return results"""
|
| 14 |
+
try:
|
| 15 |
+
# First detect chart type
|
| 16 |
+
chart_type = self.claude_service.detect_chart_type(image_data)
|
| 17 |
+
st.info(f"Detected chart type: {chart_type}")
|
| 18 |
+
|
| 19 |
+
# Generate analysis prompt
|
| 20 |
+
prompt = create_analysis_prompt(patterns, indicators)
|
| 21 |
+
|
| 22 |
+
# Get analysis from Claude
|
| 23 |
+
analysis = self.claude_service.analyze_image(image_data, prompt)
|
| 24 |
+
|
| 25 |
+
if analysis:
|
| 26 |
+
return {
|
| 27 |
+
'timestamp': datetime.now().isoformat(),
|
| 28 |
+
'chart_type': chart_type,
|
| 29 |
+
'analysis': analysis
|
| 30 |
+
}
|
| 31 |
+
return None
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"Error in chart analysis: {str(e)}")
|
| 34 |
+
return None
|
| 35 |
+
|
| 36 |
+
def handle_follow_up_question(self, question, previous_analysis, image_data):
|
| 37 |
+
"""Handle follow-up questions about the analysis"""
|
| 38 |
+
try:
|
| 39 |
+
response = self.claude_service.continue_analysis(
|
| 40 |
+
question,
|
| 41 |
+
previous_analysis,
|
| 42 |
+
image_data
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if response:
|
| 46 |
+
return {
|
| 47 |
+
'timestamp': datetime.now().isoformat(),
|
| 48 |
+
'question': question,
|
| 49 |
+
'analysis': response
|
| 50 |
+
}
|
| 51 |
+
return None
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error(f"Error handling follow-up question: {str(e)}")
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
+
def extract_stock_info(self, analysis_text):
|
| 57 |
+
"""Extract stock name and other metadata from analysis"""
|
| 58 |
+
stock_name = "Unknown"
|
| 59 |
+
try:
|
| 60 |
+
if "analyzing" in analysis_text.lower():
|
| 61 |
+
words = analysis_text.split()
|
| 62 |
+
for i, word in enumerate(words):
|
| 63 |
+
if word.lower() == "analyzing":
|
| 64 |
+
stock_name = words[i + 1].strip("(),.:")
|
| 65 |
+
except:
|
| 66 |
+
pass
|
| 67 |
+
return stock_name
|