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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -52
app.py CHANGED
@@ -3,7 +3,6 @@ from PIL import Image
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)
@@ -25,55 +24,12 @@ if 'chat_history' not in st.session_state:
25
  st.title("Gemini Chatbot")
26
 
27
  # Display the chat history
28
- for idx, (role, text) in enumerate(st.session_state['chat_history']):
29
- st.markdown(f"**{role.title()}:** {text}")
 
30
 
31
- # Function to handle sending messages
32
- def send_message():
33
- user_input = st.session_state.user_input
34
- if user_input:
35
- # Save user input to the chat history
36
- st.session_state['chat_history'].append(("User", user_input))
37
-
38
- # Assuming the generative model accepts text input
39
- prompts = [{"role": "user", "parts": [{"text": user_input}]}]
40
-
41
- # Use the appropriate Gemini model based on the inputs
42
- model_name = 'gemini-pro-vision' if uploaded_files else 'gemini-pro'
43
- model = genai.GenerativeModel(
44
- model_name=model_name,
45
- generation_config=generation_config,
46
- safety_settings=safety_settings
47
- )
48
-
49
- # Generate the response
50
- response = model.generate_content(prompts)
51
- response_text = response.text if hasattr(response, "text") else "No response text found."
52
-
53
- # Save the model response to the chat history
54
- st.session_state['chat_history'].append(("Gemini", response_text))
55
-
56
- # Process and save uploaded images to the chat history
57
- if uploaded_files:
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
-
64
- # Assuming the generative model accepts image input
65
- image_prompt = {"role": "user", "parts": [{"mime_type": uploaded_file.type, "data": image_base64}]}
66
- response = model.generate_content([image_prompt])
67
- response_text = response.text if hasattr(response, "text") else "No response text found."
68
-
69
- # Save the model response to the chat history
70
- st.session_state['chat_history'].append(("Gemini", response_text))
71
-
72
- # Clear the input box after sending the message
73
- st.session_state.user_input = ""
74
-
75
- # Text input for the user to send messages
76
- user_input = st.text_input("Enter your message here:", key="user_input", on_change=send_message)
77
 
78
  # File uploader for images
79
  uploaded_files = st.file_uploader(
@@ -90,6 +46,50 @@ def get_image_base64(image):
90
  img_str = base64.b64encode(buffered.getvalue()).decode()
91
  return img_str
92
 
93
- # Button to send the message
94
- # The button now doesn't need to do anything since the text input's on_change callback handles the logic
95
- st.button("Send", key="send_button", on_click=send_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
 
24
  st.title("Gemini Chatbot")
25
 
26
  # Display the chat history
27
+ for message in st.session_state['chat_history']:
28
+ role, text = message
29
+ st.markdown(f"**{role.title()}**: {text}")
30
 
31
+ # Multiline text input for the user to send messages
32
+ user_input = st.text_area("Enter your message here:", key="user_input")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  # File uploader for images
35
  uploaded_files = st.file_uploader(
 
46
  img_str = base64.b64encode(buffered.getvalue()).decode()
47
  return img_str
48
 
49
+ # When the 'Send' button is clicked, process the input and generate a response
50
+ if st.button("Send", key="send_button"):
51
+ if user_input or uploaded_files:
52
+ # Save user input to the chat history
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']:
94
+ role, text = message
95
+ st.markdown(f"**{role.title()}**: {text}")