File size: 2,777 Bytes
2be76ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3f1c01
48b0ee3
b3f1c01
 
 
 
 
 
338abb1
cdf210b
 
2be76ff
cdf210b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2be76ff
cdf210b
 
 
 
 
 
 
 
 
 
2be76ff
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
55
56
import os
import gradio as gr

download="wget --load-cookies /tmp/cookies.txt \"https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1-hzy09qi-OEogyge7rQG79K7iV4xsNWa' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\\n/p')&id=1-hzy09qi-OEogyge7rQG79K7iV4xsNWa\" -O indic-en.zip && rm -rf /tmp/cookies.txt"
os.system(download)
os.system('unzip /home/user/app/indic-en.zip')

from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils
from inference.engine import Model
indic2en_model = Model(expdir='/home/user/app/indic-en')

INDIC = {"Assamese": "as", "Bengali": "bn", "Gujarati": "gu", "Hindi": "hi","Kannada": "kn","Malayalam": "ml", "Marathi": "mr", "Odia": "or","Punjabi": "pa","Tamil": "ta", "Telugu" : "te"}


def translate(text, lang):
  return indic2en_model.translate_paragraph(text, INDIC[lang], 'en')

from transformers import pipeline
import gradio as gr
roberta_pipe = pipeline(
    "sentiment-analysis",
    model="siebert/sentiment-roberta-large-english",
    tokenizer="siebert/sentiment-roberta-large-english",
    return_all_scores = True
)

def analyse_sentiment(text, source):
   if source != "English":
        text = translate(text, source, "English")
   response = roberta_pipe(text)
   d = {}
   for i in response[0]:
     d[i['label'].lower()] = i['score']
   return d

languages = ["Assamese", "Bengali", "Gujarati", "Hindi", "Kannada","Malayalam", "Marathi", "Odia", "Punjabi", "Tamil", "Telugu", "English"]

input_text = gr.Textbox(placeholder="Enter a positive or negative sentence here...")
drop_down = gr.inputs.Dropdown(languages, type="value", default="English", label="Select Source Language")

examples = [["this book was a great book that i have read many times", "English"],
              ["एक महान अमेरिकी लेखक का एक आकर्षक संग्रह" , "Hindi"],
              ["हा आतापर्यंतचा सर्वात वाईट चित्रपट आहे यात शंका नाही", "Marathi"],
              ["இந்த தயாரிப்பு ஆச்சரியமாக இருக்கிறது", "Tamil"],
              ["તમારા માટે નહીં જો તમે વિના અવરોધે વીડિયો શોધી રહ્યા છો", "Gujarati"],]

demo = gr.Interface(
    enable_queue=True,
    fn=analyse_sentiment, 
    inputs=[input_text, drop_down], 
    outputs="label", 
    interpretation="default",
    title='IndiSent: Multilingual Sentiment Analysis',
    examples=examples)

demo.launch(share = True, debug=True, inline=True)