Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|