Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,44 @@
|
|
| 1 |
from flask import Flask, request, jsonify, render_template, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
-
from PIL import Image
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
import google.generativeai as genai
|
| 9 |
-
import numpy as np
|
| 10 |
|
|
|
|
| 11 |
generation_config = {
|
| 12 |
-
|
| 13 |
-
|
| 14 |
}
|
| 15 |
|
| 16 |
safety_settings = [
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
]
|
| 34 |
|
| 35 |
-
GOOGLE_API_KEY = os.environ.get("TOKEN")
|
| 36 |
|
| 37 |
-
# Configuration de l'API Gemini
|
| 38 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 39 |
|
| 40 |
app = Flask(__name__)
|
| 41 |
CORS(app)
|
| 42 |
|
| 43 |
-
@app.route('/', methods=['GET'])
|
| 44 |
-
def svt():
|
| 45 |
-
"""Renders the SVT page."""
|
| 46 |
-
return render_template("svt.html")
|
| 47 |
-
|
| 48 |
# Prompt pour la détection d'objets
|
| 49 |
DETECTION_PROMPT = "Detect items, with no more than 20 items. Output a json list where each entry contains the 2D bounding box in \"box_2d\" and a text label in \"label\"."
|
| 50 |
|
|
@@ -68,7 +62,10 @@ UPLOAD_FOLDER = 'uploads'
|
|
| 68 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 69 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
@app.route('/analyze', methods=['POST'])
|
| 74 |
def analyze_image():
|
|
@@ -89,8 +86,8 @@ def analyze_image():
|
|
| 89 |
# 1. Détection d'objets avec Gemini
|
| 90 |
model = genai.GenerativeModel("gemini-2.0-flash-exp",safety_settings=safety_settings,generation_config=generation_config)
|
| 91 |
image_part = {
|
| 92 |
-
"mime_type": "image/jpeg",
|
| 93 |
-
"data": open(filename, "rb").read()
|
| 94 |
}
|
| 95 |
response = model.generate_content([DETECTION_PROMPT, image_part])
|
| 96 |
|
|
@@ -162,6 +159,7 @@ def analyze_image():
|
|
| 162 |
print(f"Une erreur s'est produite : {e}")
|
| 163 |
return jsonify({'error': f'Erreur lors du traitement de l\'image : {e}'}), 500
|
| 164 |
|
|
|
|
| 165 |
@app.route('/uploads/<filename>')
|
| 166 |
def uploaded_file(filename):
|
| 167 |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, render_template, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import uuid
|
| 8 |
import google.generativeai as genai
|
|
|
|
| 9 |
|
| 10 |
+
# Configuration de l'API Gemini
|
| 11 |
generation_config = {
|
| 12 |
+
"temperature": 1,
|
| 13 |
+
"max_output_tokens": 8192,
|
| 14 |
}
|
| 15 |
|
| 16 |
safety_settings = [
|
| 17 |
+
{
|
| 18 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 19 |
+
"threshold": "BLOCK_NONE"
|
| 20 |
+
},
|
| 21 |
+
{
|
| 22 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 23 |
+
"threshold": "BLOCK_NONE"
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 27 |
+
"threshold": "BLOCK_NONE"
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 31 |
+
"threshold": "BLOCK_NONE"
|
| 32 |
+
},
|
| 33 |
]
|
| 34 |
|
| 35 |
+
GOOGLE_API_KEY = os.environ.get("TOKEN") # Assurez vous que la variable d'environnement TOKEN est bien définie
|
| 36 |
|
|
|
|
| 37 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 38 |
|
| 39 |
app = Flask(__name__)
|
| 40 |
CORS(app)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Prompt pour la détection d'objets
|
| 43 |
DETECTION_PROMPT = "Detect items, with no more than 20 items. Output a json list where each entry contains the 2D bounding box in \"box_2d\" and a text label in \"label\"."
|
| 44 |
|
|
|
|
| 62 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 63 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 64 |
|
| 65 |
+
@app.route('/', methods=['GET'])
|
| 66 |
+
def svt():
|
| 67 |
+
"""Renders the SVT page."""
|
| 68 |
+
return render_template("templates_svt (3).html")
|
| 69 |
|
| 70 |
@app.route('/analyze', methods=['POST'])
|
| 71 |
def analyze_image():
|
|
|
|
| 86 |
# 1. Détection d'objets avec Gemini
|
| 87 |
model = genai.GenerativeModel("gemini-2.0-flash-exp",safety_settings=safety_settings,generation_config=generation_config)
|
| 88 |
image_part = {
|
| 89 |
+
"mime_type": "image/jpeg",
|
| 90 |
+
"data": open(filename, "rb").read()
|
| 91 |
}
|
| 92 |
response = model.generate_content([DETECTION_PROMPT, image_part])
|
| 93 |
|
|
|
|
| 159 |
print(f"Une erreur s'est produite : {e}")
|
| 160 |
return jsonify({'error': f'Erreur lors du traitement de l\'image : {e}'}), 500
|
| 161 |
|
| 162 |
+
# Servir les fichiers statiques depuis le dossier 'uploads'
|
| 163 |
@app.route('/uploads/<filename>')
|
| 164 |
def uploaded_file(filename):
|
| 165 |
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|