Spaces:
Runtime error
Runtime error
Update services/chart_analysis.py
Browse files- services/chart_analysis.py +36 -10
services/chart_analysis.py
CHANGED
|
@@ -9,7 +9,7 @@ class ChartAnalysisService:
|
|
| 9 |
def __init__(self, claude_service):
|
| 10 |
self.claude_service = claude_service
|
| 11 |
|
| 12 |
-
def analyze_multiple_charts(self, image_data_list, patterns, indicators, comparison_type="Individual Analysis"):
|
| 13 |
"""Analyze multiple charts and provide comprehensive analysis"""
|
| 14 |
try:
|
| 15 |
results = []
|
|
@@ -20,7 +20,13 @@ class ChartAnalysisService:
|
|
| 20 |
chart_type = self.claude_service.detect_chart_type(image_data)
|
| 21 |
st.info(f"Chart {idx + 1} detected as: {chart_type}")
|
| 22 |
|
| 23 |
-
analysis = self.analyze_single_chart(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if analysis:
|
| 25 |
individual_analyses.append(analysis)
|
| 26 |
|
|
@@ -28,7 +34,8 @@ class ChartAnalysisService:
|
|
| 28 |
if len(individual_analyses) > 1 and comparison_type != "Individual Analysis":
|
| 29 |
comparison_prompt = self.create_comparison_prompt(
|
| 30 |
individual_analyses,
|
| 31 |
-
comparison_type
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# Combine all images for the comparison analysis
|
|
@@ -52,10 +59,10 @@ class ChartAnalysisService:
|
|
| 52 |
st.error(f"Error in multiple chart analysis: {str(e)}")
|
| 53 |
return None
|
| 54 |
|
| 55 |
-
def analyze_single_chart(self, image_data, patterns, indicators, chart_type=None):
|
| 56 |
"""Analyze a single chart"""
|
| 57 |
try:
|
| 58 |
-
prompt = create_analysis_prompt(patterns, indicators)
|
| 59 |
analysis = self.claude_service.analyze_image(image_data, prompt)
|
| 60 |
|
| 61 |
if analysis:
|
|
@@ -91,10 +98,28 @@ class ChartAnalysisService:
|
|
| 91 |
st.error(f"Error handling follow-up question: {str(e)}")
|
| 92 |
return None
|
| 93 |
|
| 94 |
-
def create_comparison_prompt(self, individual_analyses, comparison_type):
|
| 95 |
"""Create a prompt for comparing multiple charts"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if comparison_type == "Correlated Analysis":
|
| 97 |
-
prompt = """Analyze these charts together and provide insights on their relationships
|
|
|
|
| 98 |
|
| 99 |
1. Correlation Analysis
|
| 100 |
- Identify any price correlations
|
|
@@ -113,8 +138,10 @@ class ChartAnalysisService:
|
|
| 113 |
|
| 114 |
4. Summary
|
| 115 |
Provide a clear summary of the key relationships and trading implications."""
|
|
|
|
| 116 |
else: # Market Trend Analysis
|
| 117 |
-
prompt = """Analyze these charts to identify broader market patterns
|
|
|
|
| 118 |
|
| 119 |
1. Market Trend Analysis
|
| 120 |
- Identify common trend directions
|
|
@@ -138,5 +165,4 @@ class ChartAnalysisService:
|
|
| 138 |
|
| 139 |
# Add disclaimer
|
| 140 |
prompt += "\n\nIMPORTANT: Clearly mark this as AI-generated analysis for informational purposes only."
|
| 141 |
-
return prompt
|
| 142 |
-
|
|
|
|
| 9 |
def __init__(self, claude_service):
|
| 10 |
self.claude_service = claude_service
|
| 11 |
|
| 12 |
+
def analyze_multiple_charts(self, image_data_list, patterns, indicators, comparison_type="Individual Analysis", expertise_level="Novice"):
|
| 13 |
"""Analyze multiple charts and provide comprehensive analysis"""
|
| 14 |
try:
|
| 15 |
results = []
|
|
|
|
| 20 |
chart_type = self.claude_service.detect_chart_type(image_data)
|
| 21 |
st.info(f"Chart {idx + 1} detected as: {chart_type}")
|
| 22 |
|
| 23 |
+
analysis = self.analyze_single_chart(
|
| 24 |
+
image_data,
|
| 25 |
+
patterns,
|
| 26 |
+
indicators,
|
| 27 |
+
chart_type,
|
| 28 |
+
expertise_level
|
| 29 |
+
)
|
| 30 |
if analysis:
|
| 31 |
individual_analyses.append(analysis)
|
| 32 |
|
|
|
|
| 34 |
if len(individual_analyses) > 1 and comparison_type != "Individual Analysis":
|
| 35 |
comparison_prompt = self.create_comparison_prompt(
|
| 36 |
individual_analyses,
|
| 37 |
+
comparison_type,
|
| 38 |
+
expertise_level
|
| 39 |
)
|
| 40 |
|
| 41 |
# Combine all images for the comparison analysis
|
|
|
|
| 59 |
st.error(f"Error in multiple chart analysis: {str(e)}")
|
| 60 |
return None
|
| 61 |
|
| 62 |
+
def analyze_single_chart(self, image_data, patterns, indicators, chart_type=None, expertise_level="Novice"):
|
| 63 |
"""Analyze a single chart"""
|
| 64 |
try:
|
| 65 |
+
prompt = create_analysis_prompt(patterns, indicators, expertise_level)
|
| 66 |
analysis = self.claude_service.analyze_image(image_data, prompt)
|
| 67 |
|
| 68 |
if analysis:
|
|
|
|
| 98 |
st.error(f"Error handling follow-up question: {str(e)}")
|
| 99 |
return None
|
| 100 |
|
| 101 |
+
def create_comparison_prompt(self, individual_analyses, comparison_type, expertise_level="Novice"):
|
| 102 |
"""Create a prompt for comparing multiple charts"""
|
| 103 |
+
expertise_adjustments = {
|
| 104 |
+
"Novice": {
|
| 105 |
+
"depth": "Focus on basic relationships and clear patterns.",
|
| 106 |
+
"language": "Use simple terms and explain technical concepts."
|
| 107 |
+
},
|
| 108 |
+
"Intermediate": {
|
| 109 |
+
"depth": "Include both basic and advanced correlations.",
|
| 110 |
+
"language": "Balance technical terms with clear explanations."
|
| 111 |
+
},
|
| 112 |
+
"Expert": {
|
| 113 |
+
"depth": "Provide sophisticated multi-chart analysis.",
|
| 114 |
+
"language": "Use full technical terminology and advanced concepts."
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
adj = expertise_adjustments[expertise_level]
|
| 119 |
+
|
| 120 |
if comparison_type == "Correlated Analysis":
|
| 121 |
+
prompt = f"""Analyze these charts together and provide insights on their relationships.
|
| 122 |
+
{adj['depth']} {adj['language']}
|
| 123 |
|
| 124 |
1. Correlation Analysis
|
| 125 |
- Identify any price correlations
|
|
|
|
| 138 |
|
| 139 |
4. Summary
|
| 140 |
Provide a clear summary of the key relationships and trading implications."""
|
| 141 |
+
|
| 142 |
else: # Market Trend Analysis
|
| 143 |
+
prompt = f"""Analyze these charts to identify broader market patterns.
|
| 144 |
+
{adj['depth']} {adj['language']}
|
| 145 |
|
| 146 |
1. Market Trend Analysis
|
| 147 |
- Identify common trend directions
|
|
|
|
| 165 |
|
| 166 |
# Add disclaimer
|
| 167 |
prompt += "\n\nIMPORTANT: Clearly mark this as AI-generated analysis for informational purposes only."
|
| 168 |
+
return prompt
|
|
|