Spaces:
Running
Running
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"}, | |
] | |
# Choose the Gemini model (make sure it supports multiple images) | |
model = genai.GenerativeModel( | |
model_name="gemini-2.0-flash-exp", # Or gemini-1.5-pro if available | |
generation_config=generation_config, | |
safety_settings=safety_settings | |
) | |
def index(): | |
return render_template('index.html') | |
def gpt_francais(): | |
"""Handles French questions.""" | |
french_prompt = request.form.get('sujet', '').strip() | |
choix = request.form.get('choix', '').strip() | |
style = request.form.get('style', '').strip() | |
if not french_prompt: | |
return jsonify({"output": "Veuillez saisir un thème."}), 400 | |
if choix == "discuter": | |
prompt = f""" Je veux faire mon travail de français de niveau lycée sous la forme d'un travail argumentatif. | |
La question du travail est la suivante : "{french_prompt}". Tu devras discuter ce thème. | |
tu utiliseras la méthodologie suivante : | |
# INTRODUCTION: | |
- Approche par constat | |
- Problématique | |
- Annonce du plan | |
# DÉVELOPPEMENT: | |
- Introduction partielle (énonce la thèse) | |
- Argument 1: | |
* Explications | |
* Illustration (exemple + explication) | |
- Argument 2: | |
* Explications | |
* Illustration (exemple + explication) | |
- Argument 3: | |
* Explications | |
* Illustration (exemple + explication) | |
# phrase de Transiton vers la deuxieme partie : | |
- Introduction partielle (énonce l'antithèse) | |
- Argument 1: | |
* Explications | |
* Illustration (exemple + explication) | |
- Argument 2: | |
* Explications | |
* Illustration (exemple + explication) | |
- Argument 3: | |
* Explications | |
* Illustration (exemple + explication) | |
#Conclusion | |
* Bilan | |
* Ouverture du sujet (sous forme de phrase interrogative ) | |
Je veux que tu utilises un style d'écriture {style}.""" | |
else: | |
prompt = f"""Je veux faire mon travail de français de niveau lycé sous la forme d'un travail argumentatif. | |
La question du travail est la suivante : "{french_prompt}". Tu devras {choix} ce thème. | |
tu utiliseras la méthodologie suivante : | |
# INTRODUCTION: | |
- Approche par constat | |
- Problématique | |
- Annonce du plan | |
# DÉVELOPPEMENT: | |
- Phrase chapeau (énonce la thèse) | |
- Argument 1: | |
* Explications | |
* Illustration (exemple + explication) | |
- Argument 2: | |
* Explications | |
* Illustration (exemple + explication) | |
- Argument 3: | |
* Explications | |
* Illustration (exemple + explication) | |
#Conclusion | |
* Bilan (thèse + arguments1 + arguments2+arguments3) | |
* Ouverture du sujet ( sous forme de phrase interrogative ) | |
Je veux que tu utilises un style d'écriture {style}.""" | |
try: | |
response = model.generate_content(prompt) | |
return jsonify({"output": response.text}), 200 | |
except Exception as e: | |
return jsonify({"output": "eror"}), 500 | |
def gpt_francais_cc(): | |
"""Handles text analysis for French with multiple images.""" | |
if 'images' not in request.files: | |
return jsonify({"output": "Aucune image n'a été téléchargée."}), 400 | |
images = request.files.getlist('images') | |
if not images: | |
return jsonify({"output": "Aucune image selectionnée."}), 400 | |
pre_prompt = "Traite entièrement ce sujet de français." | |
image_parts = [] | |
for image in images: | |
try: | |
img = Image.open(image) | |
image_parts.append(img) | |
except Exception as e: | |
return jsonify({"output": f"Erreur lors du traitement de l'image : {e}"}), 500 | |
try: | |
response = model.generate_content([pre_prompt] + image_parts) | |
return jsonify({"output": response.text}), 200 | |
except Exception as e: | |
return jsonify({"output": str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(debug=True) |