Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
from flask import Flask, render_template, request, jsonify
|
2 |
import google.generativeai as genai
|
|
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
-
import
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
|
@@ -22,9 +23,9 @@ safety_settings = [
|
|
22 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
23 |
]
|
24 |
|
25 |
-
# Choose the Gemini model
|
26 |
model = genai.GenerativeModel(
|
27 |
-
model_name="gemini-2.0-flash-exp",
|
28 |
generation_config=generation_config,
|
29 |
safety_settings=safety_settings
|
30 |
)
|
@@ -33,8 +34,6 @@ model = genai.GenerativeModel(
|
|
33 |
def index():
|
34 |
return render_template('index.html')
|
35 |
|
36 |
-
|
37 |
-
|
38 |
@app.route('/api/francais', methods=['POST'])
|
39 |
def gpt_francais():
|
40 |
"""Handles French questions."""
|
@@ -118,7 +117,7 @@ def gpt_francais():
|
|
118 |
response = model.generate_content(prompt)
|
119 |
return jsonify({"output": response.text}), 200
|
120 |
except Exception as e:
|
121 |
-
return jsonify({"output": "
|
122 |
|
123 |
@app.route('/api/etude-texte', methods=['POST'])
|
124 |
def gpt_francais_cc():
|
@@ -130,22 +129,34 @@ def gpt_francais_cc():
|
|
130 |
if not images:
|
131 |
return jsonify({"output": "Aucune image selectionnée."}), 400
|
132 |
|
133 |
-
pre_prompt = "Traite entièrement
|
134 |
-
|
135 |
-
|
|
|
|
|
136 |
for image in images:
|
137 |
try:
|
138 |
-
|
139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
except Exception as e:
|
141 |
-
return jsonify({
|
|
|
|
|
142 |
|
143 |
try:
|
144 |
-
|
145 |
-
|
146 |
return jsonify({"output": response.text}), 200
|
147 |
except Exception as e:
|
148 |
-
return jsonify({
|
|
|
|
|
149 |
|
150 |
if __name__ == '__main__':
|
151 |
app.run(debug=True)
|
|
|
1 |
from flask import Flask, render_template, request, jsonify
|
2 |
import google.generativeai as genai
|
3 |
+
from google.generativeai import types
|
4 |
import os
|
5 |
from PIL import Image
|
6 |
+
import io
|
7 |
|
8 |
app = Flask(__name__)
|
9 |
|
|
|
23 |
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
24 |
]
|
25 |
|
26 |
+
# Choose the Gemini model
|
27 |
model = genai.GenerativeModel(
|
28 |
+
model_name="gemini-2.0-flash-exp",
|
29 |
generation_config=generation_config,
|
30 |
safety_settings=safety_settings
|
31 |
)
|
|
|
34 |
def index():
|
35 |
return render_template('index.html')
|
36 |
|
|
|
|
|
37 |
@app.route('/api/francais', methods=['POST'])
|
38 |
def gpt_francais():
|
39 |
"""Handles French questions."""
|
|
|
117 |
response = model.generate_content(prompt)
|
118 |
return jsonify({"output": response.text}), 200
|
119 |
except Exception as e:
|
120 |
+
return jsonify({"output": f"Erreur lors de la génération : {str(e)}"}), 500
|
121 |
|
122 |
@app.route('/api/etude-texte', methods=['POST'])
|
123 |
def gpt_francais_cc():
|
|
|
129 |
if not images:
|
130 |
return jsonify({"output": "Aucune image selectionnée."}), 400
|
131 |
|
132 |
+
pre_prompt = "Traite entièrement devoir."
|
133 |
+
|
134 |
+
# Préparer les images pour l'API Gemini
|
135 |
+
contents = [pre_prompt]
|
136 |
+
|
137 |
for image in images:
|
138 |
try:
|
139 |
+
# Lire l'image avec PIL
|
140 |
+
img_bytes = image.read()
|
141 |
+
img = Image.open(io.BytesIO(img_bytes))
|
142 |
+
|
143 |
+
# Convertir en format compatible avec Gemini
|
144 |
+
# L'image PIL peut être directement utilisée avec l'API Gemini
|
145 |
+
contents.append(img)
|
146 |
+
|
147 |
except Exception as e:
|
148 |
+
return jsonify({
|
149 |
+
"output": f"Erreur lors du traitement de l'image {image.filename}: {str(e)}"
|
150 |
+
}), 500
|
151 |
|
152 |
try:
|
153 |
+
# Générer le contenu avec toutes les images
|
154 |
+
response = model.generate_content(contents)
|
155 |
return jsonify({"output": response.text}), 200
|
156 |
except Exception as e:
|
157 |
+
return jsonify({
|
158 |
+
"output": f"Erreur lors de l'analyse des images : {str(e)}"
|
159 |
+
}), 500
|
160 |
|
161 |
if __name__ == '__main__':
|
162 |
app.run(debug=True)
|