Spaces:
Runtime error
Runtime error
Update services/claude_service.py
Browse files- services/claude_service.py +31 -0
services/claude_service.py
CHANGED
|
@@ -45,6 +45,37 @@ class ClaudeService:
|
|
| 45 |
st.error(f"Error in image analysis: {str(e)}")
|
| 46 |
return None
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def detect_chart_type(self, image_data):
|
| 49 |
"""Detect chart type from image"""
|
| 50 |
prompt = """What type of financial chart is this?
|
|
|
|
| 45 |
st.error(f"Error in image analysis: {str(e)}")
|
| 46 |
return None
|
| 47 |
|
| 48 |
+
def analyze_multiple_images(self, image_data_list, prompt):
|
| 49 |
+
"""Analyze multiple images together using Claude Vision"""
|
| 50 |
+
try:
|
| 51 |
+
# Create content list with prompt and all images
|
| 52 |
+
content = [{"type": "text", "text": prompt}]
|
| 53 |
+
|
| 54 |
+
# Add all images to the content
|
| 55 |
+
for idx, image_data in enumerate(image_data_list):
|
| 56 |
+
encoded_image = base64.b64encode(image_data).decode('utf-8')
|
| 57 |
+
content.append({
|
| 58 |
+
"type": "image",
|
| 59 |
+
"source": {
|
| 60 |
+
"type": "base64",
|
| 61 |
+
"media_type": "image/jpeg",
|
| 62 |
+
"data": encoded_image
|
| 63 |
+
}
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
message = self.client.messages.create(
|
| 67 |
+
model=self.model,
|
| 68 |
+
max_tokens=1500, # Increased for multiple image analysis
|
| 69 |
+
messages=[{
|
| 70 |
+
"role": "user",
|
| 71 |
+
"content": content
|
| 72 |
+
}]
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
return message.content[0].text
|
| 76 |
+
except Exception as e:
|
| 77 |
+
st.error(f"Error in multiple image analysis: {str(e)}")
|
| 78 |
+
return None
|
| 79 |
def detect_chart_type(self, image_data):
|
| 80 |
"""Detect chart type from image"""
|
| 81 |
prompt = """What type of financial chart is this?
|