Update app.py
Browse files
app.py
CHANGED
|
@@ -3,9 +3,11 @@ import google.generativeai as genai
|
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
import mimetypes
|
| 7 |
|
| 8 |
load_dotenv()
|
|
|
|
| 9 |
# Configure the API key
|
| 10 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 11 |
|
|
@@ -16,15 +18,11 @@ safety_settings = [
|
|
| 16 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 17 |
]
|
| 18 |
|
| 19 |
-
genai.
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Function to get response from the model
|
| 27 |
-
# Gemini uses 'model' for assistant; Streamlit uses 'assistant'
|
| 28 |
|
| 29 |
def role_to_streamlit(role):
|
| 30 |
if role == "model":
|
|
@@ -42,63 +40,52 @@ st.title("Mariam AI!")
|
|
| 42 |
# Display chat messages from history above current input box
|
| 43 |
for message in st.session_state.chat.history:
|
| 44 |
with st.chat_message(role_to_streamlit(message.role)):
|
| 45 |
-
# Check if the message part is text or a file part
|
| 46 |
for part in message.parts:
|
| 47 |
-
if part.
|
| 48 |
st.markdown(part.text)
|
| 49 |
-
elif part.
|
| 50 |
-
# Handle file display (e.g., image)
|
| 51 |
try:
|
| 52 |
# Infer MIME type if not provided
|
| 53 |
-
|
|
|
|
| 54 |
mime_type = mimetypes.guess_type(part.file_data.file_name)[0]
|
| 55 |
-
|
| 56 |
-
mime_type = part.file_data.mime_type
|
| 57 |
if mime_type and mime_type.startswith("image/"):
|
| 58 |
-
|
| 59 |
-
image = Image.open(io.BytesIO(image_data)) # Open the image using PIL
|
| 60 |
st.image(image)
|
| 61 |
else:
|
| 62 |
-
st.write(f"File: {part.file_data.file_name} (MIME type: {
|
| 63 |
except Exception as e:
|
| 64 |
st.error(f"Error displaying file: {e}")
|
| 65 |
|
| 66 |
-
# Accept user's next message
|
| 67 |
if prompt := st.chat_input("Hey?"):
|
| 68 |
-
|
| 69 |
-
st.chat_message("user").markdown(prompt)
|
| 70 |
-
|
| 71 |
-
# Handle file uploads
|
| 72 |
-
uploaded_file = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png", "pdf"]) # Add more type if needed
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
bytes_data = uploaded_file.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
# Display the uploaded
|
| 79 |
if uploaded_file.type.startswith("image/"):
|
| 80 |
image = Image.open(uploaded_file)
|
| 81 |
-
st.
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
{
|
| 87 |
-
"file_data": {
|
| 88 |
-
"mime_type": uploaded_file.type,
|
| 89 |
-
"file_name": uploaded_file.name,
|
| 90 |
-
"data": bytes_data
|
| 91 |
-
}
|
| 92 |
-
}
|
| 93 |
-
]
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
else:
|
| 99 |
-
# Send user entry to Gemini and read the response
|
| 100 |
-
response = st.session_state.chat.send_message(prompt)
|
| 101 |
|
| 102 |
-
# Display
|
| 103 |
with st.chat_message("assistant"):
|
| 104 |
st.markdown(response.text)
|
|
|
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
import mimetypes
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
+
|
| 11 |
# Configure the API key
|
| 12 |
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
|
|
|
|
| 18 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 19 |
]
|
| 20 |
|
| 21 |
+
model = genai.GenerativeModel(
|
| 22 |
+
'gemini-1.5-flash',
|
| 23 |
+
safety_settings=safety_settings,
|
| 24 |
+
system_instruction="Tu es un assistant intelligent. ton but est d'assister au mieux que tu peux. tu as été créé par Aenir et tu t'appelles Mariam"
|
| 25 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
def role_to_streamlit(role):
|
| 28 |
if role == "model":
|
|
|
|
| 40 |
# Display chat messages from history above current input box
|
| 41 |
for message in st.session_state.chat.history:
|
| 42 |
with st.chat_message(role_to_streamlit(message.role)):
|
|
|
|
| 43 |
for part in message.parts:
|
| 44 |
+
if part.text: # Check for text content
|
| 45 |
st.markdown(part.text)
|
| 46 |
+
elif part.file_data: # Check for file data
|
|
|
|
| 47 |
try:
|
| 48 |
# Infer MIME type if not provided
|
| 49 |
+
mime_type = part.file_data.mime_type
|
| 50 |
+
if not mime_type:
|
| 51 |
mime_type = mimetypes.guess_type(part.file_data.file_name)[0]
|
| 52 |
+
|
|
|
|
| 53 |
if mime_type and mime_type.startswith("image/"):
|
| 54 |
+
image = Image.open(io.BytesIO(part.file_data.data))
|
|
|
|
| 55 |
st.image(image)
|
| 56 |
else:
|
| 57 |
+
st.write(f"File: {part.file_data.file_name} (MIME type: {mime_type})")
|
| 58 |
except Exception as e:
|
| 59 |
st.error(f"Error displaying file: {e}")
|
| 60 |
|
| 61 |
+
# Accept user's next message and file uploads
|
| 62 |
if prompt := st.chat_input("Hey?"):
|
| 63 |
+
uploaded_file = st.file_uploader("Choose a file", type=["jpg", "jpeg", "png", "pdf"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
parts = [prompt]
|
| 66 |
+
if uploaded_file:
|
| 67 |
bytes_data = uploaded_file.getvalue()
|
| 68 |
+
parts.append({
|
| 69 |
+
"file_data": {
|
| 70 |
+
"mime_type": uploaded_file.type,
|
| 71 |
+
"file_name": uploaded_file.name,
|
| 72 |
+
"data": bytes_data
|
| 73 |
+
}
|
| 74 |
+
})
|
| 75 |
|
| 76 |
+
# Display the uploaded image
|
| 77 |
if uploaded_file.type.startswith("image/"):
|
| 78 |
image = Image.open(uploaded_file)
|
| 79 |
+
with st.chat_message("user"):
|
| 80 |
+
st.image(image, caption=f"Uploaded Image: {uploaded_file.name}")
|
| 81 |
|
| 82 |
+
# Display user's message
|
| 83 |
+
with st.chat_message("user"):
|
| 84 |
+
st.markdown(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# Send message to Gemini
|
| 87 |
+
response = st.session_state.chat.send_message(parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
# Display Gemini's response
|
| 90 |
with st.chat_message("assistant"):
|
| 91 |
st.markdown(response.text)
|