fix the model to gpt-turbo
Browse files
app.py
CHANGED
|
@@ -16,6 +16,9 @@ from pydantic import BaseModel
|
|
| 16 |
import uvicorn
|
| 17 |
import threading
|
| 18 |
import base64
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Activation du logging HTTP pour OpenAI
|
| 21 |
openai_logger = logging.getLogger("openai")
|
|
@@ -137,42 +140,42 @@ def respond(message, history, system_message, domain, model_name, max_tokens, te
|
|
| 137 |
return [{"role": "assistant", "content": f"❌ Erreur : {e}"}]
|
| 138 |
|
| 139 |
# -- Extraction OCR
|
| 140 |
-
import base64
|
| 141 |
|
| 142 |
-
def
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
results = []
|
| 147 |
-
for file_path in files: # ici file_path est un string (chemin)
|
| 148 |
-
with open(file_path, "rb") as f:
|
| 149 |
-
image_bytes = f.read()
|
| 150 |
-
|
| 151 |
-
image_base64 = base64.b64encode(image_bytes).decode()
|
| 152 |
|
|
|
|
|
|
|
|
|
|
| 153 |
messages = [
|
| 154 |
{
|
| 155 |
"role": "user",
|
| 156 |
-
"content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
"image_url": {
|
| 158 |
-
"base64":
|
| 159 |
-
"alt_text": "Image
|
| 160 |
}
|
| 161 |
}
|
| 162 |
]
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
except Exception as e:
|
| 173 |
-
results.append(f"Erreur lors de l'analyse de l'image: {e}")
|
| 174 |
|
| 175 |
-
|
|
|
|
| 176 |
|
| 177 |
|
| 178 |
# --- Interface utilisateur Gradio
|
|
@@ -234,7 +237,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
|
|
| 234 |
id_button = gr.Button("📤 Extraire les informations")
|
| 235 |
|
| 236 |
def extract_id_fn(files):
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
id_button.click(fn=extract_id_fn, inputs=[id_image_input], outputs=[id_output])
|
| 240 |
|
|
|
|
| 16 |
import uvicorn
|
| 17 |
import threading
|
| 18 |
import base64
|
| 19 |
+
from PIL import Image
|
| 20 |
+
from io import BytesIO
|
| 21 |
+
|
| 22 |
|
| 23 |
# Activation du logging HTTP pour OpenAI
|
| 24 |
openai_logger = logging.getLogger("openai")
|
|
|
|
| 140 |
return [{"role": "assistant", "content": f"❌ Erreur : {e}"}]
|
| 141 |
|
| 142 |
# -- Extraction OCR
|
|
|
|
| 143 |
|
| 144 |
+
def image_to_base64(image):
|
| 145 |
+
buffered = BytesIO()
|
| 146 |
+
image.save(buffered, format="PNG") # ou "JPEG" si besoin
|
| 147 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
def extract_identity_card_info(image):
|
| 150 |
+
try:
|
| 151 |
+
base64_img = image_to_base64(image)
|
| 152 |
messages = [
|
| 153 |
{
|
| 154 |
"role": "user",
|
| 155 |
+
"content": (
|
| 156 |
+
"Voici une image d'une carte d'identité. "
|
| 157 |
+
"Merci d'extraire les informations suivantes avec précision : "
|
| 158 |
+
"Nom, Prénom, Date de naissance, Numéro de carte, Date d'expiration, "
|
| 159 |
+
"Adresse si présente, et tout autre détail pertinent."
|
| 160 |
+
),
|
| 161 |
"image_url": {
|
| 162 |
+
"base64": base64_img,
|
| 163 |
+
"alt_text": "Image d'une carte d'identité"
|
| 164 |
}
|
| 165 |
}
|
| 166 |
]
|
| 167 |
|
| 168 |
+
response = client.chat.completions.create(
|
| 169 |
+
# model="gpt-image-1", # modèle multimodal supportant images
|
| 170 |
+
model="gpt-4-turbo", # modèle multimodal supportant images
|
| 171 |
+
messages=messages,
|
| 172 |
+
max_tokens=500,
|
| 173 |
+
temperature=0
|
| 174 |
+
)
|
| 175 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
except Exception as e:
|
| 178 |
+
return f"Erreur lors de l'extraction : {e}"
|
| 179 |
|
| 180 |
|
| 181 |
# --- Interface utilisateur Gradio
|
|
|
|
| 237 |
id_button = gr.Button("📤 Extraire les informations")
|
| 238 |
|
| 239 |
def extract_id_fn(files):
|
| 240 |
+
if not files:
|
| 241 |
+
return "❌ Aucun fichier reçu."
|
| 242 |
+
|
| 243 |
+
# Si c'est un upload Gradio en mode fichier, files est une liste de chemins locaux
|
| 244 |
+
# On prend la première image (carte d'identité)
|
| 245 |
+
file_path = files[0]
|
| 246 |
+
|
| 247 |
+
# Ouvrir l'image avec PIL
|
| 248 |
+
image = Image.open(file_path)
|
| 249 |
+
|
| 250 |
+
# Appeler la fonction d'extraction
|
| 251 |
+
return extract_identity_card_info(image)
|
| 252 |
|
| 253 |
id_button.click(fn=extract_id_fn, inputs=[id_image_input], outputs=[id_output])
|
| 254 |
|