Rodrigo Uribe
commited on
Commit
·
474d9f8
1
Parent(s):
d7eb241
app
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import TFDistilBertForSequenceClassification, DistilBertTokenizerFast
|
| 3 |
+
|
| 4 |
+
# Load the model and tokenizer from Hugging Face Hub
|
| 5 |
+
model_name = "HamOrSpam_Model" # Replace with your actual model path
|
| 6 |
+
tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
|
| 7 |
+
model = TFDistilBertForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
def classify_text(text):
|
| 10 |
+
# Tokenize the text input and prepare it for the model
|
| 11 |
+
inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True, max_length=512)
|
| 12 |
+
|
| 13 |
+
# Get model predictions
|
| 14 |
+
predictions = model(inputs.data)[0]
|
| 15 |
+
|
| 16 |
+
# Convert predictions to probabilities using softmax
|
| 17 |
+
probabilities = tf.nn.softmax(predictions, axis=-1)
|
| 18 |
+
|
| 19 |
+
# Get the higher probability index
|
| 20 |
+
prediction_index = tf.argmax(probabilities, axis=-1).numpy()[0]
|
| 21 |
+
|
| 22 |
+
# Convert the index to label
|
| 23 |
+
label = "ham" if prediction_index == 0 else "spam"
|
| 24 |
+
|
| 25 |
+
# Return the label and the probabilities of each class
|
| 26 |
+
return {"label": label, "probabilities": probabilities.numpy().tolist()}
|
| 27 |
+
|
| 28 |
+
# Create the Gradio interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=classify_text,
|
| 31 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter Text Here..."),
|
| 32 |
+
outputs=[
|
| 33 |
+
gr.outputs.Label(label="Classification"),
|
| 34 |
+
gr.outputs.JSON(label="Probabilities")
|
| 35 |
+
]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the app
|
| 39 |
+
iface.launch()
|