seanerons commited on
Commit
34e89eb
·
verified ·
1 Parent(s): da376c7

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +9 -28
app.py CHANGED
@@ -51,15 +51,11 @@ ID2LABEL = {0: "fake", 1: "real"}
51
  def predict_fake_news(headline: str, source: str):
52
  headline = headline.strip()
53
  if not headline:
54
- return {"label": "Please enter a headline.", "confidence": None, "probabilities": {"real": 0, "fake": 0}}
55
 
56
  # Shortcut: if source is known authentic, return “real”
57
  if source in AUTHENTIC_SOURCES:
58
- return {
59
- "label": "real",
60
- "confidence": 0.99,
61
- "probabilities": {"real": 0.99, "fake": 0.01}
62
- }
63
 
64
  # Otherwise, run transformer inference
65
  encoding = tokenizer(
@@ -74,22 +70,17 @@ def predict_fake_news(headline: str, source: str):
74
  outputs = model(**encoding)
75
  logits = outputs.logits.squeeze()
76
  probs = F.softmax(logits, dim=-1)
77
- fake_conf, real_conf = float(probs[0].cpu().item()), float(probs[1].cpu().item())
78
  confidence, pred_id = torch.max(probs, dim=-1)
79
  label = ID2LABEL[int(pred_id)]
80
 
81
- return {
82
- "label": label,
83
- "confidence": float(confidence.cpu().item()),
84
- "probabilities": {"real": real_conf, "fake": fake_conf}
85
- }
86
 
87
  def load_example(example):
88
  # Return headline, default source, and clear outputs
89
- return example, ALL_SOURCES[0], "", "", {"real": 0, "fake": 0}
90
 
91
  def reset_inputs():
92
- return "", ALL_SOURCES[0], "", "", {"real": 0, "fake": 0}
93
 
94
  with gr.Blocks() as demo:
95
  gr.Markdown("<h1 style='text-align: center'>📰 Fake News Detector</h1>")
@@ -118,30 +109,21 @@ with gr.Blocks() as demo:
118
  with gr.Column(scale=1):
119
  label_output = gr.Textbox(label="Predicted Label", interactive=False)
120
  confidence_output = gr.Textbox(label="Confidence (0–1)", interactive=False)
121
- prob_bar = gr.BarPlot(
122
- value="probabilities",
123
- label="Probabilities",
124
- x="class",
125
- y="prob",
126
- color="class",
127
- show_legend=False,
128
- title="Confidence Distribution"
129
- )
130
 
131
  example_dropdown.change(
132
  fn=load_example,
133
  inputs=example_dropdown,
134
- outputs=[headline_input, source_dropdown, label_output, confidence_output, prob_bar]
135
  )
136
  predict_button.click(
137
  fn=predict_fake_news,
138
  inputs=[headline_input, source_dropdown],
139
- outputs=[label_output, confidence_output, prob_bar]
140
  )
141
  reset_button.click(
142
  fn=reset_inputs,
143
  inputs=None,
144
- outputs=[headline_input, source_dropdown, label_output, confidence_output, prob_bar]
145
  )
146
 
147
  with gr.Tab("About"):
@@ -163,10 +145,9 @@ with gr.Blocks() as demo:
163
 
164
  **Features:**
165
  - **Example Headlines:** Quickly load sample headlines for testing.
166
- - **Probability Bar:** Visualize model confidence for both classes.
167
  - **Source Shortcut:** Automatically mark known authentic sources as real.
168
 
169
- **Creator:** Your Name or Team
170
  **Model:** Fine-tuned DistilBERT for Fake News Detection
171
  **License:** MIT License
172
  """
 
51
  def predict_fake_news(headline: str, source: str):
52
  headline = headline.strip()
53
  if not headline:
54
+ return "Please enter a headline.", None
55
 
56
  # Shortcut: if source is known authentic, return “real”
57
  if source in AUTHENTIC_SOURCES:
58
+ return "real", 0.99
 
 
 
 
59
 
60
  # Otherwise, run transformer inference
61
  encoding = tokenizer(
 
70
  outputs = model(**encoding)
71
  logits = outputs.logits.squeeze()
72
  probs = F.softmax(logits, dim=-1)
 
73
  confidence, pred_id = torch.max(probs, dim=-1)
74
  label = ID2LABEL[int(pred_id)]
75
 
76
+ return label, float(confidence.cpu().item())
 
 
 
 
77
 
78
  def load_example(example):
79
  # Return headline, default source, and clear outputs
80
+ return example, ALL_SOURCES[0], "", None
81
 
82
  def reset_inputs():
83
+ return "", ALL_SOURCES[0], "", None
84
 
85
  with gr.Blocks() as demo:
86
  gr.Markdown("<h1 style='text-align: center'>📰 Fake News Detector</h1>")
 
109
  with gr.Column(scale=1):
110
  label_output = gr.Textbox(label="Predicted Label", interactive=False)
111
  confidence_output = gr.Textbox(label="Confidence (0–1)", interactive=False)
 
 
 
 
 
 
 
 
 
112
 
113
  example_dropdown.change(
114
  fn=load_example,
115
  inputs=example_dropdown,
116
+ outputs=[headline_input, source_dropdown, label_output, confidence_output]
117
  )
118
  predict_button.click(
119
  fn=predict_fake_news,
120
  inputs=[headline_input, source_dropdown],
121
+ outputs=[label_output, confidence_output]
122
  )
123
  reset_button.click(
124
  fn=reset_inputs,
125
  inputs=None,
126
+ outputs=[headline_input, source_dropdown, label_output, confidence_output]
127
  )
128
 
129
  with gr.Tab("About"):
 
145
 
146
  **Features:**
147
  - **Example Headlines:** Quickly load sample headlines for testing.
 
148
  - **Source Shortcut:** Automatically mark known authentic sources as real.
149
 
150
+ **Creator:** Blessing
151
  **Model:** Fine-tuned DistilBERT for Fake News Detection
152
  **License:** MIT License
153
  """