Update app.py
Browse files
app.py
CHANGED
|
@@ -89,7 +89,7 @@ def is_english(text):
|
|
| 89 |
return english_word_count >= 5 or english_ratio > 0.25 # More stringent criteria
|
| 90 |
|
| 91 |
# News search functions
|
| 92 |
-
def
|
| 93 |
"""Search for the latest news using Tavily based on the provided query."""
|
| 94 |
search_tool = TavilySearchResults(max_results=10)
|
| 95 |
|
|
@@ -484,12 +484,12 @@ def create_news_search_workflow():
|
|
| 484 |
workflow = StateGraph(NewsState)
|
| 485 |
|
| 486 |
# Add nodes
|
| 487 |
-
workflow.add_node("
|
| 488 |
workflow.add_node("parse_news_items", parse_news_items)
|
| 489 |
|
| 490 |
# Add edges
|
| 491 |
-
workflow.add_edge(START, "
|
| 492 |
-
workflow.add_edge("
|
| 493 |
workflow.add_edge("parse_news_items", END)
|
| 494 |
|
| 495 |
return workflow.compile()
|
|
@@ -524,7 +524,7 @@ def create_blog_generator_workflow():
|
|
| 524 |
|
| 525 |
return workflow.compile()
|
| 526 |
|
| 527 |
-
def
|
| 528 |
"""Main function to generate news blog based on a specified topic."""
|
| 529 |
# Set API keys if provided
|
| 530 |
if groq_api_key:
|
|
@@ -574,11 +574,10 @@ def generate_ai_news_blog(groq_api_key=None, tavily_api_key=None, date=None, top
|
|
| 574 |
|
| 575 |
# Check if we have any news items
|
| 576 |
if not news_contents:
|
| 577 |
-
return "No English language
|
| 578 |
|
| 579 |
# Format news content for the blog generator
|
| 580 |
-
formatted_content = "\n\n".join([
|
| 581 |
-
f"TITLE: {item['title']}\nSOURCE: {item['source']}\nURL: {item['url']}\nDESCRIPTION: {item['description']}\nCONTENT: {item['content'][:2000]}..."
|
| 582 |
for item in news_contents
|
| 583 |
])
|
| 584 |
|
|
@@ -593,22 +592,22 @@ def generate_ai_news_blog(groq_api_key=None, tavily_api_key=None, date=None, top
|
|
| 593 |
|
| 594 |
# Gradio UI
|
| 595 |
def create_gradio_interface():
|
| 596 |
-
"""Create a Gradio interface for the
|
| 597 |
|
| 598 |
def run_generation(groq_key, tavily_key, selected_date, topic):
|
| 599 |
if not groq_key or not tavily_key:
|
| 600 |
return "Please provide both API keys."
|
| 601 |
|
| 602 |
try:
|
| 603 |
-
result =
|
| 604 |
return result
|
| 605 |
except Exception as e:
|
| 606 |
return f"Error generating blog: {str(e)}"
|
| 607 |
|
| 608 |
# Create the interface
|
| 609 |
-
with gr.Blocks(title="
|
| 610 |
-
gr.Markdown("#
|
| 611 |
-
gr.Markdown("Generate a daily roundup of
|
| 612 |
|
| 613 |
with gr.Row():
|
| 614 |
with gr.Column():
|
|
@@ -616,18 +615,18 @@ def create_gradio_interface():
|
|
| 616 |
tavily_key = gr.Textbox(label="Tavily API Key", placeholder="Enter your Tavily API key", type="password")
|
| 617 |
date_picker = gr.Textbox(label="Date (YYYY-MM-DD)", placeholder="Leave empty for today's date",
|
| 618 |
value=datetime.now().strftime("%Y-%m-%d"))
|
| 619 |
-
topic_input = gr.Textbox(label="Topic", placeholder="Enter a topic for news generation") #
|
| 620 |
with gr.Row():
|
| 621 |
-
generate_button = gr.Button("Generate
|
| 622 |
clear_button = gr.Button("Clear Output")
|
| 623 |
|
| 624 |
with gr.Column():
|
| 625 |
status_text = gr.Textbox(label="Status", placeholder="Ready to generate", interactive=False)
|
| 626 |
-
output_md = gr.Markdown("Your
|
| 627 |
|
| 628 |
# Add loading state and status updates
|
| 629 |
generate_button.click(
|
| 630 |
-
fn=lambda: "Generating
|
| 631 |
inputs=None,
|
| 632 |
outputs=status_text,
|
| 633 |
queue=False
|
|
|
|
| 89 |
return english_word_count >= 5 or english_ratio > 0.25 # More stringent criteria
|
| 90 |
|
| 91 |
# News search functions
|
| 92 |
+
def search_general_news(state: NewsState):
|
| 93 |
"""Search for the latest news using Tavily based on the provided query."""
|
| 94 |
search_tool = TavilySearchResults(max_results=10)
|
| 95 |
|
|
|
|
| 484 |
workflow = StateGraph(NewsState)
|
| 485 |
|
| 486 |
# Add nodes
|
| 487 |
+
workflow.add_node("search_general_news", search_general_news)
|
| 488 |
workflow.add_node("parse_news_items", parse_news_items)
|
| 489 |
|
| 490 |
# Add edges
|
| 491 |
+
workflow.add_edge(START, "search_general_news")
|
| 492 |
+
workflow.add_edge("search_general_news", "parse_news_items")
|
| 493 |
workflow.add_edge("parse_news_items", END)
|
| 494 |
|
| 495 |
return workflow.compile()
|
|
|
|
| 524 |
|
| 525 |
return workflow.compile()
|
| 526 |
|
| 527 |
+
def generate_general_news_blog(groq_api_key=None, tavily_api_key=None, date=None, topic=None):
|
| 528 |
"""Main function to generate news blog based on a specified topic."""
|
| 529 |
# Set API keys if provided
|
| 530 |
if groq_api_key:
|
|
|
|
| 574 |
|
| 575 |
# Check if we have any news items
|
| 576 |
if not news_contents:
|
| 577 |
+
return "No English language news items found for the specified date. Please try a different date."
|
| 578 |
|
| 579 |
# Format news content for the blog generator
|
| 580 |
+
formatted_content = "\n\n".join([f"TITLE: {item['title']}\nSOURCE: {item['source']}\nURL: {item['url']}\nDESCRIPTION: {item['description']}\nCONTENT: {item['content'][:2000]}..."
|
|
|
|
| 581 |
for item in news_contents
|
| 582 |
])
|
| 583 |
|
|
|
|
| 592 |
|
| 593 |
# Gradio UI
|
| 594 |
def create_gradio_interface():
|
| 595 |
+
"""Create a Gradio interface for the General News Blog Generator"""
|
| 596 |
|
| 597 |
def run_generation(groq_key, tavily_key, selected_date, topic):
|
| 598 |
if not groq_key or not tavily_key:
|
| 599 |
return "Please provide both API keys."
|
| 600 |
|
| 601 |
try:
|
| 602 |
+
result = generate_general_news_blog(groq_key, tavily_key, selected_date, topic) # Pass topic to the function
|
| 603 |
return result
|
| 604 |
except Exception as e:
|
| 605 |
return f"Error generating blog: {str(e)}"
|
| 606 |
|
| 607 |
# Create the interface
|
| 608 |
+
with gr.Blocks(title="General News Blog Generator") as demo:
|
| 609 |
+
gr.Markdown("# General News Blog Generator")
|
| 610 |
+
gr.Markdown("Generate a daily roundup of news articles on various topics.")
|
| 611 |
|
| 612 |
with gr.Row():
|
| 613 |
with gr.Column():
|
|
|
|
| 615 |
tavily_key = gr.Textbox(label="Tavily API Key", placeholder="Enter your Tavily API key", type="password")
|
| 616 |
date_picker = gr.Textbox(label="Date (YYYY-MM-DD)", placeholder="Leave empty for today's date",
|
| 617 |
value=datetime.now().strftime("%Y-%m-%d"))
|
| 618 |
+
topic_input = gr.Textbox(label="Topic", placeholder="Enter a topic for news generation") # Input for topic
|
| 619 |
with gr.Row():
|
| 620 |
+
generate_button = gr.Button("Generate News Blog", variant="primary")
|
| 621 |
clear_button = gr.Button("Clear Output")
|
| 622 |
|
| 623 |
with gr.Column():
|
| 624 |
status_text = gr.Textbox(label="Status", placeholder="Ready to generate", interactive=False)
|
| 625 |
+
output_md = gr.Markdown("Your news blog will appear here.")
|
| 626 |
|
| 627 |
# Add loading state and status updates
|
| 628 |
generate_button.click(
|
| 629 |
+
fn=lambda: "Generating News Blog... This may take several minutes.",
|
| 630 |
inputs=None,
|
| 631 |
outputs=status_text,
|
| 632 |
queue=False
|