File size: 2,394 Bytes
83a3362
 
ef2ff05
 
 
83a3362
 
 
 
 
ef2ff05
83a3362
 
ef2ff05
83a3362
 
 
 
 
 
 
 
ef2ff05
83a3362
 
 
 
 
 
 
 
 
ef2ff05
83a3362
 
 
 
 
ef2ff05
83a3362
 
 
 
ef2ff05
83a3362
 
 
 
 
 
 
 
 
ef2ff05
83a3362
 
 
 
 
ef2ff05
83a3362
 
 
 
 
 
 
 
 
 
 
 
 
 
ef2ff05
83a3362
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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()