drshabbirhassan commited on
Commit
83a3362
·
verified ·
1 Parent(s): e740368

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -66
app.py CHANGED
@@ -1,78 +1,77 @@
1
- # app.py — CSS LexiScan MVP (Hugging Face Space)
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
- # Seed idioms dataset
15
- idioms_dataset = [
16
- "break the ice", "spill the beans", "once in a blue moon",
17
- "at the drop of a hat", "bark up the wrong tree",
18
- "come up with", "look forward to"
19
- ]
20
 
21
- # Function to extract difficult vocabulary
22
- def extract_vocab(text, n=7):
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
- # Function to detect idioms
29
- def detect_idioms(text):
30
- found = []
31
- doc = nlp(text)
32
- sentences = [sent.text.strip() for sent in doc.sents]
33
- for idiom in idioms_dataset:
34
- for sent in sentences:
35
- if fuzz.partial_ratio(idiom.lower(), sent.lower()) > 80:
36
- found.append(idiom)
37
- return list(set(found))
38
 
39
- # Simple CSS-style précis (first 3 sentences)
40
- def generate_precis(text):
41
- doc = nlp(text)
42
- sentences = [sent.text.strip() for sent in doc.sents]
43
- return " ".join(sentences[:3])
 
 
 
 
44
 
45
- # Main function to process uploaded image
46
- def process_input(image):
47
- text = " ".join(reader.readtext(image, detail=0))
48
- precis = generate_precis(text)
49
- vocab_list = extract_vocab(text)
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
- # Gradio UI
58
- with gr.Blocks() as demo:
59
- gr.Markdown("# 📰 CSS LexiScan — AI-Powered Editorial Analysis")
60
- gr.Markdown("Upload a newspaper screenshot to get CSS-style précis, difficult vocabulary, and idioms detected.")
61
 
62
- with gr.Row():
63
- with gr.Column():
64
- gr.Markdown("### ✍️ CSS-style Précis")
65
- precis_box = gr.Textbox(lines=12, placeholder="Generated CSS-style précis will appear here...")
66
- with gr.Column():
67
- gr.Markdown("### 📚 Difficult Vocabulary")
68
- vocab_box = gr.Textbox(lines=8, placeholder="Difficult words will appear here...")
69
- with gr.Column():
70
- gr.Markdown("### 💡 Idioms Detected")
71
- idioms_box = gr.Textbox(lines=8, placeholder="Idioms will appear here...")
72
 
73
- img_input = gr.Image(type="filepath")
74
- submit_btn = gr.Button("Analyze Article 📝")
 
 
 
75
 
76
- submit_btn.click(process_input, inputs=img_input, outputs=[precis_box, vocab_box, idioms_box])
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- demo.launch()
 
 
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()