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