|
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool |
|
import datetime |
|
import requests |
|
import pytz |
|
import yaml |
|
from tools.final_answer import FinalAnswerTool |
|
from Gradio_UI import GradioUI |
|
from transformers import pipeline |
|
|
|
|
|
@tool |
|
def get_topic() -> str: |
|
"""A tool to prompt the user for a topic. |
|
Returns: |
|
str: The topic entered by the user. |
|
""" |
|
|
|
topic = input("Enter the topic you want to explore: ") |
|
return topic |
|
|
|
|
|
@tool |
|
def search_web(query: str) -> str: |
|
"""A tool that searches the web for the provided query. |
|
Args: |
|
query: The search query. |
|
Returns: |
|
str: A snippet from the top search result. |
|
""" |
|
|
|
results = DuckDuckGoSearchTool.search(query=query) |
|
if results and len(results) > 0: |
|
|
|
return results[0].get('snippet', "No snippet available.") |
|
return "No results found." |
|
|
|
|
|
@tool |
|
def summarise_content(text: str) -> str: |
|
"""A tool that summarizes text content. |
|
Args: |
|
text: The content to summarize. |
|
Returns: |
|
str: The summarized text. |
|
""" |
|
summarizer = pipeline("summarization") |
|
|
|
summary = summarizer(text, max_length=130, min_length=30, do_sample=False) |
|
return summary[0]['summary_text'] |
|
|
|
|
|
@tool |
|
def write_social_media_post(summary: str) -> str: |
|
"""A tool that crafts a social media post using the provided summary. |
|
Args: |
|
summary: The summary text to base the post on. |
|
Returns: |
|
str: A drafted social media post. |
|
""" |
|
|
|
post = f"Check this out: {summary} #news #update" |
|
return post |
|
|
|
final_answer = FinalAnswerTool() |
|
|
|
|
|
model = HfApiModel( |
|
max_tokens=2096, |
|
temperature=0.5, |
|
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', |
|
) |
|
|
|
|
|
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
|
|
|
with open("prompts.yaml", 'r') as stream: |
|
prompt_templates = yaml.safe_load(stream) |
|
|
|
|
|
agent = CodeAgent( |
|
model=model, |
|
tools=[ |
|
final_answer, |
|
get_topic, |
|
search_web, |
|
summarise_content, |
|
write_social_media_post |
|
], |
|
max_steps=6, |
|
verbosity_level=1, |
|
prompt_templates=prompt_templates |
|
) |
|
|
|
|
|
GradioUI(agent).launch() |
|
|