Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,93 +75,104 @@ import datetime
|
|
| 75 |
import requests
|
| 76 |
import pytz
|
| 77 |
import yaml
|
| 78 |
-
import json
|
| 79 |
from tools.final_answer import FinalAnswerTool
|
| 80 |
from Gradio_UI import GradioUI
|
| 81 |
|
| 82 |
-
# 날씨 정보 조회 도구
|
| 83 |
@tool
|
| 84 |
def get_weather_info(city: str) -> str:
|
| 85 |
"""특정 도시의 현재 날씨 정보를 가져오는 도구입니다.
|
| 86 |
Args:
|
| 87 |
city: 도시명 (예: 'Seoul', 'Busan')
|
| 88 |
"""
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return f"날씨 정보 조회 중 오류 발생: {str(e)}"
|
| 113 |
|
| 114 |
# 옷차림 추천 도구
|
| 115 |
-
@tool
|
| 116 |
def recommend_outfit(temperature: int, weather_condition: str) -> str:
|
| 117 |
"""기온과 날씨 상태를 바탕으로 옷차림을 추천하는 도구입니다.
|
| 118 |
Args:
|
| 119 |
temperature: 기온 (섭씨)
|
| 120 |
weather_condition: 날씨 상태 (예: '맑음', '비', '흐림')
|
| 121 |
"""
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
result += f" • {item}\n"
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
|
| 164 |
-
# 시간대별 현재 시간 조회 (기존 도구)
|
| 165 |
@tool
|
| 166 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 167 |
"""특정 시간대의 현재 시간을 알려주는 도구입니다.
|
|
@@ -171,62 +182,43 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 171 |
try:
|
| 172 |
tz = pytz.timezone(timezone)
|
| 173 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 174 |
-
return f"{timezone}의 현재 시간은 {local_time}입니다"
|
| 175 |
except Exception as e:
|
| 176 |
-
return f"'{timezone}' 시간대 정보를 가져오는 중 오류 발생: {str(e)}"
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
@tool
|
| 180 |
-
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 181 |
-
"""아직 아무 기능이 없는 도구입니다
|
| 182 |
-
Args:
|
| 183 |
-
arg1: 첫 번째 인수
|
| 184 |
-
arg2: 두 번째 인수
|
| 185 |
-
"""
|
| 186 |
-
return "어떤 마법을 만들어 보실 건가요?"
|
| 187 |
-
|
| 188 |
-
# Agent 설정
|
| 189 |
final_answer = FinalAnswerTool()
|
| 190 |
|
|
|
|
| 191 |
model = HfApiModel(
|
| 192 |
max_tokens=2096,
|
| 193 |
temperature=0.5,
|
| 194 |
-
model_id='
|
| 195 |
custom_role_conversions=None,
|
| 196 |
)
|
| 197 |
|
| 198 |
-
# Hub에서 도구 불러오기 (
|
| 199 |
try:
|
| 200 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 201 |
-
|
| 202 |
-
except Exception as e:
|
| 203 |
image_generation_tool = None
|
| 204 |
-
print(f"이미지 생성 도구 로드 실패: {e}")
|
| 205 |
|
| 206 |
with open("prompts.yaml", 'r') as stream:
|
| 207 |
prompt_templates = yaml.safe_load(stream)
|
| 208 |
|
| 209 |
-
# 도구 목록
|
| 210 |
tools_list = [
|
| 211 |
final_answer,
|
| 212 |
get_weather_info,
|
| 213 |
-
recommend_outfit,
|
| 214 |
get_current_time_in_timezone,
|
| 215 |
my_custom_tool
|
| 216 |
]
|
| 217 |
|
| 218 |
-
# 이미지 생성 도구가
|
| 219 |
if image_generation_tool:
|
| 220 |
tools_list.append(image_generation_tool)
|
| 221 |
|
| 222 |
-
# DuckDuckGo 검색 도구 추가 (선택사항)
|
| 223 |
-
try:
|
| 224 |
-
search_tool = DuckDuckGoSearchTool()
|
| 225 |
-
tools_list.append(search_tool)
|
| 226 |
-
print("검색 도구 추가 성공!")
|
| 227 |
-
except Exception as e:
|
| 228 |
-
print(f"검색 도구 추가 실패: {e}")
|
| 229 |
-
|
| 230 |
agent = CodeAgent(
|
| 231 |
model=model,
|
| 232 |
tools=tools_list,
|
|
@@ -234,7 +226,7 @@ agent = CodeAgent(
|
|
| 234 |
verbosity_level=1,
|
| 235 |
grammar=None,
|
| 236 |
planning_interval=None,
|
| 237 |
-
name=None,
|
| 238 |
description=None,
|
| 239 |
prompt_templates=prompt_templates
|
| 240 |
)
|
|
|
|
| 75 |
import requests
|
| 76 |
import pytz
|
| 77 |
import yaml
|
|
|
|
| 78 |
from tools.final_answer import FinalAnswerTool
|
| 79 |
from Gradio_UI import GradioUI
|
| 80 |
|
| 81 |
+
# 날씨 정보 조회 도구
|
| 82 |
@tool
|
| 83 |
def get_weather_info(city: str) -> str:
|
| 84 |
"""특정 도시의 현재 날씨 정보를 가져오는 도구입니다.
|
| 85 |
Args:
|
| 86 |
city: 도시명 (예: 'Seoul', 'Busan')
|
| 87 |
"""
|
| 88 |
+
# 더미 데이터 사용 (실제 API 대신)
|
| 89 |
+
weather_data = {
|
| 90 |
+
"seoul": {"temp": 15, "condition": "흐림", "humidity": 65, "wind": 3.2},
|
| 91 |
+
"busan": {"temp": 18, "condition": "맑음", "humidity": 70, "wind": 2.8},
|
| 92 |
+
"incheon": {"temp": 13, "condition": "비", "humidity": 80, "wind": 4.1},
|
| 93 |
+
"daegu": {"temp": 20, "condition": "맑음", "humidity": 45, "wind": 1.5},
|
| 94 |
+
"gwangju": {"temp": 17, "condition": "구름많음", "humidity": 55, "wind": 2.0},
|
| 95 |
+
"제주": {"temp": 19, "condition": "맑음", "humidity": 60, "wind": 2.5},
|
| 96 |
+
"대전": {"temp": 16, "condition": "흐림", "humidity": 58, "wind": 2.1}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
city_lower = city.lower()
|
| 100 |
+
if city_lower in weather_data:
|
| 101 |
+
data = weather_data[city_lower]
|
| 102 |
+
result = f"🌤️ {city} 날씨 정보:\n"
|
| 103 |
+
result += f"🌡️ 기온: {data['temp']}°C\n"
|
| 104 |
+
result += f"☁️ 날씨: {data['condition']}\n"
|
| 105 |
+
result += f"💧 습도: {data['humidity']}%\n"
|
| 106 |
+
result += f"💨 바람: {data['wind']}m/s"
|
| 107 |
+
return result
|
| 108 |
+
else:
|
| 109 |
+
available_cities = ", ".join(weather_data.keys())
|
| 110 |
+
return f"❌ {city}의 날씨 정보를 찾을 수 없습니다.\n📍 사용 가능한 도시: {available_cities}"
|
|
|
|
| 111 |
|
| 112 |
# 옷차림 추천 도구
|
| 113 |
+
@tool
|
| 114 |
def recommend_outfit(temperature: int, weather_condition: str) -> str:
|
| 115 |
"""기온과 날씨 상태를 바탕으로 옷차림을 추천하는 도구입니다.
|
| 116 |
Args:
|
| 117 |
temperature: 기온 (섭씨)
|
| 118 |
weather_condition: 날씨 상태 (예: '맑음', '비', '흐림')
|
| 119 |
"""
|
| 120 |
+
recommendations = []
|
| 121 |
+
accessories = []
|
| 122 |
+
|
| 123 |
+
# 기온별 기본 추천
|
| 124 |
+
if temperature <= 5:
|
| 125 |
+
recommendations = ["🧥 두꺼운 코트나 패딩", "👕 긴팔 내의", "👖 긴바지", "👢 부츠"]
|
| 126 |
+
accessories = ["🧣 목도리", "🧤 장갑", "🧢 모자"]
|
| 127 |
+
temp_comment = "❄️ 매우 추우니 보온에 신경 쓰세요!"
|
| 128 |
+
elif temperature <= 15:
|
| 129 |
+
recommendations = ["🧥 자켓이나 코트", "👕 긴팔 셔츠", "👖 긴바지", "👟 운동화"]
|
| 130 |
+
accessories = ["🧣 가벼운 목도리"]
|
| 131 |
+
temp_comment = "🍂 선선한 ���씨입니다. 겉옷을 준비하세요!"
|
| 132 |
+
elif temperature <= 25:
|
| 133 |
+
recommendations = ["🧥 가디건이나 얇은 자켓", "👕 긴팔 또는 반팔", "👖 긴바지", "👟 운동화"]
|
| 134 |
+
temp_comment = "🌤️ 적당한 날씨입니다!"
|
| 135 |
+
else:
|
| 136 |
+
recommendations = ["👕 반팔 티셔츠", "🩳 반바지나 얇은 바지", "👡 샌들이나 운동화"]
|
| 137 |
+
temp_comment = "☀️ 더우니 통풍이 잘 되는 옷을 선택하세요!"
|
| 138 |
+
|
| 139 |
+
# 날씨별 추가 아이템
|
| 140 |
+
if "비" in weather_condition:
|
| 141 |
+
accessories.extend(["☂️ 우산", "🧥 방수 재킷"])
|
| 142 |
+
weather_comment = "🌧️ 비가 오니 우산을 꼭 챙기세요!"
|
| 143 |
+
elif "눈" in weather_condition:
|
| 144 |
+
accessories.extend(["👢 방수 신발", "🧤 장갑"])
|
| 145 |
+
weather_comment = "❄️ 눈이 오니 미끄럼 방지에 주의하세요!"
|
| 146 |
+
else:
|
| 147 |
+
weather_comment = ""
|
| 148 |
+
|
| 149 |
+
# 결과 정리
|
| 150 |
+
result = f"👔 {temperature}°C, {weather_condition} 날씨 옷차림 추천:\n\n"
|
| 151 |
+
result += "📦 기본 옷차림:\n"
|
| 152 |
+
for item in recommendations:
|
| 153 |
+
result += f" • {item}\n"
|
| 154 |
+
|
| 155 |
+
if accessories:
|
| 156 |
+
result += "\n🎒 추가 아이템:\n"
|
| 157 |
+
for item in accessories:
|
| 158 |
result += f" • {item}\n"
|
| 159 |
+
|
| 160 |
+
result += f"\n💡 {temp_comment}"
|
| 161 |
+
if weather_comment:
|
| 162 |
+
result += f"\n💡 {weather_comment}"
|
| 163 |
+
|
| 164 |
+
return result
|
| 165 |
+
|
| 166 |
+
# 기존 템플릿의 도구들 유지
|
| 167 |
+
@tool
|
| 168 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 169 |
+
"""아직 아무 기능이 없는 도구입니다
|
| 170 |
+
Args:
|
| 171 |
+
arg1: 첫 번째 인수
|
| 172 |
+
arg2: 두 번째 인수
|
| 173 |
+
"""
|
| 174 |
+
return "어떤 마법을 만들어 보실 건가요?"
|
| 175 |
|
|
|
|
| 176 |
@tool
|
| 177 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 178 |
"""특정 시간대의 현재 시간을 알려주는 도구입니다.
|
|
|
|
| 182 |
try:
|
| 183 |
tz = pytz.timezone(timezone)
|
| 184 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 185 |
+
return f"🕒 {timezone}의 현재 시간은 {local_time}입니다"
|
| 186 |
except Exception as e:
|
| 187 |
+
return f"❌ '{timezone}' 시간대 정보를 가져오는 중 오류 발생: {str(e)}"
|
| 188 |
|
| 189 |
+
# Agent 설정 (원본 템플릿 기반)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
final_answer = FinalAnswerTool()
|
| 191 |
|
| 192 |
+
# 모델 설정 - 문서에서 제안한 대체 엔드포인트 사용
|
| 193 |
model = HfApiModel(
|
| 194 |
max_tokens=2096,
|
| 195 |
temperature=0.5,
|
| 196 |
+
model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', # 대체 엔드포인트
|
| 197 |
custom_role_conversions=None,
|
| 198 |
)
|
| 199 |
|
| 200 |
+
# Hub에서 도구 불러오기 (원본 템플릿 유지)
|
| 201 |
try:
|
| 202 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 203 |
+
except:
|
|
|
|
| 204 |
image_generation_tool = None
|
|
|
|
| 205 |
|
| 206 |
with open("prompts.yaml", 'r') as stream:
|
| 207 |
prompt_templates = yaml.safe_load(stream)
|
| 208 |
|
| 209 |
+
# 도구 목록 - 새로운 날씨 관련 도구들 추가
|
| 210 |
tools_list = [
|
| 211 |
final_answer,
|
| 212 |
get_weather_info,
|
| 213 |
+
recommend_outfit,
|
| 214 |
get_current_time_in_timezone,
|
| 215 |
my_custom_tool
|
| 216 |
]
|
| 217 |
|
| 218 |
+
# 이미지 생성 도구가 있다면 추가
|
| 219 |
if image_generation_tool:
|
| 220 |
tools_list.append(image_generation_tool)
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
agent = CodeAgent(
|
| 223 |
model=model,
|
| 224 |
tools=tools_list,
|
|
|
|
| 226 |
verbosity_level=1,
|
| 227 |
grammar=None,
|
| 228 |
planning_interval=None,
|
| 229 |
+
name=None,
|
| 230 |
description=None,
|
| 231 |
prompt_templates=prompt_templates
|
| 232 |
)
|