Mariam-cc / app.py
Docfile's picture
Create app.py
715946a verified
raw
history blame
2.38 kB
from flask import Flask, render_template, request, jsonify
import google.generativeai as genai
import os
from PIL import Image
import tempfile
app = Flask(__name__)
# Configuration de l'API Gemini
token = os.environ.get("TOKEN")
genai.configure(api_key=token)
generation_config = {
"temperature": 1,
"max_output_tokens": 8192,
}
safety_settings = [
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
]
model = genai.GenerativeModel(
model_name="gemini-1.5-flash-latest",
generation_config=generation_config,
safety_settings=safety_settings
)
def generate_table(image):
"""Génère le tableau d'analyse à partir de l'image"""
prompt = "Fais un tableau des outils à utiliser pour ce commentaire composé. Je veux les outils, repérage, et interprétation."
response = model.generate_content([prompt, image])
return response.text
def generate_dissertation(tableau):
"""Génère la dissertation basée sur le tableau"""
prompt = f"""En utilisant ce tableau d'analyse :
{tableau}
Génère une dissertation structurée qui analyse."""
response = model.generate_content(prompt)
return response.text
@app.route('/')
def index():
return render_template('index.html')
@app.route('/analyze', methods=['POST'])
def analyze():
if 'image' not in request.files:
return jsonify({'error': 'No image uploaded'}), 400
image_file = request.files['image']
# Sauvegarder temporairement l'image
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
image_file.save(temp_file.name)
image = Image.open(temp_file.name)
try:
# Première génération : le tableau
tableau = generate_table(image)
# Deuxième génération : la dissertation
dissertation = generate_dissertation(tableau)
return jsonify({
'tableau': tableau,
'dissertation': dissertation
})
except Exception as e:
return jsonify({'error': str(e)}), 500
finally:
# Nettoyer le fichier temporaire
os.unlink(temp_file.name)