Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,18 @@
|
|
1 |
-
import
|
2 |
import google.generativeai as genai
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Configuration de l'API Gemini
|
6 |
-
|
|
|
|
|
7 |
safety_settings = [
|
8 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
9 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
@@ -15,51 +24,107 @@ safety_settings = [
|
|
15 |
model = genai.GenerativeModel("gemini-2.0-flash-exp", safety_settings=safety_settings)
|
16 |
|
17 |
# Chargement de tous les fichiers PDF du dossier "data"
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
27 |
"""Traite le texte et l'image optionnelle et génère une réponse."""
|
28 |
try:
|
29 |
# Préparer le contenu en ajoutant tous les fichiers PDF
|
30 |
content = uploaded_files.copy()
|
31 |
|
32 |
# Si une image est fournie, l'ajouter au contenu
|
33 |
-
if
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
content.append(img_file)
|
|
|
|
|
36 |
|
37 |
# Ajout du prompt avec l'instruction de répondre en français
|
38 |
content.append("\n\n")
|
39 |
content.append(prompt + " répond en français.")
|
40 |
|
41 |
# Génération du contenu
|
42 |
-
|
43 |
-
|
44 |
return result.text
|
45 |
except Exception as e:
|
46 |
-
return f"Une erreur s'est produite :
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
#
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
import os
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Configuration de la page Streamlit
|
8 |
+
st.set_page_config(page_title="Mariam eco", layout="wide")
|
9 |
+
st.title("Mariam eco")
|
10 |
+
st.markdown("Triché !!!! On va encore triché haannnn")
|
11 |
|
12 |
# Configuration de l'API Gemini
|
13 |
+
os.environ["GEMINI_API_KEY"] = api_key
|
14 |
+
|
15 |
+
genai.configure(api_key=api_key)
|
16 |
safety_settings = [
|
17 |
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
18 |
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
|
|
24 |
model = genai.GenerativeModel("gemini-2.0-flash-exp", safety_settings=safety_settings)
|
25 |
|
26 |
# Chargement de tous les fichiers PDF du dossier "data"
|
27 |
+
@st.cache_resource
|
28 |
+
def load_pdf_files():
|
29 |
+
DATA_FOLDER = "data"
|
30 |
+
if not os.path.exists(DATA_FOLDER):
|
31 |
+
os.makedirs(DATA_FOLDER)
|
32 |
+
st.warning(f"Le dossier {DATA_FOLDER} a été créé. Veuillez y placer vos fichiers PDF.")
|
33 |
+
return []
|
34 |
+
|
35 |
+
pdf_files = [
|
36 |
+
os.path.join(DATA_FOLDER, file)
|
37 |
+
for file in os.listdir(DATA_FOLDER)
|
38 |
+
if file.lower().endswith(".pdf")
|
39 |
+
]
|
40 |
+
|
41 |
+
if not pdf_files:
|
42 |
+
st.info(f"Aucun fichier PDF trouvé dans le dossier {DATA_FOLDER}.")
|
43 |
+
return []
|
44 |
+
|
45 |
+
with st.spinner("Chargement des fichiers PDF..."):
|
46 |
+
uploaded_files = [genai.upload_file(pdf_file) for pdf_file in pdf_files]
|
47 |
+
st.success(f"{len(uploaded_files)} fichiers PDF chargés avec succès.")
|
48 |
+
|
49 |
+
return uploaded_files
|
50 |
|
51 |
+
uploaded_files = load_pdf_files()
|
52 |
+
|
53 |
+
def process_input(prompt, image_file):
|
54 |
"""Traite le texte et l'image optionnelle et génère une réponse."""
|
55 |
try:
|
56 |
# Préparer le contenu en ajoutant tous les fichiers PDF
|
57 |
content = uploaded_files.copy()
|
58 |
|
59 |
# Si une image est fournie, l'ajouter au contenu
|
60 |
+
if image_file is not None:
|
61 |
+
# Streamlit fournit un fichier en mémoire, nous devons le sauvegarder temporairement
|
62 |
+
temp_image_path = "temp_image.jpg"
|
63 |
+
img = Image.open(image_file)
|
64 |
+
img.save(temp_image_path)
|
65 |
+
img_file = genai.upload_file(temp_image_path)
|
66 |
content.append(img_file)
|
67 |
+
# Supprimer le fichier temporaire
|
68 |
+
os.remove(temp_image_path)
|
69 |
|
70 |
# Ajout du prompt avec l'instruction de répondre en français
|
71 |
content.append("\n\n")
|
72 |
content.append(prompt + " répond en français.")
|
73 |
|
74 |
# Génération du contenu
|
75 |
+
with st.spinner("Génération de la réponse..."):
|
76 |
+
result = model.generate_content(content)
|
77 |
return result.text
|
78 |
except Exception as e:
|
79 |
+
return f"Une erreur s'est produite : "
|
80 |
|
81 |
+
# Interface principale
|
82 |
+
tab1, tab2 = st.tabs(["Interface principale", "Exemples"])
|
83 |
+
|
84 |
+
with tab1:
|
85 |
+
st.subheader("Posez une question")
|
86 |
+
|
87 |
+
# Zone de texte pour la question
|
88 |
+
prompt = st.text_area("Question", placeholder="Posez une question sur le roman...", height=100)
|
89 |
+
|
90 |
+
# Upload d'image (facultatif)
|
91 |
+
image_file = st.file_uploader("Image (facultative)", type=["jpg", "jpeg", "png"])
|
92 |
+
|
93 |
+
# Affichage de l'image téléchargée
|
94 |
+
if image_file is not None:
|
95 |
+
st.image(image_file, caption="Image téléchargée", use_column_width=True)
|
96 |
+
|
97 |
+
# Bouton pour soumettre la question
|
98 |
+
if st.button("Obtenir une réponse"):
|
99 |
+
if prompt:
|
100 |
+
response = process_input(prompt, image_file)
|
101 |
+
st.markdown("### Réponse")
|
102 |
+
st.markdown(response)
|
103 |
+
else:
|
104 |
+
st.warning("Veuillez entrer une question.")
|
105 |
+
|
106 |
+
with tab2:
|
107 |
+
st.subheader("Exemples de questions")
|
108 |
+
|
109 |
+
example1 = st.button("C'est quoi un mass media?")
|
110 |
+
example2 = st.button("C'est quoi l'entrepreneuriat?")
|
111 |
+
|
112 |
+
if example1:
|
113 |
+
response = process_input("C'est quoi un mass media", None)
|
114 |
+
st.markdown("### Réponse")
|
115 |
+
st.markdown(response)
|
116 |
+
|
117 |
+
if example2:
|
118 |
+
response = process_input("C'est quoi l'entrepreneuriat", None)
|
119 |
+
st.markdown("### Réponse")
|
120 |
+
st.markdown(response)
|
121 |
|
122 |
+
# Affichage des informations sur les fichiers chargés
|
123 |
+
with st.sidebar:
|
124 |
+
st.subheader("Informations")
|
125 |
+
st.write(f"Nombre de fichiers PDF chargés: {len(uploaded_files)}")
|
126 |
+
if uploaded_files:
|
127 |
+
st.write("Fichiers PDF dans le dossier 'data':")
|
128 |
+
for file in os.listdir("data"):
|
129 |
+
if file.lower().endswith(".pdf"):
|
130 |
+
st.write(f"- {file}")
|