Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# 1️⃣ Lazy‑load your pipelines
|
| 5 |
+
summarizer = None
|
| 6 |
+
sentiment = None
|
| 7 |
+
classifier = None
|
| 8 |
+
ner = None
|
| 9 |
|
| 10 |
+
def get_summarizer():
|
| 11 |
+
global summarizer
|
| 12 |
+
if summarizer is None:
|
| 13 |
+
summarizer = pipeline("summarization", model="Curative/t5-summarizer-cnn")
|
| 14 |
+
return summarizer
|
| 15 |
|
| 16 |
+
def get_sentiment():
|
| 17 |
+
global sentiment
|
| 18 |
+
if sentiment is None:
|
| 19 |
+
sentiment = pipeline("sentiment-analysis", model="DT12the/distilbert-sentiment-analysis")
|
| 20 |
+
return sentiment
|
| 21 |
+
|
| 22 |
+
def get_classifier():
|
| 23 |
+
global classifier
|
| 24 |
+
if classifier is None:
|
| 25 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 26 |
+
return classifier
|
| 27 |
+
|
| 28 |
+
def get_ner():
|
| 29 |
+
global ner
|
| 30 |
+
if ner is None:
|
| 31 |
+
ner = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
| 32 |
+
return ner
|
| 33 |
+
|
| 34 |
+
# 2️⃣ Processing function
|
| 35 |
+
def process(text, features):
|
| 36 |
+
"""Run only the selected features on the input text."""
|
| 37 |
+
results = {}
|
| 38 |
+
if "Summarization" in features:
|
| 39 |
+
summ = get_summarizer()(text, max_length=150, min_length=40, do_sample=False)[0]["summary_text"]
|
| 40 |
+
results["summary"] = summ
|
| 41 |
+
if "Sentiment" in features:
|
| 42 |
+
sent = get_sentiment()(text)[0]
|
| 43 |
+
results["sentiment"] = sent
|
| 44 |
+
if "Classification" in features:
|
| 45 |
+
cls = get_classifier()(text)[0]
|
| 46 |
+
results["classification"] = cls
|
| 47 |
+
if "Entities" in features:
|
| 48 |
+
ents = get_ner()(text)
|
| 49 |
+
# Format entities as list of dicts
|
| 50 |
+
results["entities"] = [{"word": e["word"], "type": e["entity_group"]} for e in ents]
|
| 51 |
+
return results
|
| 52 |
+
|
| 53 |
+
# 3️⃣ Build the Gradio Blocks UI
|
| 54 |
+
with gr.Blocks() as demo:
|
| 55 |
+
gr.Markdown("## 📚 Multi‑Feature NLP Demo")
|
| 56 |
+
text_input = gr.Textbox(lines=5, placeholder="Enter your text here…")
|
| 57 |
+
feature_select = gr.CheckboxGroup(
|
| 58 |
+
choices=["Summarization", "Sentiment", "Classification", "Entities"],
|
| 59 |
+
label="Select features to run",
|
| 60 |
+
info="You can pick one or more models to apply"
|
| 61 |
+
)
|
| 62 |
+
run_button = gr.Button("Run")
|
| 63 |
+
output = gr.JSON(label="Results")
|
| 64 |
+
|
| 65 |
+
run_button.click(
|
| 66 |
+
fn=process,
|
| 67 |
+
inputs=[text_input, feature_select],
|
| 68 |
+
outputs=output
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# 4️⃣ Launch with API enabled
|
| 72 |
+
demo.queue(api_open=True).launch()
|