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()