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('