louiecerv commited on
Commit
7404ad7
·
1 Parent(s): 58c5e3e
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -3,35 +3,48 @@ from PIL import Image
3
  import google.generativeai as genai
4
  import os
5
 
6
-
7
  MODEL_ID = "gemini-2.0-flash-exp"
8
  api_key = os.getenv("GEMINI_API_KEY")
 
 
9
 
10
  # Initialize chat history
11
  if "messages" not in st.session_state:
12
  st.session_state.messages = []
13
 
 
 
 
 
 
14
  # Function to reset chat history
15
  def reset_chat():
16
  st.session_state.messages = []
 
17
 
18
  # Streamlit app
19
  st.title("Gemini Image Chat")
20
 
21
- model_id=MODEL_ID
22
- genai.configure(api_key=api_key)
23
- model = genai.GenerativeModel(model_id)
24
- chat = model.start_chat()
25
-
26
- # File uploader
27
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
28
 
29
  if uploaded_file is not None:
30
- # Display the uploaded image
31
- image = Image.open(uploaded_file)
32
- st.image(image, caption="Uploaded Image.", use_container_width=True)
33
-
34
- # Reset chat history when a new image is uploaded
 
 
 
 
 
 
 
 
 
 
 
35
  reset_chat()
36
 
37
  # Text input for user prompt
@@ -48,14 +61,14 @@ if uploaded_file is not None:
48
  with st.chat_message(message["role"]):
49
  st.markdown(message["content"])
50
 
 
 
51
 
52
- img_file = genai.upload_file(uploaded_file, mime_type="image/jpeg")
53
-
54
- # Send image and prompt to Gemini API
55
  response = model.generate_content(
56
  [
57
  user_input,
58
- img_file
59
  ]
60
  )
61
 
 
3
  import google.generativeai as genai
4
  import os
5
 
 
6
  MODEL_ID = "gemini-2.0-flash-exp"
7
  api_key = os.getenv("GEMINI_API_KEY")
8
+ model_id = MODEL_ID
9
+ genai.configure(api_key=api_key)
10
 
11
  # Initialize chat history
12
  if "messages" not in st.session_state:
13
  st.session_state.messages = []
14
 
15
+ if "model" not in st.session_state:
16
+ st.session_state.model = genai.GenerativeModel(MODEL_ID)
17
+
18
+ model = st.session_state.model
19
+
20
  # Function to reset chat history
21
  def reset_chat():
22
  st.session_state.messages = []
23
+ model.start_chat()
24
 
25
  # Streamlit app
26
  st.title("Gemini Image Chat")
27
 
28
+ # File uploader with allowed types
29
+ uploaded_file = st.file_uploader("Choose an image or PDF...", type=["jpg", "jpeg", "png", "pdf"])
 
 
 
 
 
30
 
31
  if uploaded_file is not None:
32
+ # Determine file type
33
+ file_type = uploaded_file.type
34
+ if file_type.startswith('image'):
35
+ # Display the uploaded image
36
+ image = Image.open(uploaded_file)
37
+ st.image(image, caption="Uploaded Image.", use_container_width=True)
38
+ mime_type = "image/jpeg" # Use a consistent MIME type for images
39
+ elif file_type == 'application/pdf':
40
+ # Display a message for PDF upload
41
+ st.write("PDF file uploaded. You can ask questions about its content.")
42
+ mime_type = "application/pdf"
43
+ else:
44
+ st.error("Unsupported file type. Please upload an image or PDF.")
45
+ return
46
+
47
+ # Reset chat history when a new file is uploaded
48
  reset_chat()
49
 
50
  # Text input for user prompt
 
61
  with st.chat_message(message["role"]):
62
  st.markdown(message["content"])
63
 
64
+ # Upload the file with the correct MIME type
65
+ file_data = genai.upload_file(uploaded_file, mime_type=mime_type)
66
 
67
+ # Send file and prompt to Gemini API
 
 
68
  response = model.generate_content(
69
  [
70
  user_input,
71
+ file_data
72
  ]
73
  )
74