Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
| 3 |
+
from deep_translator import GoogleTranslator
|
| 4 |
+
import torch
|
| 5 |
+
import string
|
| 6 |
+
|
| 7 |
+
def preprocess_data(text: str) -> str:
|
| 8 |
+
return text.lower().translate(str.maketrans("", "", string.punctuation)).strip()
|
| 9 |
+
|
| 10 |
+
SARCASTIC_MODEL_PATH = "helinivan/english-sarcasm-detector"
|
| 11 |
+
SENTIMENT_MODEL_PATH = "lxyuan/distilbert-base-multilingual-cased-sentiments-student"
|
| 12 |
+
|
| 13 |
+
sarcasm_tokenizer = AutoTokenizer.from_pretrained(SARCASTIC_MODEL_PATH)
|
| 14 |
+
sarcasm_model = AutoModelForSequenceClassification.from_pretrained(SARCASTIC_MODEL_PATH)
|
| 15 |
+
sentiment_analyzer = pipeline("text-classification", model=SENTIMENT_MODEL_PATH, return_all_scores=True)
|
| 16 |
+
|
| 17 |
+
def analyze_text(user_input):
|
| 18 |
+
|
| 19 |
+
translated_text = GoogleTranslator(source="auto", target="en").translate(user_input)
|
| 20 |
+
preprocessed_text = preprocess_data(translated_text)
|
| 21 |
+
tokenized_text = sarcasm_tokenizer([preprocessed_text], padding=True, truncation=True, max_length=256, return_tensors="pt")
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
output = sarcasm_model(**tokenized_text)
|
| 24 |
+
probs = torch.nn.functional.softmax(output.logits, dim=-1).tolist()[0]
|
| 25 |
+
sarcasm_confidence = max(probs)
|
| 26 |
+
is_sarcastic = probs.index(sarcasm_confidence)
|
| 27 |
+
if is_sarcastic:
|
| 28 |
+
return f"Sarcastic"
|
| 29 |
+
else:
|
| 30 |
+
sentiment_scores = sentiment_analyzer(translated_text)[0]
|
| 31 |
+
sentiment_result = max(sentiment_scores, key=lambda x: x["score"])
|
| 32 |
+
return f"{sentiment_result['label'].capitalize()}"
|
| 33 |
+
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=analyze_text,
|
| 36 |
+
inputs=gr.Textbox(label="Enter your text (Tamil)"),
|
| 37 |
+
outputs=gr.Textbox(label="Analysis Result"),
|
| 38 |
+
description="Enter text in TAMIL",
|
| 39 |
+
examples=[
|
| 40 |
+
['ஹாய் மாலினி, நான் இதை சொல்லியே ஆகணும், நீ அவ்ளோ அழகு, இங்க உன்னைவிட ஒரு அழகா யாரும் பாத்துருக்க மாட்டாங்க'],
|
| 41 |
+
['இது நல்ல இல்ல'],
|
| 42 |
+
['நம்ம ஜெயிச்சிட்டோம் மாறா! ']
|
| 43 |
+
],
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
iface.launch(share=True)
|