eaglelandsonce commited on
Commit
76988a0
Β·
verified Β·
1 Parent(s): ab9b19e

Rename pages/2_🏠_MoreTV.py to pages/2_🏠_ImageDescriptor.py

Browse files
pages/2_🏠_ImageDescriptor.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import base64
3
+ import anthropic
4
+
5
+ # Assuming anthropic is a package that provides an Anthropic client for interacting with Claude
6
+ # and it's installed or defined somewhere in your project
7
+ from anthropic import Anthropic
8
+
9
+ def image_to_base64(image):
10
+ """Convert the uploaded image to base64."""
11
+ return base64.b64encode(image.getvalue()).decode("utf-8")
12
+
13
+ def get_media_type(image_name):
14
+ """Get the media type of the uploaded image based on its file extension."""
15
+ if image_name.lower().endswith(".jpg") or image_name.lower().endswith(".jpeg"):
16
+ return "image/jpeg"
17
+ elif image_name.lower().endswith(".png"):
18
+ return "image/png"
19
+ else:
20
+ return None # Extend this function based on the image formats you expect to handle
21
+
22
+ def describe_image(image, api_key):
23
+ """Send the image to Claude for description."""
24
+ image_base64 = image_to_base64(image)
25
+ media_type = get_media_type(image.name)
26
+
27
+ client = Anthropic(api_key=api_key)
28
+ message = client.messages.create(
29
+ model="claude-3-opus-20240229",
30
+ max_tokens=1024,
31
+ messages=[
32
+ {
33
+ "role": "user",
34
+ "content": [
35
+ {
36
+ "type": "image",
37
+ "source": {
38
+ "type": "base64",
39
+ "media_type": media_type,
40
+ "data": image_base64,
41
+ },
42
+ },
43
+ {
44
+ "type": "text",
45
+ "text": "Describe this image."
46
+ }
47
+ ],
48
+ }
49
+ ],
50
+ )
51
+ return message.content
52
+
53
+ def main():
54
+ st.title("Image Description with Claude")
55
+
56
+ api_key = st.text_input("Enter your Claude API Key:", type="password")
57
+
58
+ uploaded_image = st.file_uploader("Drag and drop an image here", type=["jpg", "jpeg", "png"])
59
+
60
+ if st.button("Describe Image") and uploaded_image is not None:
61
+ if api_key:
62
+ description = describe_image(uploaded_image, api_key)
63
+ st.text("Description from Claude:")
64
+ st.write(description)
65
+ else:
66
+ st.error("Please enter a valid API key.")
67
+
68
+ if __name__ == "__main__":
69
+ main()
pages/2_🏠_MoreTV.py DELETED
@@ -1 +0,0 @@
1
- # coming