|
|
from flask import Flask, render_template, request, jsonify, Response
|
|
|
import PIL.Image
|
|
|
import io
|
|
|
from google import genai
|
|
|
from google.genai import types
|
|
|
from werkzeug.utils import secure_filename
|
|
|
import os
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
|
|
app.config['UPLOAD_FOLDER'] = 'uploads'
|
|
|
|
|
|
|
|
|
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
|
|
|
|
|
|
|
|
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
|
|
client = genai.Client(api_key=GOOGLE_API_KEY)
|
|
|
|
|
|
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"},
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
PROMPT_TEXT = """
|
|
|
je souhaite faire mon travail d'anglais qui consiste à de l'analyse de texte. j'aimerais que tu le phase en respectant scrupuleusement la méthodologie suivante. j'aimerais que tu fasses ce travail en anglais en donnant également la traduction française. soit direct et evite les reponse trop longue:
|
|
|
|
|
|
|
|
|
voici le plan :
|
|
|
|
|
|
I- INTRODUCTION
|
|
|
|
|
|
Title (what is the title of the text?)
|
|
|
|
|
|
Nature (what is it?)
|
|
|
|
|
|
Author (who wrote it?)
|
|
|
|
|
|
Origin (where was it extracted?)
|
|
|
|
|
|
Date of publication (when was it written?)
|
|
|
|
|
|
Genre (descriptive...)
|
|
|
|
|
|
General idea (what is it about?)
|
|
|
|
|
|
Writer' intention (what for?)
|
|
|
|
|
|
II - SUMMARY
|
|
|
|
|
|
The components (the structure / text subdivision)
|
|
|
(The main parts of the text and their ideas)
|
|
|
|
|
|
The summary itself.
|
|
|
|
|
|
III - COMMENTARY
|
|
|
|
|
|
Focus on the main points of interest from the summary
|
|
|
|
|
|
Discuss, explain and develop the main points of the writer's opinion, deal with them in a coherent and logical order.
|
|
|
|
|
|
Tone
|
|
|
|
|
|
Style
|
|
|
|
|
|
IV-EVALUATION
|
|
|
|
|
|
Assess the text value
|
|
|
|
|
|
The interest of the text
|
|
|
|
|
|
The lessons, you've learnt and the impact on your opinion.
|
|
|
|
|
|
V - BRANCHING OUT
|
|
|
|
|
|
Personal opinion (give one's opinion)
|
|
|
|
|
|
Feelings (I had mixed feeling about.........)
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
PROMPT_IMAGE = """
|
|
|
|
|
|
je souhaite faire mon travail d'anglais qui consiste à de l'analyse de document iconographique . j'aimerais que tu le phase en respectant scrupuleusement la méthodologie suivante. j'aimerais que tu fasses ce travail en anglais en donnant également la traduction française. soit direct et evite les reponse trop longue:
|
|
|
|
|
|
|
|
|
voici le plan :
|
|
|
|
|
|
1 - Présentation du document
|
|
|
|
|
|
Présenter le document, titre, type, date, autheur , etc...
|
|
|
|
|
|
2- Description générale.
|
|
|
|
|
|
Fais une description générale de du document en le présentant. Bref décris tout ce que tu vois sur le document
|
|
|
|
|
|
3- commentaire
|
|
|
Voici le texte extrait des sections 3 et 4 :
|
|
|
|
|
|
Commencez par l'élément principal du document. Dans votre "présentation-résumé", vous avez signalé sa présence et précisé son emplacement. À présent, faites-en une description plus détaillée, et parlez de sa fonction : pourquoi est-il là ? À quoi sert-il ? Comme pour un texte, ne séparez pas le fond de la forme : traitez globalement emplacement, forme, couleur et fonction.
|
|
|
L'étude d'un document visuel doit être aussi rigoureuse que celle d'un texte : dessins, tableaux, sculptures, publicités, photos de films, sont faits d'explicite et d'implicite. Vous devez donc chercher cet implicite. Mais, comme pour les textes : pas de délire interprétatif. Répétez cette opération avec les autres éléments. Montrez bien les liens qui existent entre ces différents éléments : tout tableau, photographie (surtout artistique), publicité est un ensemble.
|
|
|
|
|
|
|
|
|
4. La synthèse / l’évaluation et la conclusion (the evaluation)
|
|
|
|
|
|
Parlez de l’atmosphère qui se dégage du document : "comment" et "pourquoi" (synthèse de ce qui a été vu en 3). Cela vous amène à parler du but que s’était fixé l’auteur du document. Donnez votre point de vue en argumentant bien votre opinion, qu’elle soit favorable ou défavorable, notamment les critiques (GIVING ONE’S OPINION: LIKES, DISLIKES, APPROVAL, DISAPPROVAL; SUMMING UP ONE’S IDEAS).
|
|
|
|
|
|
5. Le branching-out
|
|
|
|
|
|
Servez vous du document étudié pour parler d'un sujet en rapport avec ce document
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
@app.route('/')
|
|
|
def index():
|
|
|
return render_template('index.html')
|
|
|
|
|
|
@app.route('/analyze', methods=['POST'])
|
|
|
def analyze():
|
|
|
try:
|
|
|
analysis_type = request.form.get('analysis_type')
|
|
|
files = request.files.getlist('images')
|
|
|
|
|
|
if not files:
|
|
|
return jsonify({'error': 'No files uploaded'}), 400
|
|
|
|
|
|
image_parts = []
|
|
|
for file in files:
|
|
|
if file.filename:
|
|
|
|
|
|
filename = secure_filename(file.filename)
|
|
|
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
|
file.save(filepath)
|
|
|
|
|
|
|
|
|
image = PIL.Image.open(filepath)
|
|
|
image_parts.append(image)
|
|
|
|
|
|
|
|
|
os.remove(filepath)
|
|
|
|
|
|
|
|
|
prompt = PROMPT_TEXT if analysis_type == 'text' else PROMPT_IMAGE
|
|
|
|
|
|
|
|
|
def generate():
|
|
|
for chunk in client.models.generate_content_stream(
|
|
|
model="gemini-2.5-flash",
|
|
|
contents=[prompt] + image_parts,
|
|
|
config=types.GenerateContentConfig(
|
|
|
safety_settings=safety_settings
|
|
|
)
|
|
|
):
|
|
|
for part in chunk.candidates[0].content.parts:
|
|
|
if part.text:
|
|
|
yield part.text
|
|
|
|
|
|
return Response(generate(), mimetype='text/plain')
|
|
|
|
|
|
except Exception as e:
|
|
|
return jsonify({'error': str(e)}), 500
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
app.run(debug=True) |