Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,77 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import spacy
|
| 5 |
from fuzzywuzzy import fuzz
|
| 6 |
-
from easyocr import Reader
|
| 7 |
-
|
| 8 |
-
# Load SpaCy small English model
|
| 9 |
-
nlp = spacy.load("en_core_web_sm")
|
| 10 |
-
|
| 11 |
-
# Initialize EasyOCR reader for English
|
| 12 |
-
reader = Reader(lang_list=['en'])
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
]
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
doc = nlp(text)
|
| 24 |
-
candidates = [token.text for token in doc if token.is_alpha and not token.ent_type_ and len(token.text) > 5]
|
| 25 |
-
unique_words = list(dict.fromkeys(candidates))
|
| 26 |
-
return unique_words[:n]
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
found.append(idiom)
|
| 37 |
-
return list(set(found))
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
idioms_found = detect_idioms(text)
|
| 51 |
-
return (
|
| 52 |
-
precis,
|
| 53 |
-
"\n".join(f"- {w}" for w in vocab_list),
|
| 54 |
-
"\n".join(f"- {i}" for i in idioms_found)
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
idioms_box = gr.Textbox(lines=8, placeholder="Idioms will appear here...")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import easyocr
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from fuzzywuzzy import fuzz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# -------------------------
|
| 7 |
+
# SETTINGS
|
| 8 |
+
# -------------------------
|
| 9 |
+
# Since images are directly in root, no folder
|
| 10 |
+
images_folder = "./"
|
|
|
|
| 11 |
|
| 12 |
+
# Initialize EasyOCR reader
|
| 13 |
+
reader = easyocr.Reader(['en'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# -------------------------
|
| 16 |
+
# HELPER FUNCTIONS
|
| 17 |
+
# -------------------------
|
| 18 |
+
def extract_text_from_image(image_path):
|
| 19 |
+
"""OCR the image and return full text"""
|
| 20 |
+
result = reader.readtext(image_path)
|
| 21 |
+
full_text = " ".join([res[1] for res in result])
|
| 22 |
+
return full_text
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
def get_difficult_words(text, top_n=7):
|
| 25 |
+
"""Simple heuristic for difficult words"""
|
| 26 |
+
words = [w.strip('.,!?()') for w in text.split()]
|
| 27 |
+
words = [w for w in words if len(w) > 5 and w.isalpha()]
|
| 28 |
+
freq = {}
|
| 29 |
+
for w in words:
|
| 30 |
+
freq[w] = freq.get(w, 0) + 1
|
| 31 |
+
sorted_words = sorted(freq, key=freq.get, reverse=True)
|
| 32 |
+
return sorted_words[:top_n]
|
| 33 |
|
| 34 |
+
def get_idioms(text):
|
| 35 |
+
"""Dummy idiom detection (replace with your dataset later)"""
|
| 36 |
+
seed_idioms = ["kick the bucket", "break the ice", "spill the beans"]
|
| 37 |
+
found = [i for i in seed_idioms if i in text.lower()]
|
| 38 |
+
return found
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def generate_précis(text):
|
| 41 |
+
"""Extractive CSS-style précis (first 1/3 of text)"""
|
| 42 |
+
one_third_len = max(1, len(text)//3)
|
| 43 |
+
return text[:one_third_len]
|
| 44 |
|
| 45 |
+
# -------------------------
|
| 46 |
+
# MAIN FUNCTION
|
| 47 |
+
# -------------------------
|
| 48 |
+
def process_image(image):
|
| 49 |
+
text = extract_text_from_image(image)
|
| 50 |
+
vocab = get_difficult_words(text)
|
| 51 |
+
idioms = get_idioms(text)
|
| 52 |
+
precis = generate_précis(text)
|
| 53 |
+
return precis, ", ".join(vocab), ", ".join(idioms)
|
|
|
|
| 54 |
|
| 55 |
+
# -------------------------
|
| 56 |
+
# OPTIONAL: List of existing images in root
|
| 57 |
+
# -------------------------
|
| 58 |
+
existing_images = [f for f in os.listdir(images_folder)
|
| 59 |
+
if f.lower().endswith((".png", ".jpg", ".jpeg"))]
|
| 60 |
|
| 61 |
+
# -------------------------
|
| 62 |
+
# GRADIO INTERFACE
|
| 63 |
+
# -------------------------
|
| 64 |
+
demo = gr.Interface(
|
| 65 |
+
fn=process_image,
|
| 66 |
+
inputs=gr.Image(type="filepath", label="Upload an image or select one from root"),
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.Textbox(label="CSS-Style Précis"),
|
| 69 |
+
gr.Textbox(label="Difficult Words"),
|
| 70 |
+
gr.Textbox(label="Idioms / Phrasal Expressions")
|
| 71 |
+
],
|
| 72 |
+
title="CSS LexiScan MVP",
|
| 73 |
+
description="Upload newspaper images to get grounded CSS-style précis, vocabulary, and idioms."
|
| 74 |
+
)
|
| 75 |
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
demo.launch()
|