eagle0504 commited on
Commit
38e44d8
1 Parent(s): c76be3f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+
7
+ class ChatBot:
8
+ def __init__(self, image_base64, api_key):
9
+ self.image_base64 = image_base64
10
+ self.api_key = api_key
11
+ self.contents = [
12
+ {
13
+ "parts": [
14
+ {"text": "You are a helpful assistant."},
15
+ {"inline_data": {"mime_type": "image/jpeg", "data": image_base64}},
16
+ ]
17
+ }
18
+ ]
19
+
20
+ def generate_response(self, prompt: str) -> str:
21
+ self.contents.append(
22
+ {
23
+ "parts": [
24
+ {"text": prompt},
25
+ {"inline_data": {"mime_type": "image/jpeg", "data": self.image_base64}},
26
+ ]
27
+ }
28
+ )
29
+ headers = {
30
+ "Content-Type": "application/json",
31
+ }
32
+ data = {"contents": self.contents}
33
+ response = requests.post(
34
+ f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key={self.api_key}",
35
+ headers=headers,
36
+ json=data,
37
+ )
38
+ response_json = response.json()
39
+ response_text = response_json.get("contents", [{}])[0].get("parts", [{}])[0].get("text", "")
40
+ return response_text
41
+
42
+ def get_history(self) -> list:
43
+ return self.contents
44
+
45
+ # Streamlit app
46
+ st.title("Image Chatbot with Google's Gemini API")
47
+
48
+ st.sidebar.title("Upload Image or Take a Picture")
49
+ uploaded_file = st.sidebar.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
50
+
51
+ if uploaded_file is not None:
52
+ image = Image.open(uploaded_file)
53
+ st.sidebar.image(image, caption='Uploaded Image.', use_column_width=True)
54
+ buffered = io.BytesIO()
55
+ image.save(buffered, format="JPEG")
56
+ image_base64 = base64.b64encode(buffered.getvalue()).decode()
57
+
58
+ api_key = st.sidebar.text_input("Enter your API key", type="password")
59
+
60
+ if api_key:
61
+ chatbot = ChatBot(image_base64, api_key)
62
+
63
+ st.header("Chat with the Bot")
64
+
65
+ if 'conversation' not in st.session_state:
66
+ st.session_state.conversation = []
67
+
68
+ user_input = st.text_input("Ask a question about the image:")
69
+
70
+ if user_input:
71
+ response = chatbot.generate_response(user_input)
72
+ st.session_state.conversation.append({"user": user_input, "bot": response})
73
+
74
+ if st.session_state.conversation:
75
+ for chat in st.session_state.conversation:
76
+ st.write(f"**You:** {chat['user']}")
77
+ st.write(f"**Bot:** {chat['bot']}")
78
+
79
+ else:
80
+ st.sidebar.text("Please upload an image to start the conversation.")