Spaces:
Sleeping
Sleeping
| 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 | |