import os import easyocr import gradio as gr from fuzzywuzzy import fuzz # ------------------------- # SETTINGS # ------------------------- # Since images are directly in root, no folder images_folder = "./" # Initialize EasyOCR reader reader = easyocr.Reader(['en']) # ------------------------- # HELPER FUNCTIONS # ------------------------- def extract_text_from_image(image_path): """OCR the image and return full text""" result = reader.readtext(image_path) full_text = " ".join([res[1] for res in result]) return full_text def get_difficult_words(text, top_n=7): """Simple heuristic for difficult words""" words = [w.strip('.,!?()') for w in text.split()] words = [w for w in words if len(w) > 5 and w.isalpha()] freq = {} for w in words: freq[w] = freq.get(w, 0) + 1 sorted_words = sorted(freq, key=freq.get, reverse=True) return sorted_words[:top_n] def get_idioms(text): """Dummy idiom detection (replace with your dataset later)""" seed_idioms = ["kick the bucket", "break the ice", "spill the beans"] found = [i for i in seed_idioms if i in text.lower()] return found def generate_précis(text): """Extractive CSS-style précis (first 1/3 of text)""" one_third_len = max(1, len(text)//3) return text[:one_third_len] # ------------------------- # MAIN FUNCTION # ------------------------- def process_image(image): text = extract_text_from_image(image) vocab = get_difficult_words(text) idioms = get_idioms(text) precis = generate_précis(text) return precis, ", ".join(vocab), ", ".join(idioms) # ------------------------- # OPTIONAL: List of existing images in root # ------------------------- existing_images = [f for f in os.listdir(images_folder) if f.lower().endswith((".png", ".jpg", ".jpeg"))] # ------------------------- # GRADIO INTERFACE # ------------------------- demo = gr.Interface( fn=process_image, inputs=gr.Image(type="filepath", label="Upload an image or select one from root"), outputs=[ gr.Textbox(label="CSS-Style Précis"), gr.Textbox(label="Difficult Words"), gr.Textbox(label="Idioms / Phrasal Expressions") ], title="CSS LexiScan MVP", description="Upload newspaper images to get grounded CSS-style précis, vocabulary, and idioms." ) if __name__ == "__main__": demo.launch()