Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,15 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
| 5 |
|
|
|
|
| 6 |
load_dotenv()
|
| 7 |
-
# Configure the API key
|
| 8 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
safety_settings = [
|
| 12 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 13 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
|
@@ -15,45 +18,56 @@ safety_settings = [
|
|
| 15 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 16 |
]
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
model = genai.GenerativeModel('gemini-1.5-flash',safety_settings=safety_settings,
|
| 22 |
-
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")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Function to get response from the model
|
| 27 |
-
# Gemini uses 'model' for assistant; Streamlit uses 'assistant'
|
| 28 |
-
|
| 29 |
-
|
| 30 |
def role_to_streamlit(role):
|
| 31 |
-
if role == "model"
|
| 32 |
-
return "assistant"
|
| 33 |
-
else:
|
| 34 |
-
return role
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
if "chat" not in st.session_state:
|
| 39 |
st.session_state.chat = model.start_chat(history=[])
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
st.
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
for message in st.session_state.chat.history:
|
| 46 |
with st.chat_message(role_to_streamlit(message.role)):
|
| 47 |
st.markdown(message.parts[0].text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
if prompt := st.chat_input("Hey?"):
|
| 51 |
-
# Display user's last message
|
| 52 |
-
st.chat_message("user").markdown(prompt)
|
| 53 |
-
|
| 54 |
-
# Send user entry to Gemini and read the response
|
| 55 |
-
response = st.session_state.chat.send_message(prompt)
|
| 56 |
-
|
| 57 |
-
# Display last
|
| 58 |
with st.chat_message("assistant"):
|
| 59 |
st.markdown(response.text)
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
+
# Charger les variables d'environnement
|
| 8 |
load_dotenv()
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Configurer la clé API
|
| 11 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 12 |
|
| 13 |
+
# Paramètres de sécurité
|
| 14 |
safety_settings = [
|
| 15 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 16 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
|
|
|
| 18 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 19 |
]
|
| 20 |
|
| 21 |
+
# Initialiser le modèle
|
| 22 |
+
model = genai.GenerativeModel('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 |
+
# Fonction pour convertir le rôle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def role_to_streamlit(role):
|
| 28 |
+
return "assistant" if role == "model" else role
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Configuration de la page Streamlit
|
| 31 |
+
st.set_page_config(page_title="Mariam - Assistant IA", page_icon="🤖")
|
| 32 |
+
st.title("Mariam AI - Chat Intelligent")
|
| 33 |
|
| 34 |
+
# Initialiser l'historique de chat si pas déjà présent
|
| 35 |
if "chat" not in st.session_state:
|
| 36 |
st.session_state.chat = model.start_chat(history=[])
|
| 37 |
|
| 38 |
+
# Gestion de l'upload d'images
|
| 39 |
+
uploaded_file = st.file_uploader("Télécharger une image (optionnel)",
|
| 40 |
+
type=["jpg", "jpeg", "png", "gif", "bmp"])
|
| 41 |
|
| 42 |
+
# Afficher l'historique des messages
|
| 43 |
for message in st.session_state.chat.history:
|
| 44 |
with st.chat_message(role_to_streamlit(message.role)):
|
| 45 |
st.markdown(message.parts[0].text)
|
| 46 |
+
# Afficher l'image si présente dans l'historique
|
| 47 |
+
if len(message.parts) > 1 and hasattr(message.parts[1], 'image'):
|
| 48 |
+
st.image(message.parts[1].image)
|
| 49 |
+
|
| 50 |
+
# Zone de saisie du chat
|
| 51 |
+
if prompt := st.chat_input("Que puis-je faire pour vous ?"):
|
| 52 |
+
# Préparer le message
|
| 53 |
+
if uploaded_file is not None:
|
| 54 |
+
# Convertir le fichier téléchargé en image
|
| 55 |
+
image = Image.open(uploaded_file)
|
| 56 |
+
|
| 57 |
+
# Envoyer le message avec l'image
|
| 58 |
+
response = st.session_state.chat.send_message([
|
| 59 |
+
prompt,
|
| 60 |
+
image
|
| 61 |
+
])
|
| 62 |
+
|
| 63 |
+
# Afficher l'image téléchargée
|
| 64 |
+
st.chat_message("user").markdown(prompt)
|
| 65 |
+
st.chat_message("user").image(image)
|
| 66 |
+
else:
|
| 67 |
+
# Envoyer le message texte seul
|
| 68 |
+
response = st.session_state.chat.send_message(prompt)
|
| 69 |
+
st.chat_message("user").markdown(prompt)
|
| 70 |
|
| 71 |
+
# Afficher la réponse de l'assistant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
with st.chat_message("assistant"):
|
| 73 |
st.markdown(response.text)
|