eagle0504 commited on
Commit
137e13e
1 Parent(s): 4a28b57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -35
app.py CHANGED
@@ -5,42 +5,27 @@ from PIL import Image
5
  import io
6
  import os
7
 
8
- class ChatBot:
9
- def __init__(self, image_base64, api_key):
10
- self.image_base64 = image_base64
11
- self.api_key = api_key
12
- self.contents = [
 
 
13
  {
14
  "parts": [
15
- {"text": "You are a helpful assistant."},
16
  {"inline_data": {"mime_type": "image/jpeg", "data": image_base64}},
17
  ]
18
  }
19
  ]
20
-
21
- def generate_response(self, prompt: str) -> str:
22
- self.contents.append(
23
- {
24
- "parts": [
25
- {"text": prompt},
26
- {"inline_data": {"mime_type": "image/jpeg", "data": self.image_base64}},
27
- ]
28
- }
29
- )
30
- headers = {
31
- "Content-Type": "application/json",
32
- }
33
- data = {"contents": self.contents}
34
- response = requests.post(
35
- f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key={self.api_key}",
36
- headers=headers,
37
- json=data,
38
- )
39
- response_json = response.json()
40
- return response_json
41
-
42
- def get_history(self) -> list:
43
- return self.contents
44
 
45
  # Streamlit app
46
  st.set_page_config(layout="wide")
@@ -58,8 +43,6 @@ if uploaded_file is not None:
58
  api_key = os.environ["GEMINI_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:
@@ -68,9 +51,8 @@ if uploaded_file is not None:
68
  user_input = st.text_input("Ask a question about the image:")
69
 
70
  if user_input:
71
- json_response = chatbot.generate_response(user_input)
72
- st.write(json_response)
73
- # response = json_response["candidates"][0]["content"]["parts"][0]["text"]
74
  st.session_state.conversation.append({"user": user_input, "bot": response})
75
 
76
  if st.session_state.conversation:
 
5
  import io
6
  import os
7
 
8
+ # Function to make an API call to Google's Gemini API
9
+ def call_gemini_api(image_base64, api_key, prompt="What is this picture?"):
10
+ headers = {
11
+ "Content-Type": "application/json",
12
+ }
13
+ data = {
14
+ "contents": [
15
  {
16
  "parts": [
17
+ {"text": prompt},
18
  {"inline_data": {"mime_type": "image/jpeg", "data": image_base64}},
19
  ]
20
  }
21
  ]
22
+ }
23
+ response = requests.post(
24
+ f"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent?key={api_key}",
25
+ headers=headers,
26
+ json=data,
27
+ )
28
+ return response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
  # Streamlit app
31
  st.set_page_config(layout="wide")
 
43
  api_key = os.environ["GEMINI_API_KEY"] # st.sidebar.text_input("Enter your API key", type="password")
44
 
45
  if api_key:
 
 
46
  st.header("Chat with the Bot")
47
 
48
  if 'conversation' not in st.session_state:
 
51
  user_input = st.text_input("Ask a question about the image:")
52
 
53
  if user_input:
54
+ json_response = call_gemini_api(image_base64, api_key, user_input)
55
+ response = json_response["candidates"][0]["content"]["parts"][0]["text"]
 
56
  st.session_state.conversation.append({"user": user_input, "bot": response})
57
 
58
  if st.session_state.conversation: