add onea extraction
Browse files
app.py
CHANGED
|
@@ -307,6 +307,32 @@ def classify_complaint(text):
|
|
| 307 |
response = chat.invoke(messages)
|
| 308 |
return response.content.strip()
|
| 309 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
# -- API SECTION
|
| 311 |
def validate_api_key(api_key: str = Security(api_key_header)):
|
| 312 |
if api_key != API_KEY:
|
|
@@ -418,7 +444,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
|
|
| 418 |
|
| 419 |
# 🔽 Mode selector
|
| 420 |
mode_selector = gr.Dropdown(
|
| 421 |
-
choices=["chatbotAssistance", "ID Extraction", "Vehicule Analyse", "Classification Plaintes"],
|
| 422 |
label="🎛️ Choisissez un mode", value="chatbotAssistance"
|
| 423 |
)
|
| 424 |
|
|
@@ -499,18 +525,39 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
|
|
| 499 |
classify_button = gr.Button("Classifier la plainte")
|
| 500 |
|
| 501 |
classify_button.click(fn=classify_complaint, inputs=[complaint_input], outputs=[complaint_output])
|
| 502 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
# 🔁 Fonction pour afficher le bon bloc
|
| 504 |
def toggle_mode(mode):
|
| 505 |
return (
|
| 506 |
gr.update(visible=mode == "chatbotAssistance"),
|
| 507 |
gr.update(visible=mode == "ID Extraction"),
|
|
|
|
| 508 |
gr.update(visible=mode == "Vehicule Analyse"),
|
| 509 |
gr.update(visible=mode == "Classification Plaintes")
|
| 510 |
)
|
| 511 |
|
| 512 |
-
mode_selector.change(fn=toggle_mode, inputs=[mode_selector], outputs=[chatbot_mode_block, id_extraction_block, car_detection_block, complaint_classification_block])
|
| 513 |
|
|
|
|
| 514 |
# 📨 Envoi pour le chatbot seulement
|
| 515 |
def chat_submit(msg, history, domain, model_name, max_tokens, temperature, top_p):
|
| 516 |
if not msg or not msg.strip():
|
|
|
|
| 307 |
response = chat.invoke(messages)
|
| 308 |
return response.content.strip()
|
| 309 |
|
| 310 |
+
|
| 311 |
+
# -- Function d'extraction des infos Facture ONEA
|
| 312 |
+
def extract_onea_facture_info(image):
|
| 313 |
+
try:
|
| 314 |
+
base64_img = image_to_base64(image)
|
| 315 |
+
messages = [
|
| 316 |
+
{
|
| 317 |
+
"role": "user",
|
| 318 |
+
"content": [
|
| 319 |
+
{"type": "text", "text": "Y a-t-il une facture ONEA ? Si oui analyse cette facture et extrait les informations principales et affiche en format JSON pour permettre de proceder au paiement.Si non indique juste à l'utilisateur d'uploader une image d'une facture ONEA beaucoup plus lisible."},
|
| 320 |
+
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_img}"}}
|
| 321 |
+
]
|
| 322 |
+
}
|
| 323 |
+
]
|
| 324 |
+
|
| 325 |
+
response = client.chat.completions.create(
|
| 326 |
+
model="gpt-4.1",
|
| 327 |
+
messages=messages,
|
| 328 |
+
max_tokens=500,
|
| 329 |
+
temperature=0
|
| 330 |
+
)
|
| 331 |
+
print(response)
|
| 332 |
+
return response.choices[0].message.content
|
| 333 |
+
|
| 334 |
+
except Exception as e:
|
| 335 |
+
return f"Erreur lors de l'extraction : {e}"
|
| 336 |
# -- API SECTION
|
| 337 |
def validate_api_key(api_key: str = Security(api_key_header)):
|
| 338 |
if api_key != API_KEY:
|
|
|
|
| 444 |
|
| 445 |
# 🔽 Mode selector
|
| 446 |
mode_selector = gr.Dropdown(
|
| 447 |
+
choices=["chatbotAssistance", "ID Extraction", "ONEA Extraction","Vehicule Analyse", "Classification Plaintes"],
|
| 448 |
label="🎛️ Choisissez un mode", value="chatbotAssistance"
|
| 449 |
)
|
| 450 |
|
|
|
|
| 525 |
classify_button = gr.Button("Classifier la plainte")
|
| 526 |
|
| 527 |
classify_button.click(fn=classify_complaint, inputs=[complaint_input], outputs=[complaint_output])
|
| 528 |
+
|
| 529 |
+
# -- Bloc 5 - ONEA Extraction
|
| 530 |
+
with gr.Column(visible=False) as onea_extraction_block:
|
| 531 |
+
gr.Markdown("🪪 **Extraction d'informations de la carte d'identité à partir d'image**")
|
| 532 |
+
id_image_input = gr.File(label="Uploader image de facture ONEA", file_types=["image"], file_count="multiple")
|
| 533 |
+
id_output = gr.Textbox(label="Résultat", lines=6)
|
| 534 |
+
id_button = gr.Button("📤 Extraire les informations")
|
| 535 |
+
|
| 536 |
+
def extract_id_fn(files):
|
| 537 |
+
if not files:
|
| 538 |
+
return "❌ Aucun fichier reçu."
|
| 539 |
+
file_path = files[0]
|
| 540 |
+
|
| 541 |
+
# Ouvrir l'image avec PIL
|
| 542 |
+
image = Image.open(file_path)
|
| 543 |
+
|
| 544 |
+
# Appeler la fonction d'extraction
|
| 545 |
+
return extract_onea_facture_info(image)
|
| 546 |
+
|
| 547 |
+
id_button.click(fn=extract_onea_fn, inputs=[id_image_input], outputs=[id_output])
|
| 548 |
# 🔁 Fonction pour afficher le bon bloc
|
| 549 |
def toggle_mode(mode):
|
| 550 |
return (
|
| 551 |
gr.update(visible=mode == "chatbotAssistance"),
|
| 552 |
gr.update(visible=mode == "ID Extraction"),
|
| 553 |
+
gr.update(visible=mode == "ONEA Extraction"),
|
| 554 |
gr.update(visible=mode == "Vehicule Analyse"),
|
| 555 |
gr.update(visible=mode == "Classification Plaintes")
|
| 556 |
)
|
| 557 |
|
| 558 |
+
mode_selector.change(fn=toggle_mode, inputs=[mode_selector], outputs=[chatbot_mode_block, id_extraction_block, onea_extraction_block, car_detection_block, complaint_classification_block])
|
| 559 |
|
| 560 |
+
|
| 561 |
# 📨 Envoi pour le chatbot seulement
|
| 562 |
def chat_submit(msg, history, domain, model_name, max_tokens, temperature, top_p):
|
| 563 |
if not msg or not msg.strip():
|