File size: 4,170 Bytes
9d6f684 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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()
|