Spaces:
Sleeping
Sleeping
Create claude.py
Browse files
claude.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import anthropic
|
2 |
+
import os
|
3 |
+
import base64
|
4 |
+
import httpx
|
5 |
+
|
6 |
+
os.environ["ANTHROPIC_API_KEY"] = os.getenv("ANTHROPIC_API_KEY")
|
7 |
+
client = anthropic.Anthropic()
|
8 |
+
|
9 |
+
|
10 |
+
def create_claude_request_for_text_completion(message_schema):
|
11 |
+
message = client.messages.create(
|
12 |
+
model="claude-3-5-sonnet-20240620",
|
13 |
+
max_tokens=1000,
|
14 |
+
temperature=0,
|
15 |
+
messages=message_schema)
|
16 |
+
return message.content[0].text
|
17 |
+
|
18 |
+
|
19 |
+
def create_claude_image_request_for_image_captioning(system_prompt, caption_prompt, image_data):
|
20 |
+
try:
|
21 |
+
message = client.messages.create(
|
22 |
+
model="claude-3-5-sonnet-20240620",
|
23 |
+
max_tokens=1024,
|
24 |
+
messages=[
|
25 |
+
{
|
26 |
+
"role": "user",
|
27 |
+
"content": [
|
28 |
+
{
|
29 |
+
"type": "text",
|
30 |
+
"text": system_prompt
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"type": "image",
|
34 |
+
"source": {
|
35 |
+
"type": "base64",
|
36 |
+
"media_type": f"image/jpeg",
|
37 |
+
"data": image_data
|
38 |
+
}
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"type": "text",
|
42 |
+
"text": caption_prompt
|
43 |
+
}
|
44 |
+
]
|
45 |
+
}
|
46 |
+
]
|
47 |
+
)
|
48 |
+
except:
|
49 |
+
message = client.messages.create(
|
50 |
+
model="claude-3-5-sonnet-20240620",
|
51 |
+
max_tokens=1024,
|
52 |
+
messages=[
|
53 |
+
{
|
54 |
+
"role": "user",
|
55 |
+
"content": [
|
56 |
+
{
|
57 |
+
"type": "text",
|
58 |
+
"text": system_prompt
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"type": "image",
|
62 |
+
"source": {
|
63 |
+
"type": "base64",
|
64 |
+
"media_type": f"image/png",
|
65 |
+
"data": image_data
|
66 |
+
}
|
67 |
+
},
|
68 |
+
{
|
69 |
+
"type": "text",
|
70 |
+
"text": caption_prompt
|
71 |
+
}
|
72 |
+
]
|
73 |
+
}
|
74 |
+
]
|
75 |
+
)
|
76 |
+
return message.content[0].text
|
77 |
+
|
78 |
+
|
79 |
+
def embed_base64_for_claude(image_path):
|
80 |
+
# Open the image file in binary mode
|
81 |
+
with open(image_path, "rb") as image_file:
|
82 |
+
# Read the image and encode it to base64
|
83 |
+
image_data = base64.b64encode(image_file.read()).decode("utf-8")
|
84 |
+
return image_data
|
85 |
+
|
86 |
+
|
87 |
+
def extract_data_from_text_xml(text_xml, tag):
|
88 |
+
import re
|
89 |
+
re_command = f"<{tag}>(.*?)</{tag}>"
|
90 |
+
data = re.findall(re_command, text_xml, re.DOTALL)
|
91 |
+
final_string = ""
|
92 |
+
for idx, item in enumerate(data, start=1):
|
93 |
+
output_extraction = f"{idx}:\n{item.strip()}\n"
|
94 |
+
final_string = final_string + output_extraction
|
95 |
+
return final_string
|