Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,10 +4,6 @@ import os
|
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
import PIL.Image
|
7 |
-
import requests
|
8 |
-
from urllib.parse import urlparse
|
9 |
-
import ssl
|
10 |
-
import time
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
|
@@ -27,6 +23,7 @@ safety_settings = [
|
|
27 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
28 |
]
|
29 |
|
|
|
30 |
@app.route('/')
|
31 |
def generale():
|
32 |
return render_template("generale.html")
|
@@ -52,23 +49,26 @@ def upload_and_process_file(file_path):
|
|
52 |
raise ValueError(f"Le fichier {file_path} est vide")
|
53 |
|
54 |
# Upload du fichier
|
55 |
-
|
56 |
-
|
57 |
-
# Ici, je vais simuler l'upload avec un print pour l'exemple.
|
58 |
-
print(f"Upload réussi (simulation): {file_path}")
|
59 |
|
60 |
-
# Attente du traitement
|
61 |
-
# Dans une vraie implémentation, tu devrais vérifier l'état de l'upload
|
62 |
timeout = 300 # 5 minutes
|
63 |
start_time = time.time()
|
64 |
|
65 |
-
while
|
66 |
-
|
|
|
|
|
|
|
67 |
time.sleep(10)
|
68 |
-
|
69 |
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
72 |
|
73 |
except ssl.SSLError as e:
|
74 |
print(f"Erreur SSL lors de l'upload (tentative {attempt + 1}): {e}")
|
@@ -146,7 +146,6 @@ def submit_question():
|
|
146 |
question = request.form.get('question')
|
147 |
urls = request.form.getlist('urls')
|
148 |
files = request.files.getlist('files')
|
149 |
-
web_search = request.form.get('web_search') == 'true' # Récupère l'état de la recherche web
|
150 |
|
151 |
print("URLs reçues:", urls)
|
152 |
content = [question]
|
@@ -162,8 +161,7 @@ def submit_question():
|
|
162 |
if file.filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
|
163 |
content.append(PIL.Image.open(temp_file.name))
|
164 |
else:
|
165 |
-
|
166 |
-
uploaded_file = {"path": temp_file.name, "status": "success"}
|
167 |
content.append(uploaded_file)
|
168 |
|
169 |
# Traitement des URLs
|
@@ -173,29 +171,25 @@ def submit_question():
|
|
173 |
video_path = download_youtube_video(url)
|
174 |
if video_path:
|
175 |
temp_files.append(video_path)
|
176 |
-
|
177 |
-
uploaded_file = {"path": video_path, "status": "success"}
|
178 |
content.append(uploaded_file)
|
179 |
elif url.lower().endswith('.pdf'):
|
180 |
pdf_path = telecharger_pdf(url)
|
181 |
if pdf_path:
|
182 |
temp_files.append(pdf_path)
|
183 |
-
|
184 |
-
uploaded_file = {"path": pdf_path, "status": "success"}
|
185 |
content.append(uploaded_file)
|
|
|
|
|
186 |
|
187 |
|
188 |
# Génération de contenu avec Gemini
|
189 |
model = genai.GenerativeModel(
|
190 |
-
model_name="gemini-1.5-flash",
|
191 |
safety_settings=safety_settings,
|
192 |
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."
|
193 |
)
|
194 |
-
|
195 |
-
if web_search:
|
196 |
-
response = model.generate_content(question,tools='google_search_retrieval',request_options={"timeout": 600})
|
197 |
-
else:
|
198 |
-
response = model.generate_content(content, request_options={"timeout": 600})
|
199 |
return jsonify({"response": response.text})
|
200 |
|
201 |
except Exception as e:
|
@@ -206,4 +200,6 @@ def submit_question():
|
|
206 |
try:
|
207 |
os.unlink(temp_file)
|
208 |
except Exception as e:
|
209 |
-
print(f"Erreur lors de la suppression du fichier temporaire {temp_file}: {e}")
|
|
|
|
|
|
4 |
from PIL import Image
|
5 |
import tempfile
|
6 |
import PIL.Image
|
|
|
|
|
|
|
|
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
|
|
23 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
24 |
]
|
25 |
|
26 |
+
|
27 |
@app.route('/')
|
28 |
def generale():
|
29 |
return render_template("generale.html")
|
|
|
49 |
raise ValueError(f"Le fichier {file_path} est vide")
|
50 |
|
51 |
# Upload du fichier
|
52 |
+
uploaded_file = genai.upload_file(path=file_path)
|
53 |
+
print(f"Upload réussi: {uploaded_file.uri}")
|
|
|
|
|
54 |
|
55 |
+
# Attente du traitement
|
|
|
56 |
timeout = 300 # 5 minutes
|
57 |
start_time = time.time()
|
58 |
|
59 |
+
while uploaded_file.state.name == "PROCESSING":
|
60 |
+
if time.time() - start_time > timeout:
|
61 |
+
raise TimeoutError("Timeout pendant le traitement du fichier")
|
62 |
+
|
63 |
+
print(f"En attente du traitement... Temps écoulé: {int(time.time() - start_time)}s")
|
64 |
time.sleep(10)
|
65 |
+
uploaded_file = genai.get_file(uploaded_file.name)
|
66 |
|
67 |
+
if uploaded_file.state.name == "FAILED":
|
68 |
+
raise ValueError(f"Échec du traitement: {uploaded_file.state.name}")
|
69 |
+
|
70 |
+
print(f"Traitement terminé avec succès: {uploaded_file.uri}")
|
71 |
+
return uploaded_file
|
72 |
|
73 |
except ssl.SSLError as e:
|
74 |
print(f"Erreur SSL lors de l'upload (tentative {attempt + 1}): {e}")
|
|
|
146 |
question = request.form.get('question')
|
147 |
urls = request.form.getlist('urls')
|
148 |
files = request.files.getlist('files')
|
|
|
149 |
|
150 |
print("URLs reçues:", urls)
|
151 |
content = [question]
|
|
|
161 |
if file.filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
|
162 |
content.append(PIL.Image.open(temp_file.name))
|
163 |
else:
|
164 |
+
uploaded_file = upload_and_process_file(temp_file.name)
|
|
|
165 |
content.append(uploaded_file)
|
166 |
|
167 |
# Traitement des URLs
|
|
|
171 |
video_path = download_youtube_video(url)
|
172 |
if video_path:
|
173 |
temp_files.append(video_path)
|
174 |
+
uploaded_file = upload_and_process_file(video_path)
|
|
|
175 |
content.append(uploaded_file)
|
176 |
elif url.lower().endswith('.pdf'):
|
177 |
pdf_path = telecharger_pdf(url)
|
178 |
if pdf_path:
|
179 |
temp_files.append(pdf_path)
|
180 |
+
uploaded_file = upload_and_process_file(pdf_path)
|
|
|
181 |
content.append(uploaded_file)
|
182 |
+
|
183 |
+
|
184 |
|
185 |
|
186 |
# Génération de contenu avec Gemini
|
187 |
model = genai.GenerativeModel(
|
188 |
+
model_name="models/gemini-1.5-flash-002",
|
189 |
safety_settings=safety_settings,
|
190 |
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."
|
191 |
)
|
192 |
+
response = model.generate_content(content, request_options={"timeout": 600})
|
|
|
|
|
|
|
|
|
193 |
return jsonify({"response": response.text})
|
194 |
|
195 |
except Exception as e:
|
|
|
200 |
try:
|
201 |
os.unlink(temp_file)
|
202 |
except Exception as e:
|
203 |
+
print(f"Erreur lors de la suppression du fichier temporaire {temp_file}: {e}")
|
204 |
+
|
205 |
+
|