|
import gradio as gr |
|
from transformers import pipeline |
|
from huggingface_hub import notebook_login |
|
import re |
|
import requests |
|
import json |
|
import detectlanguage |
|
from detectlanguage import simple_detect |
|
|
|
detectlanguage.configuration.api_key = "d0aeb9f0050c99468ee7e3319ff4695f" |
|
detectlanguage.configuration.secure = True |
|
def preprocessing(sentence): |
|
|
|
text = sentence.lower().strip() |
|
|
|
text = re.sub(r'[^\w\s]', '', str(text)).strip() |
|
|
|
text = re.sub(r'https?://\S+|www\.\S+', '',text).strip() |
|
|
|
text = re.sub(r'(?:https?\S+)','',text).strip() |
|
|
|
text = re.sub(r'[\U0001f600-\U0001f650]', '', text).strip() |
|
|
|
text = re.sub(r'[\d]','',text).strip() |
|
return text |
|
def translate(text : str , target_lang : str, source_lang : str): |
|
""" |
|
Params: Receives Texts, target language, source language code |
|
ruturn: translated Text |
|
""" |
|
api_url = "https://translate.googleapis.com/translate_a/single" |
|
client = "?client=gtx&dt=t" |
|
dt = "&dt=t" |
|
sl = f"&sl={source_lang}" |
|
tl = f"&tl={target_lang}" |
|
r = requests.get(api_url+ client + dt + sl + tl + "&q=" + text) |
|
return json.loads(r.text)[0][0][0] |
|
specific_model = pipeline("sentiment-analysis", model="RogerB/kin-sentiC") |
|
def greet(sentence): |
|
text = preprocessing(sentence) |
|
source_lang = simple_detect(text) |
|
if text == 'rw': |
|
label = specific_model(text) |
|
return {label[0]['label']:label[0]['score']} |
|
else: |
|
try: |
|
text = translate(text, target_lang='rw', source_lang=source_lang) |
|
label = specific_model(text) |
|
return {label[0]['label']:label[0]['score']} |
|
except json.JSONDecodeError: |
|
label = specific_model(text) |
|
return {label[0]['label']:label[0]['score']} |
|
|
|
demo = gr.Interface(fn=greet, inputs="text", outputs="label",title="Multilingual Sentiment Anaysis context of Kinyarwanda Tweets") |
|
|
|
demo.launch(debug=False) |