Spaces:
Sleeping
Sleeping
| from PIL import Image | |
| import os | |
| import anthropic | |
| import base64 | |
| from dotenv import load_dotenv | |
| import cv2 | |
| from mimetypes import MimeTypes | |
| load_dotenv() | |
| client = anthropic.Anthropic(api_key= os.getenv('ANTHROPIC_API_KEY')) | |
| prompt = """Given 2 construction blueprints your task is to analyze carefully both blueprints and point out difference for following categories in markdown format - | |
| 1. Strcutural grid. | |
| 2. Layout Areas - rooms , balcony , porch , staircase , elevator etc. | |
| 3. Interior changes or optimization. | |
| Summarize all the difference in paragraph concisely in Markdown format . | |
| """ | |
| def encode_image(image_path): | |
| with open(image_path, "rb") as image_file: | |
| return base64.b64encode(image_file.read()).decode("utf-8") | |
| # return base64.b64encode(image_path.getvalue()).decode("utf-8") | |
| def chat_claude(prompt , image1 , image2 ) : | |
| # print("image 1" , image1) | |
| image1_data = encode_image(image1) | |
| # print("image 1 data" , image1_data) | |
| image2_data = encode_image(image2) | |
| mime = MimeTypes() | |
| image1_media_type =mime.guess_type(image1)[0] | |
| image2_media_type =mime.guess_type(image2)[0] | |
| message = client.messages.create( | |
| model="claude-3-opus-20240229", | |
| max_tokens = 4096, | |
| messages=[ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "text", | |
| "text": "Image 1:" | |
| }, | |
| { | |
| "type": "image", | |
| "source": { | |
| "type": "base64", | |
| # "media_type": "image/jpeg", | |
| "media_type": image1_media_type, | |
| "data": image1_data, | |
| }, | |
| }, | |
| { | |
| "type": "text", | |
| "text": "Image 2:" | |
| }, | |
| { | |
| "type": "image", | |
| "source": { | |
| "type": "base64", | |
| # "media_type": "image/jpeg", | |
| "media_type": image2_media_type, | |
| "data": image2_data, | |
| }, | |
| }, | |
| { | |
| "type": "text", | |
| "text": f"{prompt}" | |
| } | |
| ], | |
| } | |
| ], | |
| ) | |
| return message.content[0].text | |