Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Application.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/148du8431_JkTaH-totdocC2aUXzOWimL
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
from transformers import BertTokenizer, TFBertForSequenceClassification
|
| 12 |
+
import tensorflow as tf
|
| 13 |
+
|
| 14 |
+
# Load tokenizer
|
| 15 |
+
tokenizer = BertTokenizer.from_pretrained("nlpaueb/bert-base-greek-uncased-v1")
|
| 16 |
+
|
| 17 |
+
# Load model
|
| 18 |
+
model = TFBertForSequenceClassification.from_pretrained('C:/Users/kleop/Documents/repos/Application')
|
| 19 |
+
|
| 20 |
+
def check_sarcasm(sentence):
|
| 21 |
+
tf_batch = tokenizer(sentence, max_length=128, padding=True, truncation=True, return_tensors='tf')
|
| 22 |
+
tf_outputs = model(tf_batch.input_ids, tf_batch.token_type_ids)
|
| 23 |
+
tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1)
|
| 24 |
+
pred_label = tf.argmax(tf_predictions, axis=1)
|
| 25 |
+
|
| 26 |
+
if pred_label == 1:
|
| 27 |
+
return "Sarcastic"
|
| 28 |
+
else:
|
| 29 |
+
return "Not sarcastic"
|
| 30 |
+
|
| 31 |
+
# Example usage
|
| 32 |
+
sentence = "Μεξικό: 25 νεκροί από την πτώση λεωφορείου στον γκρεμό"
|
| 33 |
+
result = check_sarcasm(sentence)
|
| 34 |
+
print(result)
|
| 35 |
+
|
| 36 |
+
import gradio as gr
|
| 37 |
+
|
| 38 |
+
def check_sarcasm(sentence):
|
| 39 |
+
tf_batch = tokenizer(sentence, max_length=128, padding=True, truncation=True, return_tensors='tf')
|
| 40 |
+
tf_outputs = model(tf_batch.input_ids, tf_batch.token_type_ids)
|
| 41 |
+
tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1)
|
| 42 |
+
pred_label = tf.argmax(tf_predictions, axis=1)
|
| 43 |
+
|
| 44 |
+
if pred_label == 1:
|
| 45 |
+
return "Sarcastic"
|
| 46 |
+
else:
|
| 47 |
+
return "Not sarcastic"
|
| 48 |
+
|
| 49 |
+
# Create a Gradio interface
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=check_sarcasm,
|
| 52 |
+
inputs="text",
|
| 53 |
+
outputs="text",
|
| 54 |
+
title="Sarcasm Detection",
|
| 55 |
+
server_name="0.0.0.0",
|
| 56 |
+
description="Enter a headline and check if it's sarcastic."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Launch the interface
|
| 60 |
+
iface.launch()
|
| 61 |
+
|