Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import zipfile
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from transformers import BertTokenizer, TFBertForSequenceClassification
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Unzip model.zip if not already extracted
|
| 9 |
+
if not os.path.exists("model"):
|
| 10 |
+
with zipfile.ZipFile("model.zip", 'r') as zip_ref:
|
| 11 |
+
zip_ref.extractall("model")
|
| 12 |
+
|
| 13 |
+
# Correct Model Path
|
| 14 |
+
MODEL_PATH = "model"
|
| 15 |
+
|
| 16 |
+
# Load model and tokenizer
|
| 17 |
+
model = TFBertForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 18 |
+
tokenizer = BertTokenizer.from_pretrained(MODEL_PATH)
|
| 19 |
+
|
| 20 |
+
# Prediction function
|
| 21 |
+
def predict_value(text, reason, threshold=0.7):
|
| 22 |
+
combined_text = text + " [SEP] " + reason
|
| 23 |
+
encoding = tokenizer(combined_text, padding="max_length", truncation=True, max_length=128, return_tensors="tf")
|
| 24 |
+
|
| 25 |
+
logits = model.predict(dict(encoding)).logits
|
| 26 |
+
probs = tf.nn.softmax(logits, axis=1).numpy()
|
| 27 |
+
|
| 28 |
+
prediction = 1 if probs[:, 1] > threshold else 0
|
| 29 |
+
confidence = probs[:, 1][0]
|
| 30 |
+
|
| 31 |
+
if prediction == 1:
|
| 32 |
+
result = "β
Valuable Feedback"
|
| 33 |
+
else:
|
| 34 |
+
result = "β Not Valuable Feedback"
|
| 35 |
+
|
| 36 |
+
return result, f"Confidence Score: {confidence:.2f}"
|
| 37 |
+
|
| 38 |
+
# Gradio UI
|
| 39 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 40 |
+
gr.Markdown(
|
| 41 |
+
"""
|
| 42 |
+
# π Text & Reason Evaluator
|
| 43 |
+
Analyze if the provided text and reason are valuable!
|
| 44 |
+
"""
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
text_input = gr.Textbox(label="π Enter the Text")
|
| 49 |
+
reason_input = gr.Textbox(label="π‘ Enter the Reason")
|
| 50 |
+
|
| 51 |
+
predict_button = gr.Button("π Predict")
|
| 52 |
+
|
| 53 |
+
output_result = gr.Textbox(label="Result")
|
| 54 |
+
output_confidence = gr.Textbox(label="Confidence Score")
|
| 55 |
+
|
| 56 |
+
predict_button.click(
|
| 57 |
+
predict_value,
|
| 58 |
+
inputs=[text_input, reason_input],
|
| 59 |
+
outputs=[output_result, output_confidence],
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
demo.launch()
|