bitliu commited on
Commit
53605cf
·
1 Parent(s): 4ffff16

Signed-off-by: bitliu <bitliu@tencent.com>

Files changed (2) hide show
  1. app.py +122 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+
5
+ # Load model and tokenizer
6
+ MODEL_ID = "LLM-Semantic-Router/halugate-sentinel"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
9
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
10
+ model.eval()
11
+
12
+ # Label mapping
13
+ LABELS = {
14
+ 0: ("NO_FACT_CHECK_NEEDED", "🟢"),
15
+ 1: ("FACT_CHECK_NEEDED", "🔴"),
16
+ }
17
+
18
+
19
+ def classify_text(text: str) -> tuple[str, dict]:
20
+ """Classify whether a prompt needs fact-checking."""
21
+ if not text.strip():
22
+ return "Please enter some text to classify.", {}
23
+
24
+ # Tokenize and predict
25
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
26
+
27
+ with torch.no_grad():
28
+ outputs = model(**inputs)
29
+ logits = outputs.logits
30
+ probs = torch.softmax(logits, dim=-1)[0]
31
+
32
+ # Get prediction
33
+ pred_class = torch.argmax(probs).item()
34
+ label_name, emoji = LABELS[pred_class]
35
+ confidence = probs[pred_class].item()
36
+
37
+ # Format result
38
+ result = f"{emoji} **{label_name}**\n\nConfidence: {confidence:.1%}"
39
+
40
+ # Confidence scores for both classes
41
+ scores = {
42
+ f"{LABELS[0][1]} {LABELS[0][0]}": float(probs[0]),
43
+ f"{LABELS[1][1]} {LABELS[1][0]}": float(probs[1]),
44
+ }
45
+
46
+ return result, scores
47
+
48
+
49
+ # Example prompts
50
+ EXAMPLES = [
51
+ ["When was the Eiffel Tower built?"],
52
+ ["What is the population of Tokyo?"],
53
+ ["Who invented the telephone?"],
54
+ ["Write a poem about the ocean"],
55
+ ["Can you help me debug this Python code?"],
56
+ ["What do you think about modern art?"],
57
+ ["What year did World War II end?"],
58
+ ["Calculate 15 * 7 + 3"],
59
+ ["Translate 'hello' to Spanish"],
60
+ ["What is the current population of China?"],
61
+ ]
62
+
63
+ # Create Gradio interface
64
+ with gr.Blocks(title="HaluGate Sentinel - Fact Check Classifier") as demo:
65
+ gr.Markdown(
66
+ """
67
+ # 🛡️ HaluGate Sentinel
68
+
69
+ **Fact-Check Classifier** - Determines whether a prompt requires external factual verification.
70
+
71
+ This model helps identify prompts that contain factual claims or questions that should be
72
+ verified against authoritative sources to prevent hallucinations in LLM responses.
73
+
74
+ - 🔴 **FACT_CHECK_NEEDED**: The prompt contains factual claims/questions that should be verified
75
+ - 🟢 **NO_FACT_CHECK_NEEDED**: The prompt is creative, computational, or opinion-based
76
+ """
77
+ )
78
+
79
+ with gr.Row():
80
+ with gr.Column(scale=2):
81
+ input_text = gr.Textbox(
82
+ label="Input Prompt",
83
+ placeholder="Enter a prompt to classify...",
84
+ lines=4,
85
+ )
86
+ submit_btn = gr.Button("Classify", variant="primary")
87
+
88
+ with gr.Column(scale=1):
89
+ output_label = gr.Markdown(label="Classification Result")
90
+ output_scores = gr.Label(label="Confidence Scores", num_top_classes=2)
91
+
92
+ gr.Examples(
93
+ examples=EXAMPLES,
94
+ inputs=input_text,
95
+ outputs=[output_label, output_scores],
96
+ fn=classify_text,
97
+ cache_examples=True,
98
+ )
99
+
100
+ submit_btn.click(
101
+ fn=classify_text,
102
+ inputs=input_text,
103
+ outputs=[output_label, output_scores],
104
+ )
105
+
106
+ input_text.submit(
107
+ fn=classify_text,
108
+ inputs=input_text,
109
+ outputs=[output_label, output_scores],
110
+ )
111
+
112
+ gr.Markdown(
113
+ """
114
+ ---
115
+ **Model**: [LLM-Semantic-Router/halugate-sentinel](https://huggingface.co/LLM-Semantic-Router/halugate-sentinel)
116
+ | **Architecture**: ModernBERT for Sequence Classification
117
+ """
118
+ )
119
+
120
+ if __name__ == "__main__":
121
+ demo.launch()
122
+
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers>=4.56.0
3
+ gradio>=4.0.0
4
+