azamat's picture
Update app.py
6aa022f verified
from smolagents import CodeAgent, load_tool, tool, LiteLLMModel
import os
import datetime
import requests
import pytz
import yaml
import markdown
from openai import OpenAI
from tools.web_search import DuckDuckGoSearchTool
from tools.final_answer import FinalAnswerTool
from tools.visit_webpage import VisitWebpageTool
from Gradio_UI import GradioUI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.getenv("OPENROUTER_API_KEY"),
)
model = LiteLLMModel(
"openrouter/google/gemini-2.0-flash-001",
temperature=0.2,
api_key=os.getenv("OPENROUTER_API_KEY")
)
@tool
def analyze_petition(petition: str, best_practices: str) -> str:
"""A tool that receives a political petition text and analyses it for improvements to make it follow the best practices and returns optimized petition using Markdown.
Args:
petition: A string containing a political petition text.
best_practices: A string containing a list of best practices for creating efficient, appealing and call-to-action political petition.
"""
completion = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
messages=[
{
"role": "user",
"content": f"""
Optimize the provided political petition to meet the best practices and format the optimized petition in Markdown by preserving all the sections from the initial petition.
Initial petition:
{petition}
Best practices:
{best_practices}
"""
}
]
)
return completion.choices[0].message.content
@tool
def create_presentation(petition: str) -> str:
"""A tool that receives a political petition text and generates slides presented in Markdown.
Args:
petition: A string containing a political petition text.
"""
completion = client.chat.completions.create(
model="google/gemini-2.0-flash-001",
messages=[
{
"role": "user",
"content": f"""
Take the provided political petition's text and create slides for the presentation outputing all slides in Markdown.
Political petition:
{petition}
"""
}
]
)
return completion.choices[0].message.content
final_answer = FinalAnswerTool()
search = DuckDuckGoSearchTool()
visit = VisitWebpageTool()
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
agent = CodeAgent(
model=model,
tools=[analyze_petition, search, visit, create_presentation, final_answer],
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
name=None,
description=None,
prompt_templates=prompt_templates
)
GradioUI(agent).launch()