Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Clé API OpenAI
|
| 6 |
+
openai.api_key = "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"
|
| 7 |
+
|
| 8 |
+
# Schéma JSON à respecter
|
| 9 |
+
extraction_schema = {
|
| 10 |
+
"name": "extract_user_info",
|
| 11 |
+
"description": "Extrait des informations personnelles depuis un texte libre pour remplir un formulaire administratif",
|
| 12 |
+
"parameters": {
|
| 13 |
+
"type": "object",
|
| 14 |
+
"properties": {
|
| 15 |
+
"nom": {"type": "string"},
|
| 16 |
+
"prenom": {"type": "string"},
|
| 17 |
+
"date_naissance": {"type": "string", "description": "au format YYYY-MM-DD"},
|
| 18 |
+
"lieu_naissance": {"type": "string"},
|
| 19 |
+
"nationalite": {"type": "string"},
|
| 20 |
+
"adresse": {
|
| 21 |
+
"type": "object",
|
| 22 |
+
"properties": {
|
| 23 |
+
"numero": {"type": "string"},
|
| 24 |
+
"rue": {"type": "string"},
|
| 25 |
+
"batiment": {"type": "string"},
|
| 26 |
+
"appartement": {"type": "string"},
|
| 27 |
+
"code_postal": {"type": "string"},
|
| 28 |
+
"ville": {"type": "string"}
|
| 29 |
+
}
|
| 30 |
+
},
|
| 31 |
+
"email": {"type": "string"},
|
| 32 |
+
"telephone": {"type": "string"},
|
| 33 |
+
"situation_familiale": {"type": "string"},
|
| 34 |
+
"nombre_enfants": {"type": "integer"},
|
| 35 |
+
"emploi": {"type": "string"},
|
| 36 |
+
"nom_employeur": {"type": "string"},
|
| 37 |
+
"type_piece_identite": {"type": "string"},
|
| 38 |
+
"numero_piece_identite": {"type": "string"},
|
| 39 |
+
"date_delivrance_piece": {"type": "string"},
|
| 40 |
+
"lieu_delivrance_piece": {"type": "string"},
|
| 41 |
+
"situation_logement": {"type": "string"}
|
| 42 |
+
},
|
| 43 |
+
"required": ["nom", "prenom", "date_naissance", "adresse"]
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
def extraire_infos(texte):
|
| 48 |
+
try:
|
| 49 |
+
completion = openai.ChatCompletion.create(
|
| 50 |
+
model="gpt-4",
|
| 51 |
+
messages=[
|
| 52 |
+
{
|
| 53 |
+
"role": "system",
|
| 54 |
+
"content": "Tu es un assistant administratif. À partir d’un texte mal rédigé, tu dois extraire les informations personnelles sous forme de JSON selon un schéma bien défini."
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"role": "user",
|
| 58 |
+
"content": texte
|
| 59 |
+
}
|
| 60 |
+
],
|
| 61 |
+
functions=[extraction_schema],
|
| 62 |
+
function_call={"name": "extract_user_info"},
|
| 63 |
+
temperature=0
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
arguments = completion["choices"][0]["message"]["function_call"]["arguments"]
|
| 67 |
+
parsed = json.loads(arguments)
|
| 68 |
+
return json.dumps(parsed, indent=2, ensure_ascii=False)
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"❌ Erreur : {e}"
|
| 71 |
+
|
| 72 |
+
texte_exemple = """slt c chloe mart1, jsuis née le 17 avrile 88 a lion. jai 36an. mtn jhabit a toulouse ds un HLM, 12 r des lilas bat B appart 304 cp 31000. je sui celib mé g 2 enfant (tom 8an, jade 5an) avec mwa. taf a carfour market, caissiere depui 5 an en CDI. mon tel c 07 82 93 55 10, mon email c clairou point martin arobase gmail point com. carte identit numero 18AB99237, donné le 12 mars 2015 a lion. jsui françaiz"""
|
| 73 |
+
|
| 74 |
+
interface = gr.Interface(
|
| 75 |
+
fn=extraire_infos,
|
| 76 |
+
inputs=gr.Textbox(lines=14, label="Texte à corriger et structurer", value=texte_exemple),
|
| 77 |
+
outputs=gr.Code(label="Résultat JSON structuré"),
|
| 78 |
+
title="Extracteur intelligent de données CERFA",
|
| 79 |
+
description="Copie-colle un texte mal écrit contenant des infos personnelles. L'IA le transforme en JSON prêt pour l'administration."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
interface.launch()
|