Spaces:
Sleeping
Sleeping
| import os | |
| import gradio as gr | |
| import requests | |
| from PIL import Image | |
| from src.application.content_detection import NewsVerification | |
| from src.application.url_reader import URLReader | |
| from src.application.content_generation import generate_fake_image, generate_fake_text, replace_text | |
| GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY') | |
| SEARCH_ENGINE_ID = os.getenv('SEARCH_ENGINE_ID') | |
| 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'} | |
| 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 generate_analysis_report(news_title:str, news_content: str, news_image: Image): | |
| if news_image is not None: | |
| # Convert to PIL Image for easier saving | |
| img = Image.open(news_image) | |
| # Save the image (you can customize the filename) | |
| filepath = "example_image_input.jpg" # Or use a dynamic filename | |
| img.save(filepath) | |
| 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=12) | |
| # NEWS ANALYSIS REPORT | |
| with gr.Column(scale=2): | |
| with gr.Accordion("News Analysis"): | |
| detection_button = gr.Button("Verify news") | |
| detailed_analysis = gr.HTML("<br>"*40) | |
| # 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]) | |
| detection_button.click(generate_analysis_report, | |
| inputs=[news_title, news_content, news_image], | |
| outputs=[detailed_analysis]) | |
| # change Image | |
| #url_input.change(load_image, inputs=url_input, outputs=image_view) | |
| try: | |
| with open('examples/example_text_real.txt','r', encoding='utf-8') as file: | |
| text_real_1 = file.read() | |
| with open('examples/example_text_real_2.txt','r', encoding='utf-8') as file: | |
| text_real_2 = file.read() | |
| with open('examples/example_text_LLM_topic.txt','r', encoding='utf-8') as file: | |
| text_llm_topic = file.read() | |
| with open('examples/example_text_LLM_modification.txt','r', encoding='utf-8') as file: | |
| text_llm_modification = 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" | |
| 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" | |
| 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], | |
| ], | |
| 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", | |
| ], | |
| ) | |
| demo.launch(share=False) |