File size: 2,781 Bytes
76988a0
 
e93f15d
76988a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e93f15d
76988a0
 
e93f15d
 
 
76988a0
e93f15d
 
 
 
76988a0
e93f15d
 
76988a0
e93f15d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76988a0
 
 
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
import streamlit as st
import base64
from anthropic import Anthropic  # Assuming anthropic is a package for interacting with Claude

def image_to_base64(image):
    """Convert the uploaded image to base64."""
    return base64.b64encode(image.getvalue()).decode("utf-8")

def get_media_type(image_name):
    """Get the media type of the uploaded image based on its file extension."""
    if image_name.lower().endswith(".jpg") or image_name.lower().endswith(".jpeg"):
        return "image/jpeg"
    elif image_name.lower().endswith(".png"):
        return "image/png"
    else:
        return None  # Extend this function based on the image formats you expect to handle

def describe_image(image, api_key):
    """Send the image to Claude for description."""
    image_base64 = image_to_base64(image)
    media_type = get_media_type(image.name)

    client = Anthropic(api_key=api_key)
    message = client.messages.create(
        model="claude-3-opus-20240229",
        max_tokens=1024,
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "media_type": media_type,
                            "data": image_base64,
                        },
                    },
                    {
                        "type": "text",
                        "text": "Describe this image."
                    }
                ],
            }
        ],
    )
    return message.content

def main():
    st.title("Image Description with Claude 3 Opus")
    
    api_key = st.text_input("Enter your Claude API Key:", type="password")

    st.header("Upload Images")
    col1, col2 = st.columns(2)
    
    with col1:
        view_image = st.file_uploader("Upload View Image", type=["jpg", "jpeg", "png"], key="view")
    with col2:
        buffer_image = st.file_uploader("Upload Buffer Image", type=["jpg", "jpeg", "png"], key="buffer")
    
    if st.button("Describe Images"):
        if not api_key:
            st.error("Please enter a valid API key.")
            return
        
        if view_image is not None:
            view_description = describe_image(view_image, api_key)
            st.text("View Image Description from Claude:")
            st.write(view_description)
        else:
            st.error("Please upload a View image.")

        if buffer_image is not None:
            buffer_description = describe_image(buffer_image, api_key)
            st.text("Buffer Image Description from Claude:")
            st.write(buffer_description)
        else:
            st.error("Please upload a Buffer image.")

if __name__ == "__main__":
    main()