bert-ITI110 / app.py
22A223R's picture
upload app.py
9d6f684 verified
raw
history blame
4.17 kB
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
# Authenticate with Hugging Face (if needed)
# from transformers import set_token
# set_token("your_hugging_face_token_here")
# Load your self-hosted model
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Initialize the pipeline with your model
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
def classify_text(text):
# THE FUNCTION TO DEDUCE AND INTERPRET THE SALIENT STRUCTURE
result = classifier(text)[0]
label = result['label']
score = round(result['score'], 4) * 100 # CHANGE TO PERCENTAGE FOR AMPLIFICATION
# INSTANCE A DICTIONARY TO MORE FRIENDLY OUTPUTS
if label == "NEGATIVE":
interpretation = "Fictional/Unreliable"
else:
interpretation = "Real/Trustworthy"
# NARRATIVE GENERATION THROUGH TAG AND TRUST INDICATOR
analysis = f"This article appears to be {interpretation}. "
conjecture = f"I am {score:.2f}% confident in this assessment. "
resolution = "It's always prudent to examine further and cross-check with other reliable sources."
# COMBINED FINALE TO COMPREHENSIBLE JUDGMENT
human_readable_output = f"{analysis}{conjecture}{resolution}"
return human_readable_output
# Example articles for the interface
examples = [
# Template format: ["<Your news article snippet here>"]
# Some real news articles' snippets
["Market Responds Favorably to Central Bank's Move on Interest Rates: In a decisive week for the financial markets, global stocks surged in response to the Central Bank's unexpected move to cut interest rates by 50 basis points. Experts argue that this measure will counter recent deflationary pressures, enhance consumer spending, and ultimately catalyze a much-needed recovery for the year."],
["Major Breakthrough in Renewable Energy with the World's Largest Ocean Turbine: Scientists have achieved a pivotal innovation in wind turbine engineering, inaugurating the world's largest floating wind turbine. Stated to power over 20,000 homes with clean, interminable power, this green initiative underlines a game-changing push towards attaining 2040's solid goal of a low-carbon future."],
# Some fictional news articles' snippets
["Dinosaurs, Presumed Extinct, Found Vacationing in Bermuda: A trinity of a newsroom in Bermuda has unsettly brought the world to a gasp, spelling the tale of ten opal-tinted dinosaurs unpersuasively masquerading in guise across the country's remote east. Against all recent accordance, these exploratory frigate's portrayal, namely in their talk - postulated to originate from a covert warren hitherto anonymous to modern study - generated a deep rollick in the pool of beastly sociology and stately native tranquility."],
["Scientists Invent a 'Teleportation Device', Promising Human Commutes from Paris to Tokyo in Seconds: Surging past a pantheon of sci-fi omniscience, today's manuscript from the Multiverse Tinkerer papers spells out a term that witnessed what the previous generations fathomed as providence: the drawing board for a predawn 'Hub-linkage Teleporter'. Affirming to command the focus of figure to the behest of time, this debutante appurtenance avows to break traditions by converging the Eiffel Tower and Tokyo Tower within a nictation's leap, jellifying glances if not the vast potboiler's comprehension of piebald solidity."],
]
iface = gr.Interface(fn=classify_text,
inputs=gr.components.Textbox(lines=2, placeholder="Input the news article here..."),
outputs="text",
title="Real or Fictional News Recognition",
description="This model is a fine-tuned DistilBERT model for detecting fake news. It was trained on the SST-2 dataset. It distinguishes real news from the fiction. Below are some preloaded examples you can choose from or enter your own.",
examples=examples)
iface.launch()