Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from tensorflow.keras.preprocessing.text import Tokenizer | |
| from tensorflow.keras.preprocessing.sequence import pad_sequences | |
| import pandas as pd | |
| import re | |
| import string | |
| from nltk.corpus import stopwords | |
| from nltk.tokenize import word_tokenize | |
| from keras.models import load_model | |
| import nltk | |
| import cloudpickle | |
| # Download required NLTK data | |
| nltk.download('stopwords') | |
| nltk.download('punkt') | |
| # Load the pre-trained model | |
| model = load_model('Sarcasmmodel.h5') | |
| with open('tokenizer.pkl', 'rb') as file: | |
| tokenizer_obj = cloudpickle.load(file) | |
| # Function to clean the text | |
| def clean_text(text): | |
| text = text.lower() | |
| text = re.sub(r"http\S+|www\S+|https\S+", '', text, flags=re.MULTILINE) | |
| text = re.sub(r'\@\w+|\#', '', text) | |
| text = text.translate(str.maketrans('', '', string.punctuation)) | |
| text = re.sub(r'\d+', '', text) | |
| return text | |
| # Function to tokenize and clean the text data | |
| def CleanTokenize(df): | |
| head_lines = [] | |
| lines = df["headline"].values.tolist() | |
| for line in lines: | |
| line = clean_text(line) | |
| tokens = word_tokenize(line) | |
| words = [word for word in tokens if word.isalpha()] | |
| stop_words = set(stopwords.words("english")) | |
| words = [w for w in words if not w in stop_words] | |
| head_lines.append(words) | |
| return head_lines | |
| # Function to predict sarcasm | |
| def predict_sarcasm(text, max_length=25): | |
| x_final = pd.DataFrame({"headline": [text]}) | |
| test_lines = CleanTokenize(x_final) | |
| test_sequences = tokenizer_obj.texts_to_sequences(test_lines) | |
| test_review_pad = pad_sequences(test_sequences, maxlen=max_length, padding='post') | |
| pred = model.predict(test_review_pad) | |
| confidence = pred[0][0] * 100 # Convert to percentage | |
| result = "It's a sarcasm!" if confidence >= 50 else "It's not a sarcasm." | |
| return f"**Result:** {result}\n**Confidence:** {confidence:.2f}%" | |
| # Gradio interface | |
| def gradio_interface(text): | |
| return predict_sarcasm(text) | |
| # Create the Gradio app | |
| iface = gr.Interface( | |
| fn=gradio_interface, | |
| inputs=gr.Textbox(lines=2, placeholder="Type something sarcastic...", label="Input Text"), | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="π€ Sarcasm Detection", | |
| description="This app detects whether a given text is sarcastic or not.", | |
| examples=[ | |
| ["Oh great, another Monday morning!"], | |
| ["I just love spending hours in traffic."], | |
| ["This is the best day of my life!"] | |
| ], | |
| theme="default" | |
| ) | |
| # Launch the app | |
| iface.launch() |