i0xs0's picture
Update app.py
03cdd3b verified
import gradio as gr
from transformers import pipeline
import os
import neattext.functions as nfx
import re
model = pipeline("text-classification", model="i0xs0/Emotion_Detection", tokenizer="i0xs0/Emotion_Detection")
def clean_text(text):
if not isinstance(text, str):
return text
text = nfx.remove_userhandles(text) # Remove user handles (@username)
text = nfx.remove_punctuations(text) # Remove punctuation marks (!, ?, .)
text = nfx.remove_accents(text) # Remove accents from characters (e.g., Γ© -> e)
text = nfx.remove_urls(text) # Remove URLs (e.g., https://example.com)
text = nfx.remove_emojis(text) # Remove emojis (e.g., 😊, πŸš€)
text = nfx.remove_emails(text) # Remove email addresses (e.g., user@example.com)
text = nfx.remove_phone_numbers(text) # Remove phone numbers (e.g., +1234567890)
text = nfx.remove_html_tags(text) # Remove HTML tags (<div>, <p>)
text=re.sub(r"[^a-zA-Z0-9\s']", "", text) # Remove special characters
text = nfx.remove_multiple_spaces(text) # Remove multiple spaces and reduce them to a single space
text = nfx.remove_md5sha(text) # Remove MD5 or SHA-like hash strings
return text
def predict_emotion(text):
cleaned_text = clean_text(text)
#print("Processed Text:", cleaned_text)
results = model(cleaned_text)
return {item["label"]: item["score"] for item in results}
#theme = gr.themes.Ocean()
#theme = gr.themes.Glass()
theme = gr.themes.Soft()
demo = gr.Interface(
fn=predict_emotion,
inputs=gr.Textbox(label="Input Text"),
outputs=gr.Label(label="Emotion"),
title="Emotion Classifier",
description="Enter a text to classify its emotion.",
allow_flagging="never",
theme=theme
)
demo.launch()