File size: 2,102 Bytes
e56279f
 
 
950842b
 
e56279f
fc959be
 
950842b
 
 
fc959be
 
 
 
950842b
 
 
fc959be
 
 
 
950842b
 
 
 
fc959be
 
950842b
 
 
 
 
 
 
fc959be
950842b
fc959be
950842b
 
 
fc959be
 
950842b
fc959be
 
950842b
fc959be
 
950842b
 
 
 
fc959be
 
950842b
 
 
 
 
fc959be
950842b
 
 
fc959be
 
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
import gradio as gr
from transformers import pipeline

# Lazy‑load pipelines
sentiment = classifier = ner = summarizer = None

def get_sentiment():
    global sentiment
    if not sentiment:
        sentiment = pipeline("sentiment-analysis",
            model="distilbert-base-uncased-finetuned-sst-2-english")
    return sentiment

def get_classifier():
    global classifier
    if not classifier:
        classifier = pipeline("text-classification",
            model="textattack/distilbert-base-uncased-ag-news")
    return classifier

def get_ner():
    global ner
    if not ner:
        ner = pipeline("ner",
            model="elastic/distilbert-base-uncased-finetuned-conll03-english",
            aggregation_strategy="simple")
    return ner

def get_summarizer():
    global summarizer
    if not summarizer:
        summarizer = pipeline("summarization",
            model="Curative/t5-summarizer-cnn")
    return summarizer

def process(text, features):
    result = {}
    if "Summarization" in features:
        result["summary"] = get_summarizer()(
            text, max_length=150, min_length=40, do_sample=False
        )[0]["summary_text"]
    if "Sentiment" in features:
        sent = get_sentiment()(text)[0]
        result["sentiment"] = {"label": sent["label"], "score": sent["score"]}
    if "Classification" in features:
        cls = get_classifier()(text)[0]
        result["classification"] = {"label": cls["label"], "score": cls["score"]}
    if "Entities" in features:
        ents = get_ner()(text)
        result["entities"] = [
            {"word": e["word"], "type": e["entity_group"]} for e in ents
        ]
    return result

with gr.Blocks() as demo:
    gr.Markdown("## 🛠️ Multi‑Feature NLP Service")
    inp = gr.Textbox(lines=6, placeholder="Enter your text here…")
    feats = gr.CheckboxGroup(
        ["Summarization","Sentiment","Classification","Entities"],
        label="Select features to run"
    )
    btn = gr.Button("Run")
    out = gr.JSON(label="Results")
    btn.click(process, [inp, feats], out)

demo.queue(api_open=True).launch()