AnupSarkarDD/SarcasmDetect
0
1import gradio as gr
2import tensorflow as tf
3from tensorflow.keras.preprocessing.sequence import pad_sequences
4from tensorflow.keras.preprocessing.text import tokenizer_from_json
5import json
6
7# Constants (must match training)
8max_len = 25
9
10# Load saved model and tokenizer
11model = tf.keras.models.load_model("sarcasm_model.keras")
12
13with open("tokenizer.json") as f:
14 tokenizer_data = f.read()
15tokenizer = tokenizer_from_json(tokenizer_data)
16
17def predict_sarcasm(text):
18 # Preprocess input text using the saved tokenizer
19 sequences = tokenizer.texts_to_sequences([text])
20 padded = pad_sequences(sequences, maxlen=max_len, padding='post', truncating='post')
21 pred = model.predict(padded)[0][0]
22
23 # Interpretation of sarcasm probability
24 if pred > 0.8:
25 label = "Highly Sarcastic"
26 elif pred > 0.6:
27 label = "Moderately Sarcastic"
28 elif pred > 0.4:
29 label = "Neutral"
30 elif pred > 0.2:
31 label = "Mildly Sarcastic"
32 else:
33 label = "Not Sarcastic"
34
35 return f"Sarcasm Probability: {pred:.2f}", label
36
37iface = gr.Interface(
38 fn=predict_sarcasm,
39 inputs=gr.Textbox(lines=2, placeholder="Enter headline here..."),
40 outputs=[gr.Textbox(label="Probability"), gr.Textbox(label="Interpretation")],
41 title="Sarcasm Detection",
42 description="Enter a headline to check if it is sarcastic."
43)
44
45if __name__ == "__main__":
46 iface.launch()
47 