import gradio as gr from utils.news_fetcher import fetch_news from utils.blog_writer import generate_blog_with_tone, generate_long_blog_with_tone, generate_tags, convert_to_audio, fact_check_blog, extend_single_blog import tempfile blog_cache = {} def generate_blogs(topic, long_form, tone): news_list = fetch_news(topic) output = "" full_text = "" fact_checks = "" blog_cache.clear() for idx, (title, summary) in enumerate(news_list): if long_form: blog = generate_long_blog_with_tone(title, summary, tone) summarized = summary else: blog, summarized = generate_blog_with_tone(title, summary, tone) tags = generate_tags(title, summarized) facts = fact_check_blog(blog) blog_entry = f"### 📰 {title}\n\n{blog}\n\n**Tags:** {tags}\n\n**Fact Check:** {facts}\n\n---\n" output += blog_entry full_text += f"{title}\n{blog}\nTags: {tags}\nFact Check: {facts}\n\n" fact_checks += f"{title} → {facts}\n" blog_cache[str(idx)] = {"title": title, "content": blog} return output, convert_to_audio(full_text), save_to_markdown(output), fact_checks, list(blog_cache.keys()) def save_to_markdown(content): with tempfile.NamedTemporaryFile(delete=False, suffix=".md", mode='w') as f: f.write(content) return f.name def extend_selected_blog(selected_id): if selected_id in blog_cache: blog_data = blog_cache[selected_id] extended_blog = extend_single_blog(blog_data["title"], blog_data["content"]) return f"## Extended Blog: {blog_data['title']}\n\n{extended_blog}" return "Please select a blog to extend." with gr.Blocks() as demo: gr.Markdown("# 🧠 AI Blog Generator from Real News with Fact Checker + Blog Extender + Tone Selector") with gr.Row(): topic_choice = gr.Dropdown(choices=["Technology", "Health", "Finance", "Science", "Entertainment"], label="Pick a Topic") custom_topic = gr.Textbox(label="Or enter a custom topic") long_form_toggle = gr.Checkbox(label="Generate Long-Form Blog Posts (600+ words)", value=False) tone_choice = gr.Dropdown( choices=["professional", "casual", "witty", "inspirational", "analytical"], label="Select Blog Tone", value="professional" ) md_output = gr.Markdown() audio_output = gr.Audio(label="Listen to Summary", type="filepath") file_output = gr.File(label="Download Blog Post (.md)") fact_output = gr.Textbox(label="Fact Check Summary") blog_selector = gr.Dropdown(label="Select a blog to extend", choices=[]) extended_output = gr.Markdown() def generate_all(topic_choice, custom_topic, long_form_toggle, tone_choice): topic = custom_topic if custom_topic else topic_choice blogs, audio_path, md_path, fact_summary, blog_ids = generate_blogs(topic, long_form_toggle, tone_choice) return blogs, audio_path, md_path, fact_summary, blog_ids generate_button = gr.Button("Generate Blog Posts") generate_button.click( fn=generate_all, inputs=[topic_choice, custom_topic, long_form_toggle, tone_choice], outputs=[md_output, audio_output, file_output, fact_output, blog_selector] ) extend_button = gr.Button("Extend Selected Blog Post") extend_button.click(fn=extend_selected_blog, inputs=blog_selector, outputs=extended_output) demo.launch()