Spaces:
Sleeping
Sleeping
Update app.py
Browse files21 unmodified lines22232425262725262728293 unmodified lines333435373637384039404143424344453 unmodified lines495051525354555621 unmodified lines """ try: api_key = os.getenv("NEWSAPI_KEY") # Fetch the API key from environment variables if not api_key: return "NewsAPI key is missing. Please set the NEWSAPI_KEY environment variable." api_key = 'd43565ac0e674145a22c5f0de4dcd69c' # Construct the API URL url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}" # Make the API request3 unmodified lines # Parse the JSON response data = response.json() # Extract relevant information (titles and URLs) # Check if the response contains articles articles = data.get("articles", []) if not articles: return "No news articles found for the given keyword." return f"No news articles found for the keyword '{keyword}'." # Format the results result = "Here are the latest news articles:\n" result = f"Here are the latest news articles about '{keyword}':\n" for i, article in enumerate(articles, 1): title = article.get("title", "No title") url = article.get("url", "#")3 unmodified lines except requests.exceptions.RequestException as e: return f"Error fetching news articles: {str(e)}" except Exception as e: return f"An unexpected error occurred: {str(e)}"
final_answer = FinalAnswerTool()app.py+7-6