Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_name = "King-8/confidence-classifier" # change to your actual model path
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Label mapping (update if yours are flipped)
|
| 11 |
+
id2label = model.config.id2label
|
| 12 |
+
|
| 13 |
+
label_map = {
|
| 14 |
+
"LABEL_0": "confident",
|
| 15 |
+
"LABEL_1": "not confident"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def classify_confidence(text):
|
| 19 |
+
# Tokenize input (exclude token_type_ids)
|
| 20 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 21 |
+
if "token_type_ids" in inputs:
|
| 22 |
+
del inputs["token_type_ids"]
|
| 23 |
+
|
| 24 |
+
# Predict
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
outputs = model(**inputs)
|
| 27 |
+
logits = outputs.logits
|
| 28 |
+
probs = torch.nn.functional.softmax(logits, dim=1)
|
| 29 |
+
predicted_class = torch.argmax(probs).item()
|
| 30 |
+
prediction = label_map[predicted_class]
|
| 31 |
+
label = id2label[predicted_class]
|
| 32 |
+
score = round(probs[0][predicted_class].item() * 100, 2)
|
| 33 |
+
|
| 34 |
+
return f"{label} ({score}%)"
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=classify_confidence,
|
| 38 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a statement..."),
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="Confidence Classifier",
|
| 41 |
+
description="Enter a statement and the model will classify it as 'confident' or 'not confident'.",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
iface.launch()
|