cryogenic22 commited on
Commit
a116cb9
·
verified ·
1 Parent(s): bce57b8

Create services/claude_service.py

Browse files
Files changed (1) hide show
  1. services/claude_service.py +115 -0
services/claude_service.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # src/services/claude_service.py
2
+
3
+ import base64
4
+ from anthropic import Anthropic
5
+ import streamlit as st
6
+ from config.settings import ANTHROPIC_API_KEY, CLAUDE_MODEL
7
+
8
+ class ClaudeService:
9
+ def __init__(self):
10
+ """Initialize Claude service with API key from HuggingFace secrets"""
11
+ if not ANTHROPIC_API_KEY:
12
+ raise ValueError("Anthropic API key not found in HuggingFace secrets. Please ensure ANTHROPIC_API_KEY is set in your space's secrets.")
13
+ self.client = Anthropic(api_key=ANTHROPIC_API_KEY)
14
+ self.model = CLAUDE_MODEL
15
+
16
+ def analyze_image(self, image_data, prompt):
17
+ """Analyze image using Claude Vision"""
18
+ try:
19
+ encoded_image = base64.b64encode(image_data).decode('utf-8')
20
+
21
+ message = self.client.messages.create(
22
+ model=self.model,
23
+ max_tokens=1000,
24
+ messages=[{
25
+ "role": "user",
26
+ "content": [
27
+ {
28
+ "type": "text",
29
+ "text": prompt
30
+ },
31
+ {
32
+ "type": "image",
33
+ "source": {
34
+ "type": "base64",
35
+ "media_type": "image/jpeg",
36
+ "data": encoded_image
37
+ }
38
+ }
39
+ ]
40
+ }]
41
+ )
42
+
43
+ return message.content[0].text
44
+ except Exception as e:
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?
51
+ Choose from: Candlestick, Line, OHLC, Area, or Other.
52
+ Just respond with one word."""
53
+
54
+ result = self.analyze_image(image_data, prompt)
55
+ return result.strip() if result else "Other"
56
+
57
+ def get_educational_content(self, concept):
58
+ """Get educational content about trading concepts"""
59
+ try:
60
+ message = self.client.messages.create(
61
+ model=self.model,
62
+ max_tokens=1000,
63
+ messages=[{
64
+ "role": "user",
65
+ "content": f"""Please explain the trading concept '{concept}'
66
+ in a clear, educational way. Include:
67
+ 1. Basic Definition
68
+ 2. How it Works
69
+ 3. Key Characteristics
70
+ 4. When to Look for It
71
+ 5. Trading Implications
72
+ 6. Common Mistakes to Avoid
73
+ 7. Real-World Example"""
74
+ }]
75
+ )
76
+
77
+ return message.content[0].text
78
+ except Exception as e:
79
+ st.error(f"Error getting educational content: {str(e)}")
80
+ return None
81
+
82
+ def continue_analysis(self, question, previous_analysis, image_data=None):
83
+ """Continue analysis based on follow-up question"""
84
+ try:
85
+ content = [
86
+ {
87
+ "type": "text",
88
+ "text": f"""Previous analysis: {previous_analysis}
89
+ User's follow-up question: {question}
90
+ Please provide a detailed answer to the follow-up question,
91
+ maintaining the context of the previous analysis."""
92
+ }
93
+ ]
94
+
95
+ if image_data:
96
+ encoded_image = base64.b64encode(image_data).decode('utf-8')
97
+ content.append({
98
+ "type": "image",
99
+ "source": {
100
+ "type": "base64",
101
+ "media_type": "image/jpeg",
102
+ "data": encoded_image
103
+ }
104
+ })
105
+
106
+ message = self.client.messages.create(
107
+ model=self.model,
108
+ max_tokens=1000,
109
+ messages=[{"role": "user", "content": content}]
110
+ )
111
+
112
+ return message.content[0].text
113
+ except Exception as e:
114
+ st.error(f"Error in follow-up analysis: {str(e)}")
115
+ return None