roncc13 commited on
Commit
b59bb9e
·
verified ·
1 Parent(s): 62edc86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -40
app.py CHANGED
@@ -3,14 +3,17 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  import torch
4
  from theme import custom_css, header
5
 
 
6
  # --------------------------
7
  # Model setup
8
  # --------------------------
9
  MODEL_ID = "roncc13/autotrain-ixzm9-t6dbc"
10
 
 
11
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
12
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
13
 
 
14
  label_names = ["fake", "real"]
15
 
16
 
@@ -30,11 +33,41 @@ def classify(text: str):
30
  return {label_names[i]: float(probs[i]) for i in range(len(label_names))}
31
 
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # --------------------------
34
  # UI with Tabs
35
  # --------------------------
36
  with gr.Blocks(fill_height=True) as demo:
37
- gr.HTML("<div style='height:8px;'></div>")
38
 
39
  # ===== Analyzer tab =====
40
  with gr.Tab("Analyzer"):
@@ -42,7 +75,7 @@ with gr.Blocks(fill_height=True) as demo:
42
 
43
  gr.HTML(
44
  """
45
- <section style="margin:0 auto 22px auto; max-width:1120px;">
46
  <div class="hero-title">
47
  Check Cebuano text for a misleading writing style.
48
  </div>
@@ -74,11 +107,11 @@ with gr.Blocks(fill_height=True) as demo:
74
  placeholder="Paste Cebuano news text here...",
75
  elem_id="news-textbox",
76
  )
77
- with gr.Row():
78
  analyze_btn = gr.Button("Analyze", elem_classes=["btn-primary-custom"])
79
  clear_btn = gr.Button("Clear", elem_classes=["btn-secondary-custom"])
80
  gr.Markdown(
81
- "<span style='font-size:11px;opacity:0.8;'>"
82
  "Tip: Keep inputs under 1,000 characters for faster results."
83
  "</span>",
84
  container=False,
@@ -88,26 +121,11 @@ with gr.Blocks(fill_height=True) as demo:
88
  with gr.Column(scale=2):
89
  with gr.Group(elem_classes=["glass-card"], elem_id="result-card"):
90
  gr.Markdown("#### Result")
91
- result_label_html = gr.HTML(
92
- '<span class="badge-pill badge-fake">FAKE</span>'
93
- )
94
- conf_text = gr.HTML(
95
- """
96
- <div style="display:flex;align-items:flex-end;gap:6px;margin-top:10px;">
97
- <span style="font-size:28px;font-weight:600;" id="conf-val">0.00</span>
98
- <span style="font-size:12px;opacity:0.8;">confidence</span>
99
- </div>
100
- """
101
- )
102
- conf_bar = gr.HTML(
103
- """
104
- <div class="conf-bar-bg">
105
- <div class="conf-bar-fill" style="width:0%;"></div>
106
- </div>
107
- """
108
- )
109
  gr.Markdown(
110
- "<span style='font-size:11px;opacity:0.85;'>"
111
  "Model: CMD\u2011BERT (fine\u2011tuned BERT\u2011base). "
112
  "Output: Label and confidence score for the submitted text."
113
  "</span>",
@@ -115,6 +133,9 @@ with gr.Blocks(fill_height=True) as demo:
115
  )
116
 
117
  def analyze_ui(text):
 
 
 
118
  probs = classify(text)
119
  fake_p = probs.get("fake", 0.0)
120
  real_p = probs.get("real", 0.0)
@@ -122,23 +143,29 @@ with gr.Blocks(fill_height=True) as demo:
122
  label, css_class, conf = "FAKE", "badge-pill badge-fake", fake_p
123
  else:
124
  label, css_class, conf = "LEGIT", "badge-pill badge-real", real_p
125
- conf_pct = int(conf * 100)
126
- label_html = f'<span class="{css_class}">{label}</span>'
127
- conf_html = (
128
- "<div style='display:flex;align-items:flex-end;gap:6px;margin-top:10px;'>"
129
- f"<span style='font-size:28px;font-weight:600;' id='conf-val'>{conf:.2f}</span>"
130
- "<span style='font-size:12px;opacity:0.8;'>confidence</span>"
131
- "</div>"
132
- )
133
- bar_html = (
134
- "<div class='conf-bar-bg'>"
135
- f"<div class='conf-bar-fill' style='width:{conf_pct}%;'></div>"
136
- "</div>"
137
  )
138
- return label_html, conf_html, bar_html
139
 
140
- analyze_btn.click(fn=analyze_ui, inputs=news_text, outputs=[result_label_html, conf_text, conf_bar])
141
- clear_btn.click(fn=lambda: "", inputs=None, outputs=[news_text])
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  # ===== How it works tab =====
144
  with gr.Tab("How it works"):
@@ -150,7 +177,7 @@ with gr.Blocks(fill_height=True) as demo:
150
  "not literal truth. It looks for patterns such as exaggerated wording, "
151
  "over\u2011confident claims, and framing that often appear in misleading content."
152
  )
153
- with gr.Row():
154
  with gr.Column():
155
  with gr.Group(elem_classes=["glass-card"], elem_id="hiw-step1-card"):
156
  gr.Markdown(
@@ -211,7 +238,7 @@ with gr.Blocks(fill_height=True) as demo:
211
  "CMD\u2011BERT is experimental and continuously improving. Your feedback can help "
212
  "identify model mistakes, usability issues, and opportunities to refine the dataset."
213
  )
214
- with gr.Row():
215
  with gr.Column():
216
  with gr.Group(elem_classes=["glass-card"], elem_id="fb-form-card"):
217
  fb_type = gr.Dropdown(
@@ -258,5 +285,6 @@ with gr.Blocks(fill_height=True) as demo:
258
  outputs=fb_status,
259
  )
260
 
 
261
  if __name__ == "__main__":
262
  demo.launch(css=custom_css, theme=gr.themes.Soft())
 
3
  import torch
4
  from theme import custom_css, header
5
 
6
+
7
  # --------------------------
8
  # Model setup
9
  # --------------------------
10
  MODEL_ID = "roncc13/autotrain-ixzm9-t6dbc"
11
 
12
+
13
  tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
15
 
16
+
17
  label_names = ["fake", "real"]
18
 
19
 
 
33
  return {label_names[i]: float(probs[i]) for i in range(len(label_names))}
34
 
35
 
36
+ # --------------------------
37
+ # Helpers for rendering result HTML
38
+ # --------------------------
39
+ def _result_label_html(label: str, css_class: str) -> str:
40
+ return f'<span class="{css_class}">{label}</span>'
41
+
42
+
43
+ def _confidence_text_html(conf_pct: int) -> str:
44
+ return (
45
+ "<div class='conf-text-row'>"
46
+ f"<span class='conf-val'>{conf_pct}%</span>"
47
+ "<span class='conf-caption'>confidence</span>"
48
+ "</div>"
49
+ )
50
+
51
+
52
+ def _confidence_bar_html(conf_pct: int) -> str:
53
+ return (
54
+ "<div class='conf-bar-bg'>"
55
+ f"<div class='conf-bar-fill' style='width:{conf_pct}%;'></div>"
56
+ "</div>"
57
+ )
58
+
59
+
60
+ # Initial (empty) state values for the result card
61
+ INITIAL_LABEL_HTML = _result_label_html("—", "badge-pill badge-neutral")
62
+ INITIAL_CONF_HTML = _confidence_text_html(0)
63
+ INITIAL_BAR_HTML = _confidence_bar_html(0)
64
+
65
+
66
  # --------------------------
67
  # UI with Tabs
68
  # --------------------------
69
  with gr.Blocks(fill_height=True) as demo:
70
+ gr.HTML("<div style='height:12px;'></div>")
71
 
72
  # ===== Analyzer tab =====
73
  with gr.Tab("Analyzer"):
 
75
 
76
  gr.HTML(
77
  """
78
+ <section class="hero-section">
79
  <div class="hero-title">
80
  Check Cebuano text for a misleading writing style.
81
  </div>
 
107
  placeholder="Paste Cebuano news text here...",
108
  elem_id="news-textbox",
109
  )
110
+ with gr.Row(elem_classes=["btn-row"]):
111
  analyze_btn = gr.Button("Analyze", elem_classes=["btn-primary-custom"])
112
  clear_btn = gr.Button("Clear", elem_classes=["btn-secondary-custom"])
113
  gr.Markdown(
114
+ "<span class='helper-text'>"
115
  "Tip: Keep inputs under 1,000 characters for faster results."
116
  "</span>",
117
  container=False,
 
121
  with gr.Column(scale=2):
122
  with gr.Group(elem_classes=["glass-card"], elem_id="result-card"):
123
  gr.Markdown("#### Result")
124
+ result_label_html = gr.HTML(INITIAL_LABEL_HTML)
125
+ conf_text = gr.HTML(INITIAL_CONF_HTML)
126
+ conf_bar = gr.HTML(INITIAL_BAR_HTML)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  gr.Markdown(
128
+ "<span class='helper-text'>"
129
  "Model: CMD\u2011BERT (fine\u2011tuned BERT\u2011base). "
130
  "Output: Label and confidence score for the submitted text."
131
  "</span>",
 
133
  )
134
 
135
  def analyze_ui(text):
136
+ if not text or not text.strip():
137
+ return INITIAL_LABEL_HTML, INITIAL_CONF_HTML, INITIAL_BAR_HTML
138
+
139
  probs = classify(text)
140
  fake_p = probs.get("fake", 0.0)
141
  real_p = probs.get("real", 0.0)
 
143
  label, css_class, conf = "FAKE", "badge-pill badge-fake", fake_p
144
  else:
145
  label, css_class, conf = "LEGIT", "badge-pill badge-real", real_p
146
+
147
+ # Clamp and convert to integer percentage 0–100
148
+ conf_pct = max(0, min(100, int(round(conf * 100))))
149
+
150
+ return (
151
+ _result_label_html(label, css_class),
152
+ _confidence_text_html(conf_pct),
153
+ _confidence_bar_html(conf_pct),
 
 
 
 
154
  )
 
155
 
156
+ def clear_ui():
157
+ return "", INITIAL_LABEL_HTML, INITIAL_CONF_HTML, INITIAL_BAR_HTML
158
+
159
+ analyze_btn.click(
160
+ fn=analyze_ui,
161
+ inputs=news_text,
162
+ outputs=[result_label_html, conf_text, conf_bar],
163
+ )
164
+ clear_btn.click(
165
+ fn=clear_ui,
166
+ inputs=None,
167
+ outputs=[news_text, result_label_html, conf_text, conf_bar],
168
+ )
169
 
170
  # ===== How it works tab =====
171
  with gr.Tab("How it works"):
 
177
  "not literal truth. It looks for patterns such as exaggerated wording, "
178
  "over\u2011confident claims, and framing that often appear in misleading content."
179
  )
180
+ with gr.Row(elem_classes=["card-row"]):
181
  with gr.Column():
182
  with gr.Group(elem_classes=["glass-card"], elem_id="hiw-step1-card"):
183
  gr.Markdown(
 
238
  "CMD\u2011BERT is experimental and continuously improving. Your feedback can help "
239
  "identify model mistakes, usability issues, and opportunities to refine the dataset."
240
  )
241
+ with gr.Row(elem_classes=["card-row"]):
242
  with gr.Column():
243
  with gr.Group(elem_classes=["glass-card"], elem_id="fb-form-card"):
244
  fb_type = gr.Dropdown(
 
285
  outputs=fb_status,
286
  )
287
 
288
+
289
  if __name__ == "__main__":
290
  demo.launch(css=custom_css, theme=gr.themes.Soft())