Update app.py
Browse files
app.py
CHANGED
|
@@ -4,104 +4,54 @@ import requests
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
| 9 |
@tool
|
| 10 |
-
def
|
| 11 |
-
|
|
|
|
| 12 |
Args:
|
| 13 |
-
|
| 14 |
-
max_keywords: Максимальное количество возвращаемых ключевых слов
|
| 15 |
-
"""
|
| 16 |
-
import random
|
| 17 |
-
from collections import deque
|
| 18 |
-
|
| 19 |
-
# Инициализация компонентов генерации
|
| 20 |
-
base_words = [w.strip().lower() for w in topic.split() if len(w) > 2]
|
| 21 |
-
if not base_words:
|
| 22 |
-
return "Необходимо указать более конкретную тему"
|
| 23 |
-
|
| 24 |
-
# Паттерны для генерации
|
| 25 |
-
patterns = [
|
| 26 |
-
["вопросы", "что такое", "как сделать", "почему", "когда", "где найти"],
|
| 27 |
-
["топ", "лучшие", "рейтинг", "самые популярные"],
|
| 28 |
-
["методы", "способы", "технологии", "решения"],
|
| 29 |
-
["обзор", "отзывы", "сравнение", "виды"],
|
| 30 |
-
["курс", "урок", "обучение", "самоучитель"],
|
| 31 |
-
["2025", "новинки", "тренды", "будущее"],
|
| 32 |
-
["для начинающих", "профессиональные", "премиум"],
|
| 33 |
-
["бесплатно", "купить", "заказать", "стоимость"],
|
| 34 |
-
["преимущества", "недостатки", "польза", "вред"],
|
| 35 |
-
["идеи", "примеры", "кейсы", "истории успеха"]
|
| 36 |
-
]
|
| 37 |
-
|
| 38 |
-
# Генерация вариантов
|
| 39 |
-
keywords = set()
|
| 40 |
-
queue = deque([(0, [], base_words)])
|
| 41 |
-
|
| 42 |
-
while queue:
|
| 43 |
-
level, current, remaining = queue.popleft()
|
| 44 |
-
|
| 45 |
-
if level >= 3 or not remaining:
|
| 46 |
-
if current:
|
| 47 |
-
keywords.add(" ".join(current))
|
| 48 |
-
continue
|
| 49 |
-
|
| 50 |
-
# Добавляем паттерны
|
| 51 |
-
if level == 0:
|
| 52 |
-
for pattern_group in patterns:
|
| 53 |
-
for p in pattern_group:
|
| 54 |
-
new_phrase = [p] + current
|
| 55 |
-
queue.append((level+1, new_phrase, remaining))
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
random.shuffle(result[:len(result)//2])
|
| 75 |
-
|
| 76 |
-
return ", ".join(result[:max(max_keywords, 1)])
|
| 77 |
|
| 78 |
-
# Initialize components
|
| 79 |
-
final_answer = FinalAnswerTool()
|
| 80 |
|
| 81 |
-
|
| 82 |
model = HfApiModel(
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
)
|
| 88 |
|
| 89 |
-
|
|
|
|
| 90 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 91 |
|
| 92 |
-
# Load prompt templates
|
| 93 |
with open("prompts.yaml", 'r') as stream:
|
| 94 |
prompt_templates = yaml.safe_load(stream)
|
| 95 |
-
|
| 96 |
-
# Create agent
|
| 97 |
agent = CodeAgent(
|
| 98 |
-
tools=[
|
| 99 |
-
image_generation_tool,
|
| 100 |
-
generate_keywords,
|
| 101 |
-
final_answer,
|
| 102 |
-
DuckDuckGoSearchTool()
|
| 103 |
-
],
|
| 104 |
model=model,
|
|
|
|
| 105 |
max_steps=6,
|
| 106 |
verbosity_level=1,
|
| 107 |
grammar=None,
|
|
@@ -111,5 +61,5 @@ agent = CodeAgent(
|
|
| 111 |
prompt_templates=prompt_templates
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
GradioUI(agent).launch()
|
|
|
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
+
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity!
|
| 11 |
@tool
|
| 12 |
+
def keyword_generator(arg1:str)-> str: # it's important to specify the return type
|
| 13 |
+
# Keep this format for the tool description / args description but feel free to modify the tool
|
| 14 |
+
"""A tool that generate keywords by the topic
|
| 15 |
Args:
|
| 16 |
+
arg1: generate 10 most relevant keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
"""
|
| 19 |
+
return "keywords"
|
| 20 |
+
|
| 21 |
+
@tool
|
| 22 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 24 |
+
Args:
|
| 25 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
+
"""
|
| 27 |
+
try:
|
| 28 |
+
# Create timezone object
|
| 29 |
+
tz = pytz.timezone(timezone)
|
| 30 |
+
# Get current time in that timezone
|
| 31 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
final_answer = FinalAnswerTool()
|
| 38 |
model = HfApiModel(
|
| 39 |
+
max_tokens=2096,
|
| 40 |
+
temperature=0.5,
|
| 41 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 42 |
+
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
+
|
| 46 |
+
# Import tool from Hub
|
| 47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 48 |
|
|
|
|
| 49 |
with open("prompts.yaml", 'r') as stream:
|
| 50 |
prompt_templates = yaml.safe_load(stream)
|
| 51 |
+
|
|
|
|
| 52 |
agent = CodeAgent(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
model=model,
|
| 54 |
+
tools=[final_answer, keyword_generator], # add your tools here (don't remove final_answer)
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
|
|
| 61 |
prompt_templates=prompt_templates
|
| 62 |
)
|
| 63 |
|
| 64 |
+
|
| 65 |
GradioUI(agent).launch()
|