Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,133 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 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
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -33,20 +144,17 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
-
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
|
| 50 |
# Import tool from Hub
|
| 51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
|
|
@@ -55,7 +163,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -65,5 +173,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
|
|
|
| 11 |
@tool
|
| 12 |
+
def task_manager(action: str, task_data: str = "") -> str:
|
| 13 |
+
"""Günlük görevleri yönetmek için kullanılan bir araç.
|
| 14 |
+
|
| 15 |
Args:
|
| 16 |
+
action: Yapılacak işlem ("add", "list", "complete", "delete" veya "help")
|
| 17 |
+
task_data: Görevle ilgili ek bilgiler (görev açıklaması, görev ID'si vs.)
|
| 18 |
+
|
| 19 |
+
Returns:
|
| 20 |
+
İşlem sonucu hakkında bilgi içeren bir metin
|
| 21 |
"""
|
| 22 |
+
import json
|
| 23 |
+
import os
|
| 24 |
+
from datetime import datetime
|
| 25 |
+
|
| 26 |
+
# Görev verilerini saklayacak dosya
|
| 27 |
+
TASKS_FILE = "tasks.json"
|
| 28 |
+
|
| 29 |
+
# Eğer dosya yoksa boş bir görev listesi oluştur
|
| 30 |
+
if not os.path.exists(TASKS_FILE):
|
| 31 |
+
with open(TASKS_FILE, "w") as f:
|
| 32 |
+
json.dump({"tasks": []}, f)
|
| 33 |
+
|
| 34 |
+
# Görevleri yükle
|
| 35 |
+
with open(TASKS_FILE, "r") as f:
|
| 36 |
+
data = json.load(f)
|
| 37 |
+
|
| 38 |
+
# Görev listesi
|
| 39 |
+
tasks = data["tasks"]
|
| 40 |
+
|
| 41 |
+
# Yardım mesajı
|
| 42 |
+
if action == "help" or action == "":
|
| 43 |
+
return """Görev Yönetici Aracı Kullanımı:
|
| 44 |
+
- add: Yeni görev ekle (örn: task_manager("add", "Marketten süt al"))
|
| 45 |
+
- list: Tüm görevleri listele (örn: task_manager("list"))
|
| 46 |
+
- complete: Görevi tamamlandı olarak işaretle (örn: task_manager("complete", "1"))
|
| 47 |
+
- delete: Görevi sil (örn: task_manager("delete", "1"))
|
| 48 |
+
- help: Bu yardım mesajını göster"""
|
| 49 |
+
|
| 50 |
+
# Yeni görev ekle
|
| 51 |
+
elif action == "add":
|
| 52 |
+
if not task_data:
|
| 53 |
+
return "Hata: Görev açıklaması boş olamaz!"
|
| 54 |
+
|
| 55 |
+
# Yeni görev oluştur
|
| 56 |
+
new_task = {
|
| 57 |
+
"id": len(tasks) + 1,
|
| 58 |
+
"description": task_data,
|
| 59 |
+
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 60 |
+
"completed": False
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
# Görevi listeye ekle
|
| 64 |
+
tasks.append(new_task)
|
| 65 |
+
|
| 66 |
+
# Değişiklikleri kaydet
|
| 67 |
+
with open(TASKS_FILE, "w") as f:
|
| 68 |
+
json.dump(data, f, indent=2)
|
| 69 |
+
|
| 70 |
+
return f"Görev eklendi: {task_data} (ID: {new_task['id']})"
|
| 71 |
+
|
| 72 |
+
# Görevleri listele
|
| 73 |
+
elif action == "list":
|
| 74 |
+
if not tasks:
|
| 75 |
+
return "Görev listeniz boş."
|
| 76 |
+
|
| 77 |
+
result = "GÖREVLERİNİZ:\n"
|
| 78 |
+
result += "-" * 50 + "\n"
|
| 79 |
+
|
| 80 |
+
for task in tasks:
|
| 81 |
+
status = "✓" if task["completed"] else "□"
|
| 82 |
+
result += f"{task['id']}. [{status}] {task['description']} ({task['created_at']})\n"
|
| 83 |
+
|
| 84 |
+
return result
|
| 85 |
+
|
| 86 |
+
# Görevi tamamlandı olarak işaretle
|
| 87 |
+
elif action == "complete":
|
| 88 |
+
try:
|
| 89 |
+
task_id = int(task_data)
|
| 90 |
+
|
| 91 |
+
# ID'ye göre görevi bul
|
| 92 |
+
for task in tasks:
|
| 93 |
+
if task["id"] == task_id:
|
| 94 |
+
task["completed"] = True
|
| 95 |
+
|
| 96 |
+
# Değişiklikleri kaydet
|
| 97 |
+
with open(TASKS_FILE, "w") as f:
|
| 98 |
+
json.dump(data, f, indent=2)
|
| 99 |
+
|
| 100 |
+
return f"Görev tamamlandı: {task['description']}"
|
| 101 |
+
|
| 102 |
+
return f"Hata: ID {task_id} olan görev bulunamadı."
|
| 103 |
+
|
| 104 |
+
except ValueError:
|
| 105 |
+
return "Hata: Geçerli bir görev ID'si belirtmelisiniz."
|
| 106 |
+
|
| 107 |
+
# Görevi sil
|
| 108 |
+
elif action == "delete":
|
| 109 |
+
try:
|
| 110 |
+
task_id = int(task_data)
|
| 111 |
+
|
| 112 |
+
# ID'ye göre görevi bul
|
| 113 |
+
for i, task in enumerate(tasks):
|
| 114 |
+
if task["id"] == task_id:
|
| 115 |
+
# Görevi listeden kaldır
|
| 116 |
+
deleted_task = tasks.pop(i)
|
| 117 |
+
|
| 118 |
+
# Değişiklikleri kaydet
|
| 119 |
+
with open(TASKS_FILE, "w") as f:
|
| 120 |
+
json.dump(data, f, indent=2)
|
| 121 |
+
|
| 122 |
+
return f"Görev silindi: {deleted_task['description']}"
|
| 123 |
+
|
| 124 |
+
return f"Hata: ID {task_id} olan görev bulunamadı."
|
| 125 |
+
|
| 126 |
+
except ValueError:
|
| 127 |
+
return "Hata: Geçerli bir görev ID'si belirtmelisiniz."
|
| 128 |
+
|
| 129 |
+
else:
|
| 130 |
+
return f"Hata: Bilinmeyen eylem '{action}'. Geçerli eylemler: add, list, complete, delete, help"
|
| 131 |
|
| 132 |
@tool
|
| 133 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 146 |
|
|
|
|
| 147 |
final_answer = FinalAnswerTool()
|
| 148 |
|
| 149 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 150 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
| 151 |
model = HfApiModel(
|
| 152 |
+
max_tokens=2096,
|
| 153 |
+
temperature=0.5,
|
| 154 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
| 155 |
+
custom_role_conversions=None,
|
| 156 |
)
|
| 157 |
|
|
|
|
| 158 |
# Import tool from Hub
|
| 159 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 160 |
|
|
|
|
| 163 |
|
| 164 |
agent = CodeAgent(
|
| 165 |
model=model,
|
| 166 |
+
tools=[final_answer, task_manager, get_current_time_in_timezone, image_generation_tool], # Eklediğimiz task_manager aracını dahil ettik
|
| 167 |
max_steps=6,
|
| 168 |
verbosity_level=1,
|
| 169 |
grammar=None,
|
|
|
|
| 173 |
prompt_templates=prompt_templates
|
| 174 |
)
|
| 175 |
|
|
|
|
| 176 |
GradioUI(agent).launch()
|