Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load 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", grouped_entities=True)
|
| 7 |
+
|
| 8 |
+
# Translation models for different languages
|
| 9 |
+
translation_models = {
|
| 10 |
+
"French": "Helsinki-NLP/opus-mt-en-fr",
|
| 11 |
+
"German": "Helsinki-NLP/opus-mt-en-de",
|
| 12 |
+
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
| 13 |
+
"Hindi": "Helsinki-NLP/opus-mt-en-hi",
|
| 14 |
+
"Tamil": "ItchyChin/OrpoLlama-3-8B-memorize-translate-tamil-20241009",
|
| 15 |
+
"Japanese": "Helsinki-NLP/opus-mt-en-ja",
|
| 16 |
+
"Chinese": "Helsinki-NLP/opus-mt-en-zh",
|
| 17 |
+
"Russian": "Helsinki-NLP/opus-mt-en-ru",
|
| 18 |
+
"Arabic": "Helsinki-NLP/opus-mt-en-ar"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Main function
|
| 22 |
+
def analyze_text(sentence, target_language):
|
| 23 |
+
# Perform Sentiment Analysis
|
| 24 |
+
sentiment = sentiment_analyzer(sentence)
|
| 25 |
+
|
| 26 |
+
# Perform Named Entity Recognition
|
| 27 |
+
ner = ner_tagger(sentence)
|
| 28 |
+
|
| 29 |
+
# Perform Translation
|
| 30 |
+
model_id = translation_models[target_language]
|
| 31 |
+
translator = pipeline("translation", model=model_id)
|
| 32 |
+
translated = translator(sentence, max_length=40)[0]['translation_text']
|
| 33 |
+
|
| 34 |
+
return sentiment, ner, translated
|
| 35 |
+
|
| 36 |
+
# Gradio UI
|
| 37 |
+
interface = gr.Interface(
|
| 38 |
+
fn=analyze_text,
|
| 39 |
+
inputs=[
|
| 40 |
+
gr.Textbox(label="Enter an English Sentence"),
|
| 41 |
+
gr.Dropdown(choices=list(translation_models.keys()), label="Translate to Language")
|
| 42 |
+
],
|
| 43 |
+
outputs=[
|
| 44 |
+
gr.JSON(label="Sentiment Analysis"),
|
| 45 |
+
gr.JSON(label="Named Entities"),
|
| 46 |
+
gr.Textbox(label="Translation Result")
|
| 47 |
+
],
|
| 48 |
+
title="🌍 NLP Translator + NER + Sentiment",
|
| 49 |
+
description="This tool analyzes a sentence for sentiment, named entities, and translates it into a chosen language using Hugging Face Transformers."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
interface.launch()
|