Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -48,8 +48,8 @@ def gpt_francais():
|
|
48 |
|
49 |
if not french_prompt:
|
50 |
logging.warning("French prompt is empty.")
|
51 |
-
return Response(f"data: {json.dumps({'type': 'error', 'content': '
|
52 |
-
|
53 |
|
54 |
# Sélectionner le modèle
|
55 |
model_to_use = DEEPTHINK_MODEL_NAME if use_deepthink else STANDARD_MODEL_NAME
|
@@ -57,12 +57,12 @@ def gpt_francais():
|
|
57 |
|
58 |
# Charger l'instruction système
|
59 |
try:
|
60 |
-
system_instruction = load_prompt(
|
61 |
-
except Exception
|
62 |
-
logging.error(
|
63 |
system_instruction = "Tu es un assistant spécialisé en français."
|
64 |
|
65 |
-
# Construire le prompt utilisateur
|
66 |
user_prompt = f"Sujet: {french_prompt}\nType: {choix}\nStyle: {style}"
|
67 |
|
68 |
# Configuration pour cette requête
|
@@ -82,7 +82,7 @@ def gpt_francais():
|
|
82 |
try:
|
83 |
thoughts = ""
|
84 |
answer = ""
|
85 |
-
|
86 |
for chunk in client.models.generate_content_stream(
|
87 |
model=model_to_use,
|
88 |
contents=[user_prompt],
|
@@ -101,48 +101,39 @@ def gpt_francais():
|
|
101 |
yield f"data: {json.dumps({'type': 'answer_start'})}\n\n"
|
102 |
answer += part.text
|
103 |
yield f"data: {json.dumps({'type': 'answer', 'content': part.text})}\n\n"
|
104 |
-
|
105 |
-
yield f"data: {json.dumps({'type': 'done'})}\n\n"
|
106 |
-
|
107 |
-
except Exception as e:
|
108 |
-
logging.exception(f"Error generating content with model {model_to_use}: {e}")
|
109 |
-
|
110 |
-
if "model" in str(e).lower() and ("not found" in str(e).lower() or "not available" in str(e).lower()):
|
111 |
-
logging.error(f"Model '{model_to_use}' not found.")
|
112 |
-
yield f"data: {json.dumps({'type': 'error', 'content':'Erreur')}"
|
113 |
-
else:
|
114 |
-
logging.error(f"Internal error occurred: {e}")
|
115 |
-
yield f"data: {json.dumps({'type': 'error', 'content': 'Une erreur interne est survenue lors de la génération du contenu. Veuillez réessayer.'})}\n\n"
|
116 |
|
117 |
-
|
118 |
|
|
|
|
|
|
|
119 |
|
120 |
-
|
121 |
|
122 |
|
123 |
@app.route('/api/etude-texte', methods=['POST'])
|
124 |
def gpt_francais_cc():
|
125 |
"""Analyse d'images avec streaming imposé."""
|
126 |
if 'images' not in request.files:
|
127 |
-
return Response(f"data: {json.dumps({'type': 'error', 'content': '
|
128 |
-
|
129 |
|
130 |
images = request.files.getlist('images')
|
131 |
if not images:
|
132 |
-
return Response(f"data: {json.dumps({'type': 'error', 'content': '
|
133 |
-
|
134 |
|
135 |
def generate_image_analysis():
|
136 |
try:
|
137 |
# Charger l'instruction système
|
138 |
try:
|
139 |
-
system_instruction = load_prompt(
|
140 |
-
except Exception
|
141 |
-
logging.error(
|
142 |
system_instruction = "Tu es un assistant spécialisé dans l'analyse de textes et de documents."
|
143 |
|
144 |
content = ["Réponds aux questions présentes dans les images."]
|
145 |
-
|
146 |
for image in images:
|
147 |
if image.filename:
|
148 |
img_data = image.read()
|
@@ -151,13 +142,13 @@ def gpt_francais_cc():
|
|
151 |
mime_type=image.content_type or 'image/jpeg'
|
152 |
)
|
153 |
content.append(img_part)
|
154 |
-
|
155 |
config = types.GenerateContentConfig(
|
156 |
system_instruction=system_instruction,
|
157 |
temperature=0.7,
|
158 |
max_output_tokens=4096
|
159 |
)
|
160 |
-
|
161 |
for chunk in client.models.generate_content_stream(
|
162 |
model=STANDARD_MODEL_NAME,
|
163 |
contents=content,
|
@@ -166,17 +157,14 @@ def gpt_francais_cc():
|
|
166 |
for part in chunk.candidates[0].content.parts:
|
167 |
if part.text:
|
168 |
yield f"data: {json.dumps({'type': 'content', 'content': part.text})}\n\n"
|
169 |
-
|
170 |
-
yield f"data: {json.dumps({'type': 'done'})}\n\n"
|
171 |
-
|
172 |
-
except Exception as e:
|
173 |
-
logging.exception(f"Error in image analysis streaming: {e}")
|
174 |
-
yield f"data: {json.dumps({'type': 'error', 'content': 'Erreur lors de l\\'analyse des images.'})}\n\n"
|
175 |
|
176 |
-
|
177 |
|
|
|
|
|
|
|
178 |
|
179 |
-
|
180 |
|
181 |
|
182 |
if __name__ == '__main__':
|
|
|
48 |
|
49 |
if not french_prompt:
|
50 |
logging.warning("French prompt is empty.")
|
51 |
+
return Response(f"data: {json.dumps({'type': 'error', 'content': 'Erreur'})}\n\n",
|
52 |
+
mimetype='text/event-stream'), 400
|
53 |
|
54 |
# Sélectionner le modèle
|
55 |
model_to_use = DEEPTHINK_MODEL_NAME if use_deepthink else STANDARD_MODEL_NAME
|
|
|
57 |
|
58 |
# Charger l'instruction système
|
59 |
try:
|
60 |
+
system_instruction = load_prompt()
|
61 |
+
except Exception:
|
62 |
+
logging.error("Erreur")
|
63 |
system_instruction = "Tu es un assistant spécialisé en français."
|
64 |
|
65 |
+
# Construire le prompt utilisateur
|
66 |
user_prompt = f"Sujet: {french_prompt}\nType: {choix}\nStyle: {style}"
|
67 |
|
68 |
# Configuration pour cette requête
|
|
|
82 |
try:
|
83 |
thoughts = ""
|
84 |
answer = ""
|
85 |
+
|
86 |
for chunk in client.models.generate_content_stream(
|
87 |
model=model_to_use,
|
88 |
contents=[user_prompt],
|
|
|
101 |
yield f"data: {json.dumps({'type': 'answer_start'})}\n\n"
|
102 |
answer += part.text
|
103 |
yield f"data: {json.dumps({'type': 'answer', 'content': part.text})}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
+
yield f"data: {json.dumps({'type': 'done'})}\n\n"
|
106 |
|
107 |
+
except Exception:
|
108 |
+
logging.error("Erreur")
|
109 |
+
yield f"data: {json.dumps({'type': 'error', 'content': 'Erreur'})}\n\n"
|
110 |
|
111 |
+
return Response(generate_stream(), mimetype='text/event-stream')
|
112 |
|
113 |
|
114 |
@app.route('/api/etude-texte', methods=['POST'])
|
115 |
def gpt_francais_cc():
|
116 |
"""Analyse d'images avec streaming imposé."""
|
117 |
if 'images' not in request.files:
|
118 |
+
return Response(f"data: {json.dumps({'type': 'error', 'content': 'Erreur'})}\n\n",
|
119 |
+
mimetype='text/event-stream'), 400
|
120 |
|
121 |
images = request.files.getlist('images')
|
122 |
if not images:
|
123 |
+
return Response(f"data: {json.dumps({'type': 'error', 'content': 'Erreur'})}\n\n",
|
124 |
+
mimetype='text/event-stream'), 400
|
125 |
|
126 |
def generate_image_analysis():
|
127 |
try:
|
128 |
# Charger l'instruction système
|
129 |
try:
|
130 |
+
system_instruction = load_prompt()
|
131 |
+
except Exception:
|
132 |
+
logging.error("Erreur")
|
133 |
system_instruction = "Tu es un assistant spécialisé dans l'analyse de textes et de documents."
|
134 |
|
135 |
content = ["Réponds aux questions présentes dans les images."]
|
136 |
+
|
137 |
for image in images:
|
138 |
if image.filename:
|
139 |
img_data = image.read()
|
|
|
142 |
mime_type=image.content_type or 'image/jpeg'
|
143 |
)
|
144 |
content.append(img_part)
|
145 |
+
|
146 |
config = types.GenerateContentConfig(
|
147 |
system_instruction=system_instruction,
|
148 |
temperature=0.7,
|
149 |
max_output_tokens=4096
|
150 |
)
|
151 |
+
|
152 |
for chunk in client.models.generate_content_stream(
|
153 |
model=STANDARD_MODEL_NAME,
|
154 |
contents=content,
|
|
|
157 |
for part in chunk.candidates[0].content.parts:
|
158 |
if part.text:
|
159 |
yield f"data: {json.dumps({'type': 'content', 'content': part.text})}\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
yield f"data: {json.dumps({'type': 'done'})}\n\n"
|
162 |
|
163 |
+
except Exception:
|
164 |
+
logging.error("Erreur")
|
165 |
+
yield f"data: {json.dumps({'type': 'error', 'content': 'Erreur'})}\n\n"
|
166 |
|
167 |
+
return Response(generate_image_analysis(), mimetype='text/event-stream')
|
168 |
|
169 |
|
170 |
if __name__ == '__main__':
|