|
import streamlit as st |
|
import google.generativeai as genai |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
tools = "google_search_retrieval" |
|
|
|
|
|
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"}, |
|
] |
|
|
|
model = genai.GenerativeModel('gemini-2.0-flash-exp',tools='code_execution' , |
|
safety_settings=safety_settings, 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") |
|
|
|
def role_to_streamlit(role): |
|
if role == "model": |
|
return "assistant" |
|
else: |
|
return role |
|
|
|
|
|
if "chat" not in st.session_state: |
|
st.session_state.chat = model.start_chat(history=[]) |
|
|
|
|
|
st.title("Mariam AI!") |
|
|
|
|
|
uploaded_file = st.file_uploader("Télécharger un fichier (image/document)", type=['jpg','mp4','mp3', 'jpeg', 'png', 'pdf', 'txt']) |
|
|
|
|
|
for message in st.session_state.chat.history: |
|
with st.chat_message(role_to_streamlit(message.role)): |
|
st.markdown(message.parts[0].text) |
|
|
|
|
|
def process_uploaded_file(file): |
|
if file is not None: |
|
|
|
with open(os.path.join("temp", file.name), "wb") as f: |
|
f.write(file.getbuffer()) |
|
|
|
|
|
try: |
|
gemini_file = genai.upload_file(os.path.join("temp", file.name)) |
|
return gemini_file |
|
except Exception as e: |
|
st.error(f"Erreur lors du téléchargement du fichier : {e}") |
|
return None |
|
|
|
|
|
if prompt := st.chat_input("Hey?"): |
|
|
|
uploaded_gemini_file = None |
|
if uploaded_file: |
|
uploaded_gemini_file = process_uploaded_file(uploaded_file) |
|
|
|
|
|
st.chat_message("user").markdown(prompt) |
|
|
|
|
|
try: |
|
if uploaded_gemini_file: |
|
|
|
response = st.session_state.chat.send_message([uploaded_gemini_file, "\n\n", prompt]) |
|
else: |
|
|
|
print(prompt) |
|
print("---------") |
|
response = st.session_state.chat.send_message(prompt) |
|
print(response.text) |
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(response.text) |
|
|
|
except Exception as e: |
|
st.error(f"Erreur lors de l'envoi du message : {e}") |
|
|
|
|
|
os.makedirs("temp", exist_ok=True) |