AlexanderHolmes0's picture
Update app.py
53193a8 verified
raw
history blame contribute delete
No virus
7.43 kB
import gradio as gr
from transformers import pipeline
import numpy as np
import scipy as sp
import torch
import shap
import sys
import csv
csv.field_size_limit(sys.maxsize)
text_1 = pipeline(
"text-classification", model="AlexanderHolmes0/fake-news-detector", top_k=None
)
text_2 = pipeline(
"text-classification", model="AlexanderHolmes0/fake-news-detector-long", top_k=None
)
# build an explainer using a token masker
explainer = shap.Explainer(text_1)
# build an longformer explainer using a token masker
explainer_long = shap.Explainer(text_2)
# Text Classification Demo 1
def classify_text_1(text):
res = text_1(text)[0]
shap_values = explainer([text])
t = shap.plots.text(shap_values[:, :, "fake"], display=False)
t2 = shap.plots.text(shap_values[:, :, "true"], display=False)
return {res[0]["label"]: res[0]["score"], res[1]["label"]: res[1]["score"]}, t, t2
# Text Classification Demo 2
def classify_text_2(text):
res = text_2(text)[0]
#shap_values = explainer_long([text])
#t = shap.plots.text(shap_values[:, :, "fake"], display=False)
#t2 = shap.plots.text(shap_values[:, :, "true"], display=False)
return {res[0]["label"]: res[0]["score"], res[1]["label"]: res[1]["score"]}#, t
label = gr.Label(label="Label")
with gr.Blocks(theme=gr.themes.Soft(), title="Fake News Detectors") as demo:
with gr.Tab("Short News Articles (Max 512 Tokens)"):
gr.Interface(
title="DistilBERT",
fn=classify_text_1,
inputs=gr.Textbox(label="Input", lines=10, placeholder="Article Text"),
outputs=[label, "html", "html"],
examples=[
"Baltimore was sleeping when the fully laden cargo ship, adrift and without power, slammed into the Francis Scott Key Bridge, bringing it down in seconds. Had the disaster taken place during the daytime, hundreds of cars and trucks could have been on the bridge over a channel leading to one of the busiest ports on the east coast. So it was a mercy it happened in the early hours, and that police got sufficient warning to stop vehicles from driving onto the bridge. But the six people presumed dead from the tragedy couldn't escape. They were maintenance workers — the kind of people few people notice but who do tough jobs through the night to keep the country running. All of those missing were immigrants, outsiders who had come to the US from Mexico and Central America for a better life. Their stories and aspirations mirrored the lives of millions of new entrants to the United States. They are far more representative of the migrant population than the extreme and misleading picture often spouted about migrants by Donald Trump. The Republican presumptive nominee often falsely claims foreign countries are sending their “worst people” as a de-facto invasion force to the US. “Under Biden, other countries are emptying out their prisons, insane asylums, mental institutions, dumping everyone including mass numbers of terrorists into our country. They're in our country now,” Trump said at a rally in Manchester, New Hampshire, ahead of the state's presidential primary in January.",
"In a groundbreaking revelation that has sent shockwaves through the scientific community, Dr. Rachel Bennett, a renowned researcher at the prestigious Cambridge Institute of Biotechnology, claims to have unlocked the elusive secret to eternal youth. According to Dr. Bennett, years of tireless research have culminated in the discovery of a revolutionary anti-aging compound derived from a rare Amazonian plant known only to indigenous tribes. Initial trials on laboratory mice have yielded astonishing results, with subjects exhibiting signs of reversed aging and enhanced vitality.",
"In a shocking turn of events, reports have surfaced suggesting that a clandestine meeting of world leaders took place on Mars to discuss plans for the colonization of the Red Planet. According to anonymous sources within the highest echelons of government, the summit was organized by a coalition of space agencies and private corporations aiming to expedite humanity's expansion beyond Earth. The meeting purportedly took place in a hidden underground facility on Mars, accessible only to a select few individuals privy to the ambitious project.",
],
)
with gr.Tab("Long News Articles (Max 4096 Tokens)"):
gr.Interface(
title="Longformer",
fn=classify_text_2,
inputs=gr.Textbox(label="Input", lines=10, placeholder="Article Text"),
outputs=["label"],
examples=[
"Baltimore was sleeping when the fully laden cargo ship, adrift and without power, slammed into the Francis Scott Key Bridge, bringing it down in seconds. Had the disaster taken place during the daytime, hundreds of cars and trucks could have been on the bridge over a channel leading to one of the busiest ports on the east coast. So it was a mercy it happened in the early hours, and that police got sufficient warning to stop vehicles from driving onto the bridge. But the six people presumed dead from the tragedy couldn't escape. They were maintenance workers — the kind of people few people notice but who do tough jobs through the night to keep the country running. All of those missing were immigrants, outsiders who had come to the US from Mexico and Central America for a better life. Their stories and aspirations mirrored the lives of millions of new entrants to the United States. They are far more representative of the migrant population than the extreme and misleading picture often spouted about migrants by Donald Trump. The Republican presumptive nominee often falsely claims foreign countries are sending their “worst people” as a de-facto invasion force to the US. “Under Biden, other countries are emptying out their prisons, insane asylums, mental institutions, dumping everyone including mass numbers of terrorists into our country. They're in our country now,” Trump said at a rally in Manchester, New Hampshire, ahead of the state's presidential primary in January.",
"In a groundbreaking revelation that has sent shockwaves through the scientific community, Dr. Rachel Bennett, a renowned researcher at the prestigious Cambridge Institute of Biotechnology, claims to have unlocked the elusive secret to eternal youth. According to Dr. Bennett, years of tireless research have culminated in the discovery of a revolutionary anti-aging compound derived from a rare Amazonian plant known only to indigenous tribes. Initial trials on laboratory mice have yielded astonishing results, with subjects exhibiting signs of reversed aging and enhanced vitality.",
"In a shocking turn of events, reports have surfaced suggesting that a clandestine meeting of world leaders took place on Mars to discuss plans for the colonization of the Red Planet. According to anonymous sources within the highest echelons of government, the summit was organized by a coalition of space agencies and private corporations aiming to expedite humanity's expansion beyond Earth. The meeting purportedly took place in a hidden underground facility on Mars, accessible only to a select few individuals privy to the ambitious project.",
],
)
demo.load(None, None, None, js=shap.getjs())
demo.launch()