Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
-
import os
|
| 3 |
import datetime
|
| 4 |
import requests
|
| 5 |
import pytz
|
|
@@ -7,38 +6,7 @@ import yaml
|
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 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 |
|
|
@@ -63,18 +31,18 @@ def summarize_article(article_text: str) -> str:
|
|
| 63 |
@tool
|
| 64 |
def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
| 65 |
"""
|
| 66 |
-
A tool that fetches the latest news articles
|
| 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
|
| 74 |
"""
|
| 75 |
try:
|
| 76 |
|
| 77 |
-
api_key =
|
| 78 |
|
| 79 |
# Construct the API URL
|
| 80 |
url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
|
|
@@ -101,13 +69,9 @@ def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
|
| 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
|
|
@@ -136,7 +100,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 136 |
# Agent and tools
|
| 137 |
agent = CodeAgent(
|
| 138 |
model=model,
|
| 139 |
-
tools=[final_answer, news_aggregator, summarize_article
|
| 140 |
max_steps=6,
|
| 141 |
verbosity_level=1,
|
| 142 |
grammar=None,
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
|
|
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
|
|
|
| 9 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
summarizer = pipeline("summarization")
|
| 12 |
|
|
|
|
| 31 |
@tool
|
| 32 |
def news_aggregator(keyword: str, max_results: int = 5) -> str:
|
| 33 |
"""
|
| 34 |
+
A tool that fetches the latest news articles and summarizes them.
|
| 35 |
|
| 36 |
Args:
|
| 37 |
keyword: The topic or keyword to search for (e.g., "AI", "technology").
|
| 38 |
max_results: The maximum number of articles to return (default is 5).
|
| 39 |
|
| 40 |
Returns:
|
| 41 |
+
A string containing the titles, summaries, and URLs of the latest news articles.
|
| 42 |
"""
|
| 43 |
try:
|
| 44 |
|
| 45 |
+
api_key = "d43565ac0e674145a22c5f0de4dcd69c" # Replace with your actual NewsAPI key
|
| 46 |
|
| 47 |
# Construct the API URL
|
| 48 |
url = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"
|
|
|
|
| 69 |
# Summarize the article
|
| 70 |
summary = summarize_article(content)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
# Format the results
|
| 73 |
result += f"{i}. {title}\n"
|
| 74 |
result += f" Summary: {summary}\n"
|
|
|
|
| 75 |
result += f" URL: {url}\n"
|
| 76 |
|
| 77 |
return result
|
|
|
|
| 100 |
# Agent and tools
|
| 101 |
agent = CodeAgent(
|
| 102 |
model=model,
|
| 103 |
+
tools=[final_answer, news_aggregator, summarize_article], ## add your tools here (don't remove final answer)
|
| 104 |
max_steps=6,
|
| 105 |
verbosity_level=1,
|
| 106 |
grammar=None,
|