Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +47 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load sentiment and NER pipelines
|
| 5 |
+
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 6 |
+
ner_tagger = pipeline("ner", model="dslim/bert-base-NER", aggregation_strategy="simple")
|
| 7 |
+
|
| 8 |
+
# Supported translation models
|
| 9 |
+
translation_models = {
|
| 10 |
+
"Hindi": "Helsinki-NLP/opus-mt-en-hi",
|
| 11 |
+
"French": "Helsinki-NLP/opus-mt-en-fr"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Load translation pipelines
|
| 15 |
+
translation_pipelines = {
|
| 16 |
+
lang: pipeline("translation", model=model_id)
|
| 17 |
+
for lang, model_id in translation_models.items()
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
def nlp_assistant(sentence, language):
|
| 21 |
+
sentiment_result = sentiment_analyzer(sentence)[0]
|
| 22 |
+
sentiment = f"{sentiment_result['label']} (Confidence: {sentiment_result['score']:.2f})"
|
| 23 |
+
|
| 24 |
+
ner_result = ner_tagger(sentence)
|
| 25 |
+
named_entities = "\n".join([f"{ent['word']} ({ent['entity_group']})" for ent in ner_result]) if ner_result else "No named entities found."
|
| 26 |
+
|
| 27 |
+
translator = translation_pipelines[language]
|
| 28 |
+
translation = translator(sentence)[0]['translation_text']
|
| 29 |
+
|
| 30 |
+
return sentiment, named_entities, translation
|
| 31 |
+
|
| 32 |
+
iface = gr.Interface(
|
| 33 |
+
fn=nlp_assistant,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Textbox(lines=2, label="Enter an English sentence"),
|
| 36 |
+
gr.Dropdown(choices=list(translation_models.keys()), label="Choose Translation Language")
|
| 37 |
+
],
|
| 38 |
+
outputs=[
|
| 39 |
+
gr.Textbox(label="🧠 Sentiment Analysis"),
|
| 40 |
+
gr.Textbox(label="🔍 Named Entity Recognition"),
|
| 41 |
+
gr.Textbox(label="🌍 Translation")
|
| 42 |
+
],
|
| 43 |
+
title="🧠 Mini NLP AI Assistant",
|
| 44 |
+
description="Analyze sentiment, detect named entities, and translate to Hindi or French using Hugging Face Transformers."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|