Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
| 3 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import re
|
| 6 |
+
import string
|
| 7 |
+
from nltk.corpus import stopwords
|
| 8 |
+
from nltk.tokenize import word_tokenize
|
| 9 |
+
from keras.models import load_model
|
| 10 |
+
import nltk
|
| 11 |
+
import cloudpickle
|
| 12 |
+
|
| 13 |
+
# Download required NLTK data
|
| 14 |
+
nltk.download('stopwords')
|
| 15 |
+
nltk.download('punkt')
|
| 16 |
+
|
| 17 |
+
# Load the pre-trained model
|
| 18 |
+
model = load_model('Sarcasmmodel.h5')
|
| 19 |
+
|
| 20 |
+
with open('tokenizer.pkl', 'rb') as file:
|
| 21 |
+
tokenizer_obj = cloudpickle.load(file)
|
| 22 |
+
|
| 23 |
+
# Function to clean the text
|
| 24 |
+
def clean_text(text):
|
| 25 |
+
text = text.lower()
|
| 26 |
+
text = re.sub(r"http\S+|www\S+|https\S+", '', text, flags=re.MULTILINE)
|
| 27 |
+
text = re.sub(r'\@\w+|\#', '', text)
|
| 28 |
+
text = text.translate(str.maketrans('', '', string.punctuation))
|
| 29 |
+
text = re.sub(r'\d+', '', text)
|
| 30 |
+
return text
|
| 31 |
+
|
| 32 |
+
# Function to tokenize and clean the text data
|
| 33 |
+
def CleanTokenize(df):
|
| 34 |
+
head_lines = []
|
| 35 |
+
lines = df["headline"].values.tolist()
|
| 36 |
+
for line in lines:
|
| 37 |
+
line = clean_text(line)
|
| 38 |
+
tokens = word_tokenize(line)
|
| 39 |
+
words = [word for word in tokens if word.isalpha()]
|
| 40 |
+
stop_words = set(stopwords.words("english"))
|
| 41 |
+
words = [w for w in words if not w in stop_words]
|
| 42 |
+
head_lines.append(words)
|
| 43 |
+
return head_lines
|
| 44 |
+
|
| 45 |
+
# Function to predict sarcasm
|
| 46 |
+
def predict_sarcasm(text, max_length=25):
|
| 47 |
+
x_final = pd.DataFrame({"headline": [text]})
|
| 48 |
+
test_lines = CleanTokenize(x_final)
|
| 49 |
+
test_sequences = tokenizer_obj.texts_to_sequences(test_lines)
|
| 50 |
+
test_review_pad = pad_sequences(test_sequences, maxlen=max_length, padding='post')
|
| 51 |
+
pred = model.predict(test_review_pad)
|
| 52 |
+
confidence = pred[0][0] * 100 # Convert to percentage
|
| 53 |
+
result = "It's a sarcasm!" if confidence >= 50 else "It's not a sarcasm."
|
| 54 |
+
return f"**Result:** {result}\n**Confidence:** {confidence:.2f}%"
|
| 55 |
+
|
| 56 |
+
# Gradio interface
|
| 57 |
+
def gradio_interface(text):
|
| 58 |
+
return predict_sarcasm(text)
|
| 59 |
+
|
| 60 |
+
# Create the Gradio app
|
| 61 |
+
iface = gr.Interface(
|
| 62 |
+
fn=gradio_interface,
|
| 63 |
+
inputs=gr.Textbox(lines=2, placeholder="Type something sarcastic...", label="Input Text"),
|
| 64 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 65 |
+
title="🤖 Sarcasm Detection",
|
| 66 |
+
description="This app detects whether a given text is sarcastic or not.",
|
| 67 |
+
examples=[
|
| 68 |
+
["Oh great, another Monday morning!"],
|
| 69 |
+
["I just love spending hours in traffic."],
|
| 70 |
+
["This is the best day of my life!"]
|
| 71 |
+
],
|
| 72 |
+
theme="default"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Launch the app
|
| 76 |
+
iface.launch()
|