File size: 1,714 Bytes
28d03d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
import gradio as gr

# Load sentiment and NER pipelines
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
ner_tagger = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")

# Supported translation models
translation_models = {
    "Hindi": "Helsinki-NLP/opus-mt-en-hi",
    "French": "Helsinki-NLP/opus-mt-en-fr"
}

# Load translation pipelines
translation_pipelines = {
    lang: pipeline("translation", model=model_id)
    for lang, model_id in translation_models.items()
}

def nlp_assistant(sentence, language):
    sentiment_result = sentiment_analyzer(sentence)[0]
    sentiment = f"{sentiment_result['label']} (Confidence: {sentiment_result['score']:.2f})"

    ner_result = ner_tagger(sentence)
    named_entities = "\n".join([f"{ent['word']} ({ent['entity_group']})" for ent in ner_result]) if ner_result else "No named entities found."

    translator = translation_pipelines[language]
    translation = translator(sentence)[0]['translation_text']

    return sentiment, named_entities, translation

iface = gr.Interface(
    fn=nlp_assistant,
    inputs=[
        gr.Textbox(lines=2, label="Enter an English sentence"),
        gr.Dropdown(choices=list(translation_models.keys()), label="Choose Translation Language")
    ],
    outputs=[
        gr.Textbox(label="🧠 Sentiment Analysis"),
        gr.Textbox(label="πŸ” Named Entity Recognition"),
        gr.Textbox(label="🌍 Translation")
    ],
    title="🧠 Mini NLP AI Assistant",
    description="Analyze sentiment, detect named entities, and translate to Hindi or French using Hugging Face Transformers."
)

iface.launch()