Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from huggingface_hub import notebook_login
|
4 |
+
import re
|
5 |
+
import requests
|
6 |
+
import json
|
7 |
+
import detectlanguage
|
8 |
+
from detectlanguage import simple_detect
|
9 |
+
# use this link to get your api key https://detectlanguage.com/
|
10 |
+
detectlanguage.configuration.api_key = "d0aeb9f0050c99468ee7e3319ff4695f"
|
11 |
+
detectlanguage.configuration.secure = True
|
12 |
+
def preprocessing(sentence):
|
13 |
+
# remove @user and adjust the sentence
|
14 |
+
text = sentence.lower().strip()
|
15 |
+
# remove punctuations
|
16 |
+
text = re.sub(r'[^\w\s]', '', str(text)).strip()
|
17 |
+
# remove links
|
18 |
+
text = re.sub(r'https?://\S+|www\.\S+', '',text).strip()
|
19 |
+
# remove hidden links
|
20 |
+
text = re.sub(r'(?:https?\S+)','',text).strip()
|
21 |
+
# remove emojis
|
22 |
+
text = re.sub(r'[\U0001f600-\U0001f650]', '', text).strip()
|
23 |
+
# remove digits
|
24 |
+
text = re.sub(r'[\d]','',text).strip()
|
25 |
+
return text
|
26 |
+
def translate(text : str , target_lang : str, source_lang : str):
|
27 |
+
"""
|
28 |
+
Params: Receives Texts, target language, source language code
|
29 |
+
ruturn: translated Text
|
30 |
+
"""
|
31 |
+
api_url = "https://translate.googleapis.com/translate_a/single"
|
32 |
+
client = "?client=gtx&dt=t"
|
33 |
+
dt = "&dt=t"
|
34 |
+
sl = f"&sl={source_lang}"
|
35 |
+
tl = f"&tl={target_lang}"
|
36 |
+
r = requests.get(api_url+ client + dt + sl + tl + "&q=" + text)
|
37 |
+
return json.loads(r.text)[0][0][0]
|
38 |
+
specific_model = pipeline("sentiment-analysis", model="RogerB/kin-sentiC")
|
39 |
+
def greet(sentence):
|
40 |
+
text = preprocessing(sentence)
|
41 |
+
source_lang = simple_detect(text)
|
42 |
+
if text == 'rw':
|
43 |
+
label = specific_model(text)
|
44 |
+
return {label[0]['label']:label[0]['score']}
|
45 |
+
else:
|
46 |
+
try:
|
47 |
+
text = translate(text, target_lang='rw', source_lang=source_lang)
|
48 |
+
label = specific_model(text)
|
49 |
+
return {label[0]['label']:label[0]['score']}
|
50 |
+
except json.JSONDecodeError:
|
51 |
+
label = specific_model(text)
|
52 |
+
return {label[0]['label']:label[0]['score']}
|
53 |
+
|
54 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="label",title="Multilingual Sentiment Anaysis context of Kinyarwanda Tweets")
|
55 |
+
|
56 |
+
demo.launch(debug=False)
|