ziyadsuper2017 commited on
Commit
c72bfe4
·
1 Parent(s): a76b0fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -3,6 +3,7 @@ from PIL import Image
3
  import io
4
  import base64
5
 
 
6
  import google.generativeai as genai
7
 
8
  # Configure the API key (should be set as an environment variable or secure storage in production)
@@ -53,41 +54,38 @@ if st.button("Send", key="send_button"):
53
  if user_input:
54
  st.session_state['chat_history'].append(("User", user_input))
55
 
56
- if uploaded_files:
57
- images_prompts = []
58
- for uploaded_file in uploaded_files:
59
- bytes_data = uploaded_file.read()
60
- image = Image.open(io.BytesIO(bytes_data))
61
- image_base64 = get_image_base64(image)
62
- st.session_state['chat_history'].append(("User", f"Uploaded image: {uploaded_file.name}"))
63
- image_prompt = {"role": "user", "parts": [{"mime_type": uploaded_file.type, "data": image_base64}]}
64
- images_prompts.append(image_prompt)
65
-
66
- # Use the Gemini model for vision
67
- model_name = 'gemini-pro-vision'
68
- model = genai.GenerativeModel(
69
- model_name=model_name,
70
- generation_config=generation_config,
71
- safety_settings=safety_settings
72
- )
73
- response = model.generate_content(images_prompts)
74
- response_text = response.text if hasattr(response, "text") else "No response text found."
75
- st.session_state['chat_history'].append(("Gemini", response_text))
76
- elif user_input:
77
- # Use the Gemini model for text
78
- model_name = 'gemini-pro'
79
  model = genai.GenerativeModel(
80
- model_name=model_name,
81
  generation_config=generation_config,
82
  safety_settings=safety_settings
83
  )
84
- prompts = [{"role": "user", "parts": [{"text": user_input}]}]
85
  response = model.generate_content(prompts)
86
  response_text = response.text if hasattr(response, "text") else "No response text found."
87
  st.session_state['chat_history'].append(("Gemini", response_text))
88
 
89
- # Clear the input box after sending the message
90
- st.session_state['user_input'] = ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
  # Re-display the chat history to include the new messages
93
  for message in st.session_state['chat_history']:
 
3
  import io
4
  import base64
5
 
6
+ # Assuming google.generativeai as genai is the correct import based on your description
7
  import google.generativeai as genai
8
 
9
  # Configure the API key (should be set as an environment variable or secure storage in production)
 
54
  if user_input:
55
  st.session_state['chat_history'].append(("User", user_input))
56
 
57
+ # Process text input
58
+ if user_input and not uploaded_files:
59
+ prompts = [{"role": "user", "parts": [{"text": user_input}]}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  model = genai.GenerativeModel(
61
+ model_name='gemini-pro',
62
  generation_config=generation_config,
63
  safety_settings=safety_settings
64
  )
 
65
  response = model.generate_content(prompts)
66
  response_text = response.text if hasattr(response, "text") else "No response text found."
67
  st.session_state['chat_history'].append(("Gemini", response_text))
68
 
69
+ # Process and save uploaded images to the chat history
70
+ if uploaded_files:
71
+ for uploaded_file in uploaded_files:
72
+ bytes_data = uploaded_file.read()
73
+ image = Image.open(io.BytesIO(bytes_data))
74
+ image_base64 = get_image_base64(image)
75
+ st.session_state['chat_history'].append(("User", f"Uploaded image: {uploaded_file.name}"))
76
+
77
+ image_prompt = {"role": "user", "parts": [{"mime_type": uploaded_file.type, "data": image_base64}]}
78
+ model = genai.GenerativeModel(
79
+ model_name='gemini-pro-vision',
80
+ generation_config=generation_config,
81
+ safety_settings=safety_settings
82
+ )
83
+ response = model.generate_content([image_prompt])
84
+ response_text = response.text if hasattr(response, "text") else "No response text found."
85
+ st.session_state['chat_history'].append(("Gemini", response_text))
86
+
87
+ # Clear the input box after sending the message
88
+ st.session_state['user_input'] = ""
89
 
90
  # Re-display the chat history to include the new messages
91
  for message in st.session_state['chat_history']: