Skander453 commited on
Commit
fd40321
Β·
verified Β·
1 Parent(s): 20f642c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -9
app.py CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
2
  import easyocr
3
  from transformers import pipeline
4
  import numpy as np
 
 
 
5
  from PIL import Image
6
 
7
  # Initialisation du lecteur EasyOCR (anglais par dΓ©faut)
@@ -46,10 +49,21 @@ def extraire_et_resumer(image):
46
 
47
  # RΓ©sumΓ©
48
  resume = summarizer(
49
- texte_extrait,
50
- max_length=max_length,
51
- min_length=min_length,
52
- do_sample=False
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
 
55
  texte_resume = resume[0]["summary_text"]
@@ -59,6 +73,34 @@ def extraire_et_resumer(image):
59
  except Exception as e:
60
  return f"Erreur : {str(e)}", "Impossible de gΓ©nΓ©rer le rΓ©sumΓ©."
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  # Interface Gradio
63
  with gr.Blocks(title="OCR & Text Summarizer") as demo:
64
  gr.Markdown("""
@@ -72,9 +114,11 @@ with gr.Blocks(title="OCR & Text Summarizer") as demo:
72
 
73
  with gr.Row():
74
  with gr.Column():
75
- image_input = gr.Image(
76
- type="pil",
77
- label="TΓ©lΓ©verser une image"
 
 
78
  )
79
  submit_btn = gr.Button("Extraire et rΓ©sumer", variant="primary")
80
 
@@ -89,11 +133,12 @@ with gr.Blocks(title="OCR & Text Summarizer") as demo:
89
  )
90
 
91
  submit_btn.click(
92
- fn=extraire_et_resumer,
93
- inputs=image_input,
94
  outputs=[sortie_texte, sortie_resume]
95
  )
96
 
 
97
  # Lancer l'application
98
  if __name__ == "__main__":
99
  demo.launch()
 
2
  import easyocr
3
  from transformers import pipeline
4
  import numpy as np
5
+ from docx import Document
6
+ from pptx import Presentation
7
+ import os
8
  from PIL import Image
9
 
10
  # Initialisation du lecteur EasyOCR (anglais par dΓ©faut)
 
49
 
50
  # RΓ©sumΓ©
51
  resume = summarizer(
52
+ if not text or len(text.split()) < 30:
53
+ return "Texte trop court pour Γͺtre rΓ©sumΓ©."
54
+
55
+ max_length = min(150, len(text.split()))
56
+ min_length = min(30, len(text.split()) // 2)
57
+
58
+ summary = summarizer(
59
+ text,
60
+ max_length=max_length,
61
+ min_length=min_length,
62
+ do_sample=False
63
+ )
64
+
65
+ return summary[0]["summary_text"]
66
+
67
  )
68
 
69
  texte_resume = resume[0]["summary_text"]
 
73
  except Exception as e:
74
  return f"Erreur : {str(e)}", "Impossible de gΓ©nΓ©rer le rΓ©sumΓ©."
75
 
76
+
77
+
78
+ def extract_text_from_file(file_path):
79
+ ext = os.path.splitext(file_path)[1].lower()
80
+
81
+ # TXT
82
+ if ext == ".txt":
83
+ with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
84
+ return f.read()
85
+
86
+ # WORD
87
+ elif ext == ".docx":
88
+ doc = Document(file_path)
89
+ return "\n".join([p.text for p in doc.paragraphs])
90
+
91
+ # POWERPOINT
92
+ elif ext == ".pptx":
93
+ prs = Presentation(file_path)
94
+ text = []
95
+ for slide in prs.slides:
96
+ for shape in slide.shapes:
97
+ if hasattr(shape, "text"):
98
+ text.append(shape.text)
99
+ return "\n".join(text)
100
+
101
+ else:
102
+ return ""
103
+
104
  # Interface Gradio
105
  with gr.Blocks(title="OCR & Text Summarizer") as demo:
106
  gr.Markdown("""
 
114
 
115
  with gr.Row():
116
  with gr.Column():
117
+ file_input = gr.File(
118
+ label="TΓ©lΓ©verser un fichier (TXT, DOCX, PPTX)",
119
+ file_types=[".txt", ".docx", ".pptx"]
120
+ )
121
+
122
  )
123
  submit_btn = gr.Button("Extraire et rΓ©sumer", variant="primary")
124
 
 
133
  )
134
 
135
  submit_btn.click(
136
+ fn=process_file,
137
+ inputs=file_input,
138
  outputs=[sortie_texte, sortie_resume]
139
  )
140
 
141
+
142
  # Lancer l'application
143
  if __name__ == "__main__":
144
  demo.launch()