Spaces:
Sleeping
Sleeping
Update app.py
Browse files123451 unmodified line789101112131415161718192021222324252627282930313233343536373839404142434418 unmodified lines63646566676869707172737475767778798020 unmodified lines10110210310410510610710810911011111211322 unmodified lines136137138139140141142from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,toolimport osimport datetimeimport requestsimport pytz1 unmodified linefrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUIfrom transformers import pipelinefrom diffusers import StableDiffusionPipelineimport torchpipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-1", torch_dtype=torch.float16)pipe = pipe.to("cuda") # Use GPU if available@tooldef generate_image(prompt: str) -> str:"""A tool that generates an image based on a text prompt.Args:prompt: The text prompt to generate an image from.Returns:The file path or URL of the generated image."""try:# Generate the imageimage = pipe(prompt).images[0]# Save the image to a fileimage_path = f"generated_images/{prompt[:20]}.png" # Save with a unique nameimage.save(image_path)return image_pathexcept Exception as e:return f"Error generating image: {str(e)}"summarizer = pipeline("summarization")18 unmodified lines@tooldef news_aggregator(keyword: str, max_results: int = 5) -> str:"""A tool that fetches the latest news articles, summarizes them and generates images.Args:keyword: The topic or keyword to search for (e.g., "AI", "technology").max_results: The maximum number of articles to return (default is 5).Returns:A string containing the titles, summaries, and image paths of the latest news articles."""try:api_key = 'd43565ac0e674145a22c5f0de4dcd69c'# Construct the API URLurl = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"20 unmodified lines# Summarize the articlesummary = summarize_article(content)# Generate an image based on the titleimage_path = generate_image(title)# Format the resultsresult += f"{i}. {title}\n"result += f" Summary: {summary}\n"result += f" Image: {image_path}\n"result += f" URL: {url}\n"return result22 unmodified lines# Agent and toolsagent = CodeAgent(model=model,tools=[final_answer, news_aggregator, summarize_article, generate_image], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,12341 unmodified line678910111218 unmodified lines31323334353637383940414243444546474820 unmodified lines69707172737475767722 unmodified lines100101102103104105106from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,toolimport datetimeimport requestsimport pytz1 unmodified linefrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUIfrom transformers import pipelinesummarizer = pipeline("summarization")18 unmodified lines@tooldef news_aggregator(keyword: str, max_results: int = 5) -> str:"""A tool that fetches the latest news articles and summarizes them.Args:keyword: The topic or keyword to search for (e.g., "AI", "technology").max_results: The maximum number of articles to return (default is 5).Returns:A string containing the titles, summaries, and URLs of the latest news articles."""try:api_key = "d43565ac0e674145a22c5f0de4dcd69c" # Replace with your actual NewsAPI key# Construct the API URLurl = f"https://newsapi.org/v2/everything?q={keyword}&pageSize={max_results}&apiKey={api_key}"20 unmodified lines# Summarize the articlesummary = summarize_article(content)# Format the resultsresult += f"{i}. {title}\n"result += f" Summary: {summary}\n"result += f" URL: {url}\n"return result22 unmodified lines# Agent and toolsagent = CodeAgent(model=model,tools=[final_answer, news_aggregator, summarize_article], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,
app.py+4-40