Spaces:
Running
Running
import os | |
import gradio as gr | |
import openai | |
import requests | |
from PIL import Image | |
import re | |
from src.application.content_detection import generate_analysis_report | |
from src.application.url_reader import URLReader | |
from src.application.content_generation import generate_content, replace_text | |
# from dotenv import load_dotenv | |
# load_dotenv() | |
# AZURE_OPENAI_API_KEY = os.getenv('AZURE_OPENAI_API_KEY') | |
# AZURE_OPENAI_ENDPOINT = os.getenv('AZURE_OPENAI_ENDPOINT') | |
# AZURE_OPENAI_API_VERSION = os.getenv('AZURE_OPENAI_API_VERSION') | |
# client = openai.AzureOpenAI( | |
# api_version = AZURE_OPENAI_API_VERSION, | |
# api_key = AZURE_OPENAI_API_KEY, | |
# azure_endpoint = AZURE_OPENAI_ENDPOINT, | |
# ) | |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY') | |
SEARCH_ENGINE_ID = os.getenv('SEARCH_ENGINE_ID') | |
AZURE_OPENAI_MODEL = ["gpt-4o-mini", "gpt-4o"] | |
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'} | |
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: | |
print(f"Error loading image from {content.top_image}") | |
except (requests.exceptions.RequestException, FileNotFoundError) as e: | |
print(f"Error fetching image: {e}") | |
return content.title, content.text, image | |
def show_detailed_analysis(title): | |
return f"More details of {title} will be shown here." | |
# Define the GUI | |
with gr.Blocks() as demo: | |
gr.Markdown("# FAKE NEWS DETECTION") | |
with gr.Row(): | |
# SETTINGS | |
with gr.Column(scale=1): | |
with gr.Accordion("Settings"): | |
gr.Markdown("This tool generates fake news by modifying the content of a given URL.") | |
with gr.Accordion("1. Enter a URL"): | |
url_input = gr.Textbox( | |
label="URL", | |
value="https://bbc.com/future/article/20250110-how-often-you-should-wash-your-towels-according-to-science", | |
) | |
load_button = gr.Button("Load URL") | |
with gr.Accordion("2. Select a content-generation model", open=True): | |
with gr.Row(): | |
text_generation_model = gr.Dropdown(choices=AZURE_OPENAI_MODEL, label="Text-generation model") | |
image_generation_model = gr.Dropdown(choices=["Dall-e", "Stable Diffusion"], label="Image-generation model") | |
generate_button = gr.Button("Random generation") | |
with gr.Accordion("3. Replace any terms", open=True): | |
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.Column(scale=1): | |
with gr.Accordion("Generated News Contents"): | |
detection_button = gr.Button("Check for fake news") | |
news_title = gr.Textbox(label="Title", value="") | |
news_image = gr.Image(label="Image") | |
news_content = gr.Textbox(label="Content", value="", lines=12) | |
# FAKE NEWS ANALYSIS REPORT | |
with gr.Column(scale=1): | |
with gr.Accordion("Fake News Analysis"): | |
html_out = gr.HTML() | |
detailed_analysis_button = gr.Button("Show detailed analysis...") | |
# 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_button.click(generate_content, | |
inputs=[text_generation_model, image_generation_model, news_title, news_content], | |
outputs=[news_title, news_content]) | |
detection_button.click(generate_analysis_report, | |
inputs=[news_title, news_content, news_image], | |
outputs=html_out) | |
detailed_analysis_button.click(show_detailed_analysis, | |
inputs=[news_title], | |
outputs=[html_out]) | |
# change Image | |
#url_input.change(load_image, inputs=url_input, outputs=image_view) | |
demo.launch() |