Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
-
# Initialize
|
| 6 |
-
|
| 7 |
-
task="image-to-text",
|
| 8 |
-
model="nlpconnect/vit-gpt2-image-captioning"
|
| 9 |
-
)
|
| 10 |
|
| 11 |
-
def
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
for f in files:
|
| 18 |
-
# f may be a temporary file object or file path
|
| 19 |
path = f.name if hasattr(f, 'name') else f
|
| 20 |
img = Image.open(path).convert("RGB")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Build Gradio Blocks app
|
| 27 |
-
with gr.Blocks(title="
|
| 28 |
-
gr.Markdown("#
|
| 29 |
-
gr.Markdown(
|
| 30 |
-
"Upload one or more images below and click **Generate Alt Text** to get descriptive captions."
|
| 31 |
-
)
|
| 32 |
-
# Use File component to allow multiple image uploads
|
| 33 |
file_input = gr.File(
|
| 34 |
label="Upload Images",
|
| 35 |
file_count="multiple",
|
| 36 |
file_types=["image"]
|
| 37 |
)
|
| 38 |
-
# Textbox to display the generated alt texts
|
| 39 |
output_box = gr.Textbox(
|
| 40 |
-
label="
|
| 41 |
-
placeholder="
|
| 42 |
lines=10,
|
| 43 |
elem_id="output_box"
|
| 44 |
)
|
| 45 |
with gr.Row():
|
| 46 |
-
|
| 47 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
fn=generate_alt_text,
|
| 51 |
inputs=file_input,
|
| 52 |
outputs=output_box
|
| 53 |
)
|
| 54 |
-
# Copy to clipboard
|
| 55 |
copy_btn.click(
|
| 56 |
fn=None,
|
| 57 |
inputs=None,
|
|
@@ -59,6 +55,5 @@ with gr.Blocks(title="Multi-Image Alt Text Generator") as demo:
|
|
| 59 |
js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# Launch the app
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import easyocr
|
| 4 |
|
| 5 |
+
# Initialize EasyOCR reader (supports multiple languages; here English)
|
| 6 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def extract_text(files):
|
| 9 |
"""
|
| 10 |
+
Extracts text from each uploaded image file using EasyOCR.
|
| 11 |
+
Returns numbered list of detected text strings.
|
| 12 |
"""
|
| 13 |
+
results = []
|
| 14 |
for f in files:
|
|
|
|
| 15 |
path = f.name if hasattr(f, 'name') else f
|
| 16 |
img = Image.open(path).convert("RGB")
|
| 17 |
+
# Run OCR
|
| 18 |
+
ocr_results = reader.readtext(path, detail=0)
|
| 19 |
+
# Join text segments with spaces
|
| 20 |
+
text = ' '.join(ocr_results).strip()
|
| 21 |
+
if not text:
|
| 22 |
+
text = "<No text detected>"
|
| 23 |
+
results.append(text)
|
| 24 |
+
# Format as numbered list
|
| 25 |
+
return "\n".join([f"{i+1}. {t}" for i, t in enumerate(results)])
|
| 26 |
|
| 27 |
+
# Build Gradio Blocks app
|
| 28 |
+
with gr.Blocks(title="Image Text Extractor (OCR)") as demo:
|
| 29 |
+
gr.Markdown("# Accurate Text Extraction from Multiple Images")
|
| 30 |
+
gr.Markdown("Upload images and extract all visible text using EasyOCR.")
|
|
|
|
|
|
|
|
|
|
| 31 |
file_input = gr.File(
|
| 32 |
label="Upload Images",
|
| 33 |
file_count="multiple",
|
| 34 |
file_types=["image"]
|
| 35 |
)
|
|
|
|
| 36 |
output_box = gr.Textbox(
|
| 37 |
+
label="Extracted Text",
|
| 38 |
+
placeholder="Detected text will appear here...",
|
| 39 |
lines=10,
|
| 40 |
elem_id="output_box"
|
| 41 |
)
|
| 42 |
with gr.Row():
|
| 43 |
+
extract_btn = gr.Button("Extract Text")
|
| 44 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 45 |
+
extract_btn.click(
|
| 46 |
+
fn=extract_text,
|
|
|
|
| 47 |
inputs=file_input,
|
| 48 |
outputs=output_box
|
| 49 |
)
|
| 50 |
+
# Copy to clipboard
|
| 51 |
copy_btn.click(
|
| 52 |
fn=None,
|
| 53 |
inputs=None,
|
|
|
|
| 55 |
js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
|
| 56 |
)
|
| 57 |
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
demo.launch()
|