NLP_Assistant / app.py
JoelJebaraj93's picture
Update app.py
b3daaa7 verified
import spacy
from textblob import TextBlob
from deep_translator import GoogleTranslator
import gradio as gr
# Load spaCy model
try:
nlp = spacy.load("en_core_web_sm")
except:
import os
os.system("python -m spacy download en_core_web_sm")
nlp = spacy.load("en_core_web_sm")
def nlp_assistant(text, language):
result = ""
# Sentiment
blob = TextBlob(text)
polarity = blob.sentiment.polarity
sentiment = "Positive" if polarity > 0 else "Negative" if polarity < 0 else "Neutral"
confidence = abs(polarity) * 100
result += f"💬 Sentiment: **{sentiment}** ({confidence:.2f}% confidence)\n\n"
# NER
doc = nlp(text)
ner_output = [f"➡️ {ent.text} ({ent.label_})" for ent in doc.ents]
if ner_output:
result += "🧾 Named Entities:\n" + "\n".join(ner_output) + "\n\n"
else:
result += "🧾 Named Entities:\n❌ None found.\n\n"
# Translation
try:
translated = GoogleTranslator(source='auto', target=language).translate(text)
result += f"🌐 Translated to `{language.upper()}`:\n➡️ {translated}"
except Exception as e:
result += f"🌐 Translation error: {str(e)}"
return result
interface = gr.Interface(
fn=nlp_assistant,
inputs=[
gr.Textbox(label="Enter a sentence"),
gr.Radio(["ta", "hi", "fr"], label="Translate to (Tamil/Hindi/French)")
],
outputs=gr.Textbox(label="NLP Result"),
title="🧠 Mini NLP Assistant",
description="Performs Sentiment, NER & Translation using spaCy, TextBlob, and Deep Translator"
)
interface.launch()