import streamlit as st import google.generativeai as genai import os from dotenv import load_dotenv import http.client import json import shutil import streamlit.components.v1 as components # Charger les variables d'environnement load_dotenv() genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) # Paramètres de sécurité safety_settings = [ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"}, {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"}, ] # Instructions système (inchangées pour brièveté) ss = """ # Prompt System pour Mariam, IA conçu par youssouf [...] """ model = genai.GenerativeModel('gemini-2.0-flash-exp', tools='code_execution', safety_settings=safety_settings, system_instruction=ss) # Fonctions de recherche web (inchangées) def perform_web_search(query): conn = http.client.HTTPSConnection("google.serper.dev") payload = json.dumps({"q": query}) headers = {'X-API-KEY': '9b90a274d9e704ff5b21c0367f9ae1161779b573', 'Content-Type': 'application/json'} try: conn.request("POST", "/search", payload, headers) res = conn.getresponse() data = json.loads(res.read().decode("utf-8")) return data except Exception as e: st.error(f"Erreur lors de la recherche web : {e}") return None finally: conn.close() def format_search_results(data): if not data: return "Aucun résultat trouvé" result = "" if 'knowledgeGraph' in data: kg = data['knowledgeGraph'] result += f"### {kg.get('title', '')}\n*{kg.get('type', '')}*\n\n{kg.get('description', '')}\n\n" if 'organic' in data: result += "### Résultats principaux:\n" for item in data['organic'][:3]: result += f"- **{item['title']}**\n {item['snippet']}\n [Lien]({item['link']})\n\n" return result # Initialisation de l’état if "chat" not in st.session_state: st.session_state.chat = model.start_chat(history=[]) if "web_search" not in st.session_state: st.session_state.web_search = False if "messages" not in st.session_state: st.session_state.messages = [] # Fonction pour effacer l’historique def clear_chat_history(): st.session_state.chat = model.start_chat(history=[]) st.session_state.messages = [] # CSS personnalisé pour mobile st.markdown(""" """, unsafe_allow_html=True) # Titre st.title("Mariam AI") # Section des paramètres (expansible) with st.expander("Paramètres"): st.session_state.web_search = st.toggle("Activer la recherche web", value=st.session_state.web_search, help="Permet des réponses enrichies avec des données du web.") if st.button("Effacer l’historique"): clear_chat_history() st.success("Historique effacé.") # Section de téléchargement with st.expander("Télécharger un fichier"): st.info("Types acceptés : jpg, jpeg, png, pdf, txt") uploaded_file = st.file_uploader("Choisir un fichier", type=['jpg', 'jpeg', 'png', 'pdf', 'txt']) # Zone de conversation st.markdown('
', unsafe_allow_html=True) for message in st.session_state.messages: if message["role"] == "user": st.markdown(f'
{message["content"]}
', unsafe_allow_html=True) else: st.markdown(f'
{message["content"]}
', unsafe_allow_html=True) st.markdown('
', unsafe_allow_html=True) # JavaScript pour défiler automatiquement components.html(""" """) # Zone de saisie st.markdown('
', unsafe_allow_html=True) user_input = st.text_area("Votre message", height=80, key="input") col1, col2 = st.columns([3, 1]) with col1: pass with col2: send_button = st.button("Envoyer") st.markdown('
', unsafe_allow_html=True) # Traitement de l’entrée utilisateur if send_button and user_input: st.session_state.messages.append({"role": "user", "content": user_input}) # Traitement du fichier uploaded_gemini_file = None if uploaded_file: file_ext = uploaded_file.name.split('.')[-1].lower() if file_ext in ['jpg', 'jpeg', 'png', 'pdf', 'txt']: with open(os.path.join("temp", uploaded_file.name), "wb") as f: f.write(uploaded_file.getbuffer()) try: uploaded_gemini_file = genai.upload_file(os.path.join("temp", uploaded_file.name)) except Exception as e: st.error(f"Erreur lors de l’upload : {e}") else: st.error("Type de fichier non accepté.") # Recherche web web_results = None if st.session_state.web_search: with st.spinner("Recherche web en cours..."): web_results = perform_web_search(user_input) if web_results: formatted_results = format_search_results(web_results) user_input = f"{user_input}\n\nRésultats de recherche :\n{formatted_results}\nAnalyse ces informations et réponds-moi." # Réponse de Mariam try: if uploaded_gemini_file: response = st.session_state.chat.send_message([uploaded_gemini_file, "\n\n", user_input]) else: response = st.session_state.chat.send_message(user_input) st.session_state.messages.append({"role": "assistant", "content": response.text}) except Exception as e: st.error(f"Erreur : {e}") # Nettoyage if uploaded_file: shutil.rmtree("temp", ignore_errors=True) os.makedirs("temp", exist_ok=True) # Rafraîchir la page pour afficher la réponse st.rerun()