File size: 1,603 Bytes
1f0565a
 
 
 
 
b3daaa7
 
 
 
 
 
 
1f0565a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3daaa7
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
49
50
51
52
53
54
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()