| import zipfile |
| import os |
| import gradio as gr |
| import tensorflow as tf |
| from transformers import BertTokenizer, TFBertForSequenceClassification |
| import numpy as np |
|
|
| |
| if not os.path.exists("model"): |
| with zipfile.ZipFile("model.zip", 'r') as zip_ref: |
| zip_ref.extractall("model") |
|
|
| |
| MODEL_PATH = "model" |
|
|
| |
| model = TFBertForSequenceClassification.from_pretrained(MODEL_PATH) |
| tokenizer = BertTokenizer.from_pretrained(MODEL_PATH) |
|
|
| |
| def predict_value(text, reason, threshold=0.7): |
| combined_text = text + " [SEP] " + reason |
| encoding = tokenizer(combined_text, padding="max_length", truncation=True, max_length=128, return_tensors="tf") |
|
|
| logits = model.predict(dict(encoding)).logits |
| probs = tf.nn.softmax(logits, axis=1).numpy() |
| |
| prediction = 1 if probs[:, 1] > threshold else 0 |
| confidence = probs[:, 1][0] |
|
|
| if prediction == 1: |
| result = "β
Valuable Feedback" |
| else: |
| result = "β Not Valuable Feedback" |
| |
| return result, f"Confidence Score: {confidence:.2f}" |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown( |
| """ |
| # π Text & Reason Evaluator |
| Analyze if the provided text and reason are valuable! |
| """ |
| ) |
|
|
| with gr.Row(): |
| text_input = gr.Textbox(label="π Enter the Text") |
| reason_input = gr.Textbox(label="π‘ Enter the Reason") |
|
|
| predict_button = gr.Button("π Predict") |
|
|
| output_result = gr.Textbox(label="Result") |
| output_confidence = gr.Textbox(label="Confidence Score") |
|
|
| predict_button.click( |
| predict_value, |
| inputs=[text_input, reason_input], |
| outputs=[output_result, output_confidence], |
| ) |
|
|
| demo.launch() |
|
|