Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,105 @@
|
|
1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from tools.final_answer import FinalAnswerTool
|
7 |
-
import traceback
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
@tool
|
12 |
-
def
|
13 |
-
"""
|
14 |
-
|
15 |
Args:
|
16 |
-
|
17 |
Returns:
|
18 |
-
|
19 |
-
example: "RELIANCE NS: 1237.22 Rupees."
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
latest_price = latest_data['Close'].values[0]
|
29 |
-
return (f"{stock_symbol}: {round(latest_price, 2)} Rupees.")
|
30 |
-
else:
|
31 |
-
return "INVALID TICKER/STOCK"
|
32 |
-
except Exception as e:
|
33 |
-
traceback.print_exc()
|
34 |
-
return "INVALID REQUEST"
|
35 |
|
36 |
@tool
|
37 |
-
def
|
38 |
-
"""
|
|
|
39 |
Args:
|
40 |
-
|
|
|
|
|
|
|
41 |
"""
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
# Get current time in that timezone
|
46 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
47 |
-
return f"The current local time in {timezone} is: {local_time}"
|
48 |
-
except Exception as e:
|
49 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
50 |
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
)
|
|
|
|
|
|
|
|
|
|
|
59 |
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
|
|
|
|
|
|
79 |
|
80 |
-
|
|
|
|
1 |
+
from smolagents import CodeAgent, ToolCallingAgent, DuckDuckGoSearchTool, LiteLLMModel, tool
|
2 |
+
from typing import Optional
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import gradio as gr
|
|
|
|
|
6 |
|
7 |
+
# .env 파일에서 환경 변수를 로드
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
# Gemini AI 모델 초기화
|
11 |
+
model = LiteLLMModel(
|
12 |
+
model_id="gemini/gemini-2.0-flash-exp",
|
13 |
+
api_key=os.getenv("GOOGLE_API_KEY")
|
14 |
+
)
|
15 |
|
16 |
@tool
|
17 |
+
def search_news(topic: str) -> str:
|
18 |
+
"""
|
19 |
+
주어진 주제에 대한 최신 뉴스를 검색하는 도구입니다.
|
20 |
Args:
|
21 |
+
topic: 검색할 뉴스 주제
|
22 |
Returns:
|
23 |
+
검색된 뉴스 정보
|
|
|
24 |
"""
|
25 |
+
search_agent = CodeAgent(
|
26 |
+
tools=[DuckDuckGoSearchTool()],
|
27 |
+
model=model,
|
28 |
+
additional_authorized_imports=["requests", "bs4"]
|
29 |
+
)
|
30 |
+
search_query = f"최신 뉴스 {topic}"
|
31 |
+
return search_agent.run(f"Search for: {search_query}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
@tool
|
34 |
+
def write_article(topic: str, search_results: str) -> str:
|
35 |
+
"""
|
36 |
+
검색 결과를 바탕으로 새로운 기사를 작성하는 도구입니다.
|
37 |
Args:
|
38 |
+
topic: 기사 주제
|
39 |
+
search_results: 검색된 뉴스 정보
|
40 |
+
Returns:
|
41 |
+
작성된 기사
|
42 |
"""
|
43 |
+
NEWS_WRITER_PROMPT = """
|
44 |
+
당신은 전문 기자입니다. 주어진 주제에 대해 검색된 정보를 바탕으로 새로운 기사를 한국어로 작성해야 합니다.
|
45 |
+
다음 형식으로 기사를 작성해주세요:
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
1. 제목 (흥미롭고 눈에 띄는 제목)
|
48 |
+
2. 요약 (핵심 내용을 2-3문장으로 요약)
|
49 |
+
3. 본문 (상세한 내용을 단락으로 구분하여 작성)
|
50 |
+
4. 결론 (기사의 의의나 향후 전망)
|
51 |
|
52 |
+
기사는 객관적이고 사실에 기반하여 작성해야 하며, 검색된 정보를 재구성하여 새로운 시각으로 작성해주세요.
|
53 |
+
"""
|
54 |
+
|
55 |
+
writer_agent = ToolCallingAgent(
|
56 |
+
model=model,
|
57 |
+
system_prompt=NEWS_WRITER_PROMPT
|
58 |
+
)
|
59 |
+
|
60 |
+
prompt = f"""
|
61 |
+
다음 주제와 검색 결과를 바탕으로 새로운 기사를 한국어로 작성해주세요:
|
62 |
+
|
63 |
+
주제: {topic}
|
64 |
|
65 |
+
검색 결과:
|
66 |
+
{search_results}
|
67 |
|
68 |
+
위 정보를 바탕으로 새롭고 통찰력 있는 기사를 한국어로 작성해주세요.
|
69 |
+
"""
|
70 |
+
return writer_agent.run(prompt)
|
71 |
|
72 |
+
class NewsWriterGradioUI:
|
73 |
+
def __init__(self):
|
74 |
+
self.agent = CodeAgent(
|
75 |
+
tools=[search_news, write_article],
|
76 |
+
model=model
|
77 |
+
)
|
78 |
|
79 |
+
def generate_article(self, topic: str) -> str:
|
80 |
+
try:
|
81 |
+
prompt = f"""
|
82 |
+
1. '{topic}' 주제에 대해 search_news 도구를 사용하여 최신 뉴스를 검색하세요.
|
83 |
+
2. 검색된 결과를 바탕으로 write_article 도구를 사용하여 새로운 기사를 작성하세요.
|
84 |
+
"""
|
85 |
+
result = self.agent.run(prompt)
|
86 |
+
return result
|
87 |
+
except Exception as e:
|
88 |
+
return f"에러가 발생했습니다: {str(e)}"
|
89 |
+
|
90 |
+
def launch(self):
|
91 |
+
interface = gr.Interface(
|
92 |
+
fn=self.generate_article,
|
93 |
+
inputs=gr.Textbox(label="뉴스 주제를 입력하세요"),
|
94 |
+
outputs=gr.Textbox(label="생성된 기사"),
|
95 |
+
title="AI 기자",
|
96 |
+
description="주제를 입력하면 관련 뉴스를 검색하고 새로운 기사를 작성합니다."
|
97 |
+
)
|
98 |
+
interface.launch()
|
99 |
|
100 |
+
def main():
|
101 |
+
ui = NewsWriterGradioUI()
|
102 |
+
ui.launch()
|
103 |
|
104 |
+
if __name__ == "__main__":
|
105 |
+
main()
|