pakangels / app.py
iqra785's picture
Update app.py
5b5f28a verified
import gradio as gr
from diffusers import DiffusionPipeline
import torch
import requests
from bs4 import BeautifulSoup
# Step 3: Load the Diffusion Model for Image Generation
# Step 3: Load the Diffusion Model for Image Generation
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, use_safetensors=True, variant="fp16")
pipe.to("cuda")
# Step 2: Import Necessary Libraries
import gradio as gr
from diffusers import DiffusionPipeline
import torch
import requests
from bs4 import BeautifulSoup
# Step 4: Function to Generate Image Based on Timeline and Category
def generate_image(timeline, category):
# Customize the prompt based on the timeline and category
prompt = f"{category} in the year {timeline}"
negative_prompt = "deformed, low resolution, blurry"
# Generate the image using the prompt
image = pipe(prompt, negative_prompt=negative_prompt).images[0]
image.save("generated_image.png")
return "generated_image.png"
# Step 5: Function to Scrape News Based on Timeline and Category
def scrape_news(timeline, category):
# Format the search query
search_query = f"{category} events in {timeline}"
url = f"https://en.wikipedia.org/wiki/Special:Search?search={search_query}"
# Send a request to the Wikipedia search page
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract search results or relevant content
search_results = soup.find_all('div', class_='mw-search-result-heading')
# Compile news content
news_content = []
for result in search_results[:5]: # Limit to first 5 results
title = result.find('a').get_text()
link = "https://en.wikipedia.org" + result.find('a')['href']
news_content.append(f"{title}: {link}")
return "\n".join(news_content)
# Step 6: Function to Generate Content (Image + News) Based on User Input
def generate_content(timeline, category):
# Generate Image
generated_image_path = generate_image(timeline, category)
# Scrape News
news_content = scrape_news(timeline, category)
return generated_image_path, news_content
# Step 7: Create the Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("# Time Machine - Explore Historical Timelines!")
timeline = gr.Slider(1900, 2024, value=2000, step=1, label="Select Year")
category = gr.Radio(["Wars", "Tech", "Fashion"], label="Choose Category")
generate_button = gr.Button("Generate")
image_output = gr.Image()
news_output = gr.Textbox()
generate_button.click(
fn=generate_content,
inputs=[timeline, category],
outputs=[image_output, news_output]
)
# Step 8: Launch the Gradio App
demo.launch()