Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 tempfile
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Configuration de l'API Gemini
|
10 |
+
token = os.environ.get("TOKEN")
|
11 |
+
genai.configure(api_key=token)
|
12 |
+
|
13 |
+
generation_config = {
|
14 |
+
"temperature": 1,
|
15 |
+
"max_output_tokens": 8192,
|
16 |
+
}
|
17 |
+
|
18 |
+
safety_settings = [
|
19 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
20 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
21 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
22 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
23 |
+
]
|
24 |
+
|
25 |
+
model = genai.GenerativeModel(
|
26 |
+
model_name="gemini-1.5-flash-latest",
|
27 |
+
generation_config=generation_config,
|
28 |
+
safety_settings=safety_settings
|
29 |
+
)
|
30 |
+
|
31 |
+
def generate_table(image):
|
32 |
+
"""Génère le tableau d'analyse à partir de l'image"""
|
33 |
+
prompt = "Fais un tableau des outils à utiliser pour ce commentaire composé. Je veux les outils, repérage, et interprétation."
|
34 |
+
response = model.generate_content([prompt, image])
|
35 |
+
return response.text
|
36 |
+
|
37 |
+
def generate_dissertation(tableau):
|
38 |
+
"""Génère la dissertation basée sur le tableau"""
|
39 |
+
prompt = f"""En utilisant ce tableau d'analyse :
|
40 |
+
{tableau}
|
41 |
+
|
42 |
+
Génère une dissertation structurée qui analyse."""
|
43 |
+
response = model.generate_content(prompt)
|
44 |
+
return response.text
|
45 |
+
|
46 |
+
@app.route('/')
|
47 |
+
def index():
|
48 |
+
return render_template('index.html')
|
49 |
+
|
50 |
+
@app.route('/analyze', methods=['POST'])
|
51 |
+
def analyze():
|
52 |
+
if 'image' not in request.files:
|
53 |
+
return jsonify({'error': 'No image uploaded'}), 400
|
54 |
+
|
55 |
+
image_file = request.files['image']
|
56 |
+
|
57 |
+
# Sauvegarder temporairement l'image
|
58 |
+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
|
59 |
+
image_file.save(temp_file.name)
|
60 |
+
image = Image.open(temp_file.name)
|
61 |
+
|
62 |
+
try:
|
63 |
+
# Première génération : le tableau
|
64 |
+
tableau = generate_table(image)
|
65 |
+
|
66 |
+
# Deuxième génération : la dissertation
|
67 |
+
dissertation = generate_dissertation(tableau)
|
68 |
+
|
69 |
+
return jsonify({
|
70 |
+
'tableau': tableau,
|
71 |
+
'dissertation': dissertation
|
72 |
+
})
|
73 |
+
except Exception as e:
|
74 |
+
return jsonify({'error': str(e)}), 500
|
75 |
+
finally:
|
76 |
+
# Nettoyer le fichier temporaire
|
77 |
+
os.unlink(temp_file.name)
|
78 |
+
|