Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,66 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
#
|
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
|
19 |
-
sentiment = pipeline("sentiment-analysis",
|
|
|
20 |
return sentiment
|
21 |
|
22 |
def get_classifier():
|
23 |
global classifier
|
24 |
-
if classifier
|
25 |
-
classifier = pipeline("text-classification",
|
|
|
26 |
return classifier
|
27 |
|
28 |
def get_ner():
|
29 |
global ner
|
30 |
-
if ner
|
31 |
-
ner = pipeline("ner",
|
|
|
|
|
32 |
return ner
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def process(text, features):
|
36 |
-
|
37 |
-
results = {}
|
38 |
if "Summarization" in features:
|
39 |
-
|
40 |
-
|
|
|
41 |
if "Sentiment" in features:
|
42 |
sent = get_sentiment()(text)[0]
|
43 |
-
|
44 |
if "Classification" in features:
|
45 |
cls = get_classifier()(text)[0]
|
46 |
-
|
47 |
if "Entities" in features:
|
48 |
ents = get_ner()(text)
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
-
# 3️⃣ Build the Gradio Blocks UI
|
54 |
with gr.Blocks() as demo:
|
55 |
-
gr.Markdown("##
|
56 |
-
|
57 |
-
|
58 |
-
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Lazy‑load pipelines
|
5 |
+
sentiment = classifier = ner = summarizer = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def get_sentiment():
|
8 |
global sentiment
|
9 |
+
if not sentiment:
|
10 |
+
sentiment = pipeline("sentiment-analysis",
|
11 |
+
model="distilbert-base-uncased-finetuned-sst-2-english")
|
12 |
return sentiment
|
13 |
|
14 |
def get_classifier():
|
15 |
global classifier
|
16 |
+
if not classifier:
|
17 |
+
classifier = pipeline("text-classification",
|
18 |
+
model="textattack/distilbert-base-uncased-ag-news")
|
19 |
return classifier
|
20 |
|
21 |
def get_ner():
|
22 |
global ner
|
23 |
+
if not ner:
|
24 |
+
ner = pipeline("ner",
|
25 |
+
model="elastic/distilbert-base-uncased-finetuned-conll03-english",
|
26 |
+
aggregation_strategy="simple")
|
27 |
return ner
|
28 |
|
29 |
+
def get_summarizer():
|
30 |
+
global summarizer
|
31 |
+
if not summarizer:
|
32 |
+
summarizer = pipeline("summarization",
|
33 |
+
model="Curative/t5-summarizer-cnn")
|
34 |
+
return summarizer
|
35 |
+
|
36 |
def process(text, features):
|
37 |
+
result = {}
|
|
|
38 |
if "Summarization" in features:
|
39 |
+
result["summary"] = get_summarizer()(
|
40 |
+
text, max_length=150, min_length=40, do_sample=False
|
41 |
+
)[0]["summary_text"]
|
42 |
if "Sentiment" in features:
|
43 |
sent = get_sentiment()(text)[0]
|
44 |
+
result["sentiment"] = {"label": sent["label"], "score": sent["score"]}
|
45 |
if "Classification" in features:
|
46 |
cls = get_classifier()(text)[0]
|
47 |
+
result["classification"] = {"label": cls["label"], "score": cls["score"]}
|
48 |
if "Entities" in features:
|
49 |
ents = get_ner()(text)
|
50 |
+
result["entities"] = [
|
51 |
+
{"word": e["word"], "type": e["entity_group"]} for e in ents
|
52 |
+
]
|
53 |
+
return result
|
54 |
|
|
|
55 |
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("## 🛠️ Multi‑Feature NLP Service")
|
57 |
+
inp = gr.Textbox(lines=6, placeholder="Enter your text here…")
|
58 |
+
feats = gr.CheckboxGroup(
|
59 |
+
["Summarization","Sentiment","Classification","Entities"],
|
60 |
+
label="Select features to run"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
)
|
62 |
+
btn = gr.Button("Run")
|
63 |
+
out = gr.JSON(label="Results")
|
64 |
+
btn.click(process, [inp, feats], out)
|
65 |
|
|
|
66 |
demo.queue(api_open=True).launch()
|