|
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 |
|
|
|
|
|
load_dotenv() |
|
genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) |
|
|
|
|
|
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"}, |
|
] |
|
|
|
|
|
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) |
|
|
|
|
|
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 |
|
|
|
|
|
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 = [] |
|
|
|
|
|
def clear_chat_history(): |
|
st.session_state.chat = model.start_chat(history=[]) |
|
st.session_state.messages = [] |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.chat-container { |
|
height: 60vh; |
|
overflow-y: auto; |
|
padding: 10px; |
|
background-color: #f9f9f9; |
|
border-radius: 10px; |
|
} |
|
.user-message { |
|
background-color: #007bff; |
|
color: white; |
|
padding: 10px; |
|
border-radius: 10px; |
|
margin: 5px 0; |
|
max-width: 80%; |
|
align-self: flex-end; |
|
} |
|
.assistant-message { |
|
background-color: #e9ecef; |
|
color: black; |
|
padding: 10px; |
|
border-radius: 10px; |
|
margin: 5px 0; |
|
max-width: 80%; |
|
} |
|
.input-container { |
|
position: fixed; |
|
bottom: 0; |
|
width: 100%; |
|
padding: 10px; |
|
background-color: white; |
|
box-shadow: 0 -2px 5px rgba(0,0,0,0.1); |
|
} |
|
@media (max-width: 600px) { |
|
.chat-container { |
|
height: 70vh; |
|
} |
|
.stTextArea textarea { |
|
height: 80px !important; |
|
} |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.title("Mariam AI") |
|
|
|
|
|
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é.") |
|
|
|
|
|
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']) |
|
|
|
|
|
st.markdown('<div class="chat-container">', unsafe_allow_html=True) |
|
for message in st.session_state.messages: |
|
if message["role"] == "user": |
|
st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True) |
|
else: |
|
st.markdown(f'<div class="assistant-message">{message["content"]}</div>', unsafe_allow_html=True) |
|
st.markdown('</div>', unsafe_allow_html=True) |
|
|
|
|
|
components.html(""" |
|
<script> |
|
const chatContainer = document.querySelector('.chat-container'); |
|
chatContainer.scrollTop = chatContainer.scrollHeight; |
|
</script> |
|
""") |
|
|
|
|
|
st.markdown('<div class="input-container">', 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('</div>', unsafe_allow_html=True) |
|
|
|
|
|
if send_button and user_input: |
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
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é.") |
|
|
|
|
|
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." |
|
|
|
|
|
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}") |
|
|
|
|
|
if uploaded_file: |
|
shutil.rmtree("temp", ignore_errors=True) |
|
os.makedirs("temp", exist_ok=True) |
|
|
|
|
|
st.rerun() |