Spaces:
Sleeping
Sleeping
Add gradio
Browse files- app.py +103 -0
- requirements.txt +13 -0
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import pytesseract
|
4 |
+
from PIL import Image, UnidentifiedImageError
|
5 |
+
import re
|
6 |
+
import os
|
7 |
+
import logging
|
8 |
+
|
9 |
+
# Configurer les répertoires de cache
|
10 |
+
os.environ['TRANSFORMERS_CACHE'] = '/app/.cache'
|
11 |
+
os.environ['HF_HOME'] = '/app/.cache'
|
12 |
+
|
13 |
+
# Configurer les logs
|
14 |
+
logging.basicConfig(level=logging.INFO)
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
# Initialiser les pipelines
|
18 |
+
summarize = pipeline('summarization', model="facebook/bart-large-cnn")
|
19 |
+
pipe = pipeline("summarization", model="plguillou/t5-base-fr-sum-cnndm")
|
20 |
+
classify_zero_shot = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
21 |
+
|
22 |
+
# Fonction de résumé de texte avec classification
|
23 |
+
def summarize_text(text):
|
24 |
+
if text.strip() == "":
|
25 |
+
return "Veuillez entrer un texte", {}
|
26 |
+
|
27 |
+
preprocessing_text = re.sub(r'\s+', ' ', text).strip()
|
28 |
+
summary = pipe(preprocessing_text, do_sample=False)
|
29 |
+
summary_text = summary[0].get('summary_text')
|
30 |
+
|
31 |
+
logger.info(f"[INFO] Input data: {preprocessing_text}")
|
32 |
+
logger.info(f"[INFO] Summary: {summary_text}")
|
33 |
+
|
34 |
+
result = classify_zero_shot(
|
35 |
+
summary_text,
|
36 |
+
candidate_labels=["En Cours", "Non traiter", "Terminer"],
|
37 |
+
hypothesis_template="Cet Résumé est sur {}."
|
38 |
+
)
|
39 |
+
|
40 |
+
scores = {label: float(score) for label, score in zip(result['labels'], result['scores'])}
|
41 |
+
|
42 |
+
return summary_text, scores
|
43 |
+
|
44 |
+
# Fonction de chargement d'image
|
45 |
+
def image_load(image):
|
46 |
+
try:
|
47 |
+
if image is None:
|
48 |
+
return "Aucune image fournie", {}
|
49 |
+
|
50 |
+
raw_text = pytesseract.image_to_string(image, lang='fra')
|
51 |
+
preprocessing = re.sub(r'\s+', ' ', raw_text).strip()
|
52 |
+
text_summary = pipe(preprocessing, do_sample=False)
|
53 |
+
summary_text_from_image = text_summary[0].get('summary_text')
|
54 |
+
result = classify_zero_shot(
|
55 |
+
summary_text_from_image,
|
56 |
+
candidate_labels=["En Cours", "Non traiter", "Terminer"],
|
57 |
+
hypothesis_template="Cet Résumé est sur {}."
|
58 |
+
)
|
59 |
+
scores = {label: float(score) for label, score in zip(result['labels'], result['scores'])}
|
60 |
+
logger.info(f"[INFO] Input data: {preprocessing}")
|
61 |
+
logger.info(f"[INFO] Summary: {result}")
|
62 |
+
return summary_text_from_image,scores
|
63 |
+
except UnidentifiedImageError:
|
64 |
+
return "Impossible de charger l'image", {}
|
65 |
+
except Exception as e:
|
66 |
+
logger.error(f"Error processing image: {e}")
|
67 |
+
return str(e), {}
|
68 |
+
|
69 |
+
# Fonction de gestion des entrées
|
70 |
+
def handle_input(text_input, image_input, mode):
|
71 |
+
if mode == "Texte":
|
72 |
+
return summarize_text(text_input)
|
73 |
+
elif mode == "Image":
|
74 |
+
return image_load(image_input)
|
75 |
+
else:
|
76 |
+
return "Sélectionnez une option valide", {}
|
77 |
+
|
78 |
+
# Interface Gradio
|
79 |
+
with gr.Blocks() as iface:
|
80 |
+
gr.Markdown("## Sélectionnez une option")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column():
|
84 |
+
mode = gr.Dropdown(choices=["Texte", "Image"], label="Resumé Texte ou Image",info="Selectionner une options")
|
85 |
+
text_input = gr.Textbox(lines=4,label="Entrée de texte")
|
86 |
+
image_input = gr.Image(label="Téléverser une image", type="pil")
|
87 |
+
submit_btn = gr.Button("Soumettre")
|
88 |
+
|
89 |
+
with gr.Column():
|
90 |
+
output_summary = gr.Textbox(label="Résumé")
|
91 |
+
output_classification = gr.Label(label="Classification")
|
92 |
+
|
93 |
+
def update_inputs(mode_select):
|
94 |
+
if mode_select == "Texte":
|
95 |
+
return gr.update(visible=True), gr.update(visible=False)
|
96 |
+
elif mode_select == "Image":
|
97 |
+
return gr.update(visible=False), gr.update(visible=True)
|
98 |
+
logger.info(f"[INFO] input mode: {update_inputs}")
|
99 |
+
mode.change(fn=update_inputs, inputs=mode, outputs=[text_input, image_input])
|
100 |
+
submit_btn.click(fn=handle_input, inputs=[text_input, image_input, mode], outputs=[output_summary, output_classification])
|
101 |
+
|
102 |
+
if __name__ == "__main__":
|
103 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.3.1
|
2 |
+
transformers==4.41.2
|
3 |
+
gradio==4.37.2
|
4 |
+
numpy==1.23.5
|
5 |
+
scipy==1.11.3
|
6 |
+
black
|
7 |
+
pytest
|
8 |
+
scikit-learn
|
9 |
+
sentencepiece==0.2.0
|
10 |
+
PyMuPDF==1.24.5
|
11 |
+
pytesseract==0.3.10
|
12 |
+
pillow==10.3.0
|
13 |
+
protobuf
|