| import gradio as gr |
| from diffusers import DiffusionPipeline |
| import torch |
| import requests |
| from bs4 import BeautifulSoup |
|
|
| |
| |
| pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, use_safetensors=True, variant="fp16") |
| pipe.to("cuda") |
| |
| import gradio as gr |
| from diffusers import DiffusionPipeline |
| import torch |
| import requests |
| from bs4 import BeautifulSoup |
|
|
| |
| def generate_image(timeline, category): |
| |
| prompt = f"{category} in the year {timeline}" |
| negative_prompt = "deformed, low resolution, blurry" |
| |
| |
| image = pipe(prompt, negative_prompt=negative_prompt).images[0] |
| image.save("generated_image.png") |
| |
| return "generated_image.png" |
|
|
| |
| def scrape_news(timeline, category): |
| |
| search_query = f"{category} events in {timeline}" |
| url = f"https://en.wikipedia.org/wiki/Special:Search?search={search_query}" |
| |
| |
| response = requests.get(url) |
| soup = BeautifulSoup(response.text, 'html.parser') |
| |
| |
| search_results = soup.find_all('div', class_='mw-search-result-heading') |
| |
| |
| news_content = [] |
| for result in search_results[:5]: |
| 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) |
|
|
| |
| def generate_content(timeline, category): |
| |
| generated_image_path = generate_image(timeline, category) |
| |
| |
| news_content = scrape_news(timeline, category) |
| |
| return generated_image_path, news_content |
|
|
| |
| 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] |
| ) |
|
|
| |
| demo.launch() |