AC-Angelo93 commited on
Commit
ecd4181
·
verified ·
1 Parent(s): eb3489d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -8
app.py CHANGED
@@ -1,4 +1,5 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
2
  import datetime
3
  import requests
4
  import pytz
@@ -7,22 +8,74 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is my news aggregator tool!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  @tool
12
  def news_aggregator(keyword: str, max_results: int = 5) -> str:
13
  """
14
- A tool that fetches the latest news articles related to a specific keyword.
15
 
16
  Args:
17
  keyword: The topic or keyword to search for (e.g., "AI", "technology").
18
  max_results: The maximum number of articles to return (default is 5).
19
 
20
  Returns:
21
- A string containing the titles and URLs of the latest news articles.
22
  """
23
  try:
24
 
25
  api_key = 'd43565ac0e674145a22c5f0de4dcd69c'
 
26
  # Construct the API URL
27
  url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
28
 
@@ -38,12 +91,24 @@ def news_aggregator(keyword: str, max_results: int = 5) -> str:
38
  if not articles:
39
  return f"No news articles found for the keyword '{keyword}'."
40
 
41
- # Format the results
42
  result = f"Here are the latest news articles about '{keyword}':\n"
43
  for i, article in enumerate(articles, 1):
44
  title = article.get("title", "No title")
45
  url = article.get("url", "#")
46
- result += f"{i}. {title}\n {url}\n"
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  return result
49
 
@@ -51,8 +116,6 @@ def news_aggregator(keyword: str, max_results: int = 5) -> str:
51
  return f"Error fetching news articles: {str(e)}"
52
  except Exception as e:
53
  return f"An unexpected error occurred: {str(e)}"
54
-
55
-
56
  final_answer = FinalAnswerTool()
57
 
58
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
@@ -73,7 +136,7 @@ with open("prompts.yaml", 'r') as stream:
73
  # Agent and tools
74
  agent = CodeAgent(
75
  model=model,
76
- tools=[final_answer, news_aggregator], ## add your tools here (don't remove final answer)
77
  max_steps=6,
78
  verbosity_level=1,
79
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ import os
3
  import datetime
4
  import requests
5
  import pytz
 
8
 
9
  from Gradio_UI import GradioUI
10
 
11
+ from transformers import pipeline
12
+ from diffusers import StableDiffusionPipeline
13
+ import torch
14
+
15
+
16
+ pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)
17
+ pipe = pipe.to("cuda") # Use GPU if available
18
+
19
+ @tool
20
+ def generate_image(prompt: str) -> str:
21
+ """
22
+ A tool that generates an image based on a text prompt.
23
+
24
+ Args:
25
+ prompt: The text prompt to generate an image from.
26
+
27
+ Returns:
28
+ The file path or URL of the generated image.
29
+ """
30
+ try:
31
+ # Generate the image
32
+ image = pipe(prompt).images[0]
33
+
34
+ # Save the image to a file
35
+ image_path = f"generated_images/{prompt[:20]}.png" # Save with a unique name
36
+ image.save(image_path)
37
+
38
+ return image_path
39
+ except Exception as e:
40
+ return f"Error generating image: {str(e)}"
41
+
42
+
43
+ summarizer = pipeline("summarization")
44
+
45
+ @tool
46
+ def summarize_article(article_text: str) -> str:
47
+ """
48
+ A tool that summarizes the content of a news article.
49
+
50
+ Args:
51
+ article_text: The text content of the article to summarize.
52
+
53
+ Returns:
54
+ A summarized version of the article.
55
+ """
56
+ try:
57
+ # Summarize the article text
58
+ summary = summarizer(article_text, max_length=50, min_length=25, do_sample=False)
59
+ return summary[0]['summary_text']
60
+ except Exception as e:
61
+ return f"Error summarizing article: {str(e)}"
62
+
63
  @tool
64
  def news_aggregator(keyword: str, max_results: int = 5) -> str:
65
  """
66
+ A tool that fetches the latest news articles, summarizes them and generates images.
67
 
68
  Args:
69
  keyword: The topic or keyword to search for (e.g., "AI", "technology").
70
  max_results: The maximum number of articles to return (default is 5).
71
 
72
  Returns:
73
+ A string containing the titles, summaries, and image paths of the latest news articles.
74
  """
75
  try:
76
 
77
  api_key = 'd43565ac0e674145a22c5f0de4dcd69c'
78
+
79
  # Construct the API URL
80
  url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
81
 
 
91
  if not articles:
92
  return f"No news articles found for the keyword '{keyword}'."
93
 
94
+ # Process each article
95
  result = f"Here are the latest news articles about '{keyword}':\n"
96
  for i, article in enumerate(articles, 1):
97
  title = article.get("title", "No title")
98
  url = article.get("url", "#")
99
+ content = article.get("content", "") # Get the article content
100
+
101
+ # Summarize the article
102
+ summary = summarize_article(content)
103
+
104
+ # Generate an image based on the title
105
+ image_path = generate_image(title)
106
+
107
+ # Format the results
108
+ result += f"{i}. {title}\n"
109
+ result += f" Summary: {summary}\n"
110
+ result += f" Image: {image_path}\n"
111
+ result += f" URL: {url}\n"
112
 
113
  return result
114
 
 
116
  return f"Error fetching news articles: {str(e)}"
117
  except Exception as e:
118
  return f"An unexpected error occurred: {str(e)}"
 
 
119
  final_answer = FinalAnswerTool()
120
 
121
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
136
  # Agent and tools
137
  agent = CodeAgent(
138
  model=model,
139
+ tools=[final_answer, news_aggregator, summarize_article, generate_image], ## add your tools here (don't remove final answer)
140
  max_steps=6,
141
  verbosity_level=1,
142
  grammar=None,