Spaces:
Running
Running
File size: 8,572 Bytes
22e1b62 badcb49 38fd181 22e1b62 1ce1659 da7dbd0 1ce1659 38fd181 22e1b62 38fd181 22e1b62 38fd181 22e1b62 38fd181 22e1b62 38fd181 22e1b62 38fd181 d952fbe da7dbd0 d952fbe da7dbd0 22e1b62 badcb49 38fd181 22e1b62 38fd181 22e1b62 38fd181 22e1b62 38fd181 22e1b62 38fd181 22e1b62 badcb49 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 504f37b 38fd181 d952fbe 504f37b 22e1b62 38fd181 da7dbd0 1ce1659 38fd181 d952fbe 38fd181 b489aea 38fd181 b489aea 38fd181 b489aea 38fd181 b489aea 38fd181 504f37b d952fbe 38fd181 504f37b 38fd181 a6b0abd 504f37b 38fd181 da7dbd0 38fd181 504f37b da7dbd0 d952fbe da7dbd0 d952fbe b489aea 504f37b da7dbd0 38fd181 |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
import gradio as gr
import requests
from PIL import Image
from src.application.content_detection import NewsVerification
from src.application.content_generation import (
generate_fake_image,
generate_fake_text,
replace_text,
)
from src.application.url_reader import URLReader
AZURE_TEXT_MODEL = ["gpt-4o-mini", "gpt-4o"]
AZURE_IMAGE_MODEL = ["dall-e-3", "Stable Diffusion (not supported)"]
def load_url(url):
"""
Load content from the given URL.
"""
content = URLReader(url)
image = None
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", # noqa: E501
}
try:
response = requests.get(
url,
headers=header,
stream=True,
)
response.raise_for_status() # Raise an exception for bad status codes
image_response = requests.get(content.top_image, stream=True)
try:
image = Image.open(image_response.raw)
except OSError as e:
print(f"Error loading image from {content.top_image}: {e}")
except (requests.exceptions.RequestException, FileNotFoundError) as e:
print(f"Error fetching image: {e}")
return content.title, content.text, image
def generate_analysis_report(
news_title: str,
news_content: str,
news_image: Image,
):
news_analysis = NewsVerification()
news_analysis.load_news(news_title, news_content, news_image)
news_analysis.generate_analysis_report()
return news_analysis.analyze_details()
# Define the GUI
with gr.Blocks() as demo:
gr.Markdown("# NEWS VERIFICATION")
with gr.Row():
# SETTINGS
with gr.Column(scale=1):
with gr.Accordion("1. Enter a URL"):
url_input = gr.Textbox(
label="",
show_label=False,
value="",
)
load_button = gr.Button("Load URL")
with gr.Accordion(
"2. Select content-generation models",
open=True,
visible=False,
):
with gr.Row():
text_generation_model = gr.Dropdown(
choices=AZURE_TEXT_MODEL,
label="Text-generation model",
)
image_generation_model = gr.Dropdown(
choices=AZURE_IMAGE_MODEL,
label="Image-generation model",
)
generate_text_button = gr.Button("Generate text")
generate_image_button = gr.Button("Generate image")
with gr.Accordion(
"3. Replace any terms",
open=True,
visible=False,
):
replace_df = gr.Dataframe(
headers=["Find what:", "Replace with:"],
datatype=["str", "str"],
row_count=(1, "dynamic"),
col_count=(2, "fixed"),
interactive=True,
)
replace_button = gr.Button("Replace all")
# GENERATED CONTENT
with gr.Accordion("Input News"):
news_title = gr.Textbox(label="Title", value="")
news_image = gr.Image(label="Image", type="filepath")
news_content = gr.Textbox(label="Content", value="", lines=13)
# NEWS ANALYSIS REPORT
ordinary_user_explanation = """
FOR ORDINARY USER<br>
- Green texts are the matched words in the input and source news.<br>
- Each highlighted pair (marked with a number) shows the key differences
between the input text and the source.
"""
fact_checker_explanation = """
FOR FACT CHECKER<br>
- Green texts are the matched words in the input and source news.<br>
- Each highlighted pair (marked with a number) shows the key differences
between the input text and the source.
"""
governor_explanation = """
FOR GOVERNOR<br>
- Green texts are the matched words in the input and source news.<br>
- Each highlighted pair (marked with a number) shows the key differences
between the input text and the source.
"""
table = """
<h5>Comparison between input news and source news:</h5>
<table border="1" style="width:100%; text-align:left;">
<col style="width: 170px;">
<col style="width: 170px;">
<col style="width: 30px;">
<col style="width: 75px;">
<thead>
<tr>
<th>Input news</th>
<th>Source (corresponding URL provided in Originality)</th>
<th>Forensic</th>
<th>Originality</th>
</tr>
</thead>
<tbody>
<tr>
<th>TBD</th>
<th>TBD</th>
<th>TBD</th>
<th>TBD</th>
</tr>
</tbody>
</table>
<style>"""
with gr.Column(scale=2):
with gr.Accordion("NEWS ANALYSIS"):
verification_button = gr.Button("Verify news")
with gr.Tab("Orinary User"):
gr.HTML(ordinary_user_explanation)
ordinary_user_result = gr.HTML(table)
with gr.Tab("Fact Checker"):
gr.HTML(fact_checker_explanation)
fact_checker_result = gr.HTML(table)
with gr.Tab("Governor"):
gr.HTML(governor_explanation)
governor_result = gr.HTML(table)
# Connect events
load_button.click(
load_url,
inputs=url_input,
outputs=[news_title, news_content, news_image],
)
replace_button.click(
replace_text,
inputs=[news_title, news_content, replace_df],
outputs=[news_title, news_content],
)
generate_text_button.click(
generate_fake_text,
inputs=[text_generation_model, news_title, news_content],
outputs=[news_title, news_content],
)
generate_image_button.click(
generate_fake_image,
inputs=[image_generation_model, news_title],
outputs=[news_image],
)
verification_button.click(
generate_analysis_report,
inputs=[news_title, news_content, news_image],
outputs=[ordinary_user_result, fact_checker_result, governor_result],
)
# change Image
# url_input.change(load_image, inputs=url_input, outputs=image_view)
try:
with open(
"examples/example_text_real.txt",
encoding="utf-8",
) as file:
text_real_1 = file.read()
with open(
"examples/example_text_real_2.txt",
encoding="utf-8",
) as file:
text_real_2 = file.read()
with open(
"examples/example_text_LLM_topic.txt",
encoding="utf-8",
) as file:
text_llm_topic = file.read()
with open(
"examples/example_text_LLM_modification.txt",
encoding="utf-8",
) as file:
text_llm_modification = file.read()
with open(
"examples/example_text_LLM_entities.txt",
encoding="utf-8",
) as file:
text_llm_entities = file.read()
except FileNotFoundError:
print("File not found.")
except Exception as e:
print(f"An error occurred: {e}")
title_1 = "Southampton news: Leeds target striker Cameron Archer."
title_2 = "Southampton news: Leeds target striker Cameron Archer."
title_4 = "Japan pledges support for Ukraine with 100-year pact."
image_1 = "examples/example_image_real_1.jpg.webp"
image_2 = "examples/example_image_real_2.jpg.webp"
image_3 = "examples/example_image_real_3.jpg"
image_4 = "examples/example_image_real_4.jpg.webp"
gr.Examples(
examples=[
[title_1, image_1, text_real_1 + "\n\n" + text_real_2],
[title_1, image_2, text_real_1 + "\n\n" + text_llm_modification],
[title_1, image_3, text_real_1 + "\n\n" + text_llm_topic],
[title_4, image_4, text_llm_entities],
],
inputs=[news_title, news_image, news_content],
label="Examples",
example_labels=[
"2 real news",
"1 real news + 1 LLM modification-based news",
"1 real news + 1 LLM topic-based news",
"1 LLM changed-entities news",
],
)
demo.launch(share=True)
|