Spaces:
Running
Running
Robin Chiu
commited on
Commit
·
e225706
1
Parent(s):
9f63939
Add examples
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ def parse_news_item(html: str) -> dict:
|
|
| 31 |
|
| 32 |
|
| 33 |
# %%
|
| 34 |
-
def search_news(keyword, page=1):
|
| 35 |
"""
|
| 36 |
Fetch news articles related to a keyword from udn.com.
|
| 37 |
|
|
@@ -40,7 +40,7 @@ def search_news(keyword, page=1):
|
|
| 40 |
page: The page number to fetch (default is 1).
|
| 41 |
|
| 42 |
Returns:
|
| 43 |
-
A list of dictionaries containing news article data.
|
| 44 |
"""
|
| 45 |
url = f"https://money.udn.com/search/result/1001/{keyword}/{page}"
|
| 46 |
response = requests.get(url)
|
|
@@ -66,7 +66,7 @@ def search_news(keyword, page=1):
|
|
| 66 |
|
| 67 |
# %%
|
| 68 |
# write a function to get the url and parse the content
|
| 69 |
-
def get_content(url):
|
| 70 |
"""
|
| 71 |
Fetch and parse the content of a given URL.
|
| 72 |
|
|
@@ -117,13 +117,13 @@ def newsAgent(task: str) -> str:
|
|
| 117 |
"""
|
| 118 |
result = ""
|
| 119 |
with ToolCollection.from_mcp(server_parameters, trust_remote_code=True) as mcp_tools:
|
| 120 |
-
agent = CodeAgent(tools=[*mcp_tools.tools], model=model)
|
| 121 |
for event in agent.run(task, stream=True, max_steps=5):
|
| 122 |
if isinstance(event, ActionStep):
|
| 123 |
-
result
|
| 124 |
# yield result
|
| 125 |
if isinstance(event, FinalAnswerStep):
|
| 126 |
-
result
|
| 127 |
# yield result
|
| 128 |
return result
|
| 129 |
|
|
@@ -142,19 +142,52 @@ def main():
|
|
| 142 |
page = gr.Number(label="Page Number", value=1, step=1)
|
| 143 |
search_button = gr.Button("Search")
|
| 144 |
search_results = gr.DataFrame(label="Search Results", headers=["Link", "Time", "Headline", "Text"])
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
search_button.click(search_news, inputs=[keyword, page], outputs=search_results)
|
|
|
|
| 147 |
|
| 148 |
with gr.Tab("Get Content from URL"):
|
| 149 |
url_input = gr.Textbox(label="URL", placeholder="Enter URL to fetch content")
|
| 150 |
content_output = gr.JSON(label="Content Output")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
url_input.submit(get_content, inputs=url_input, outputs=content_output)
|
| 152 |
|
| 153 |
with gr.Tab("News Agent"):
|
| 154 |
agent_input = gr.Textbox(label="Task", placeholder="Enter the task")
|
| 155 |
-
run_button = gr.Button("Run")
|
| 156 |
-
result_output = gr.
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
demo.launch(mcp_server=True, server_name="0.0.0.0",allowed_paths=["/"], share=True)
|
| 160 |
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# %%
|
| 34 |
+
def search_news(keyword, page=1) -> list:
|
| 35 |
"""
|
| 36 |
Fetch news articles related to a keyword from udn.com.
|
| 37 |
|
|
|
|
| 40 |
page: The page number to fetch (default is 1).
|
| 41 |
|
| 42 |
Returns:
|
| 43 |
+
A list of dictionaries containing link, time, headline and text of news article data.
|
| 44 |
"""
|
| 45 |
url = f"https://money.udn.com/search/result/1001/{keyword}/{page}"
|
| 46 |
response = requests.get(url)
|
|
|
|
| 66 |
|
| 67 |
# %%
|
| 68 |
# write a function to get the url and parse the content
|
| 69 |
+
def get_content(url) -> dict:
|
| 70 |
"""
|
| 71 |
Fetch and parse the content of a given URL.
|
| 72 |
|
|
|
|
| 117 |
"""
|
| 118 |
result = ""
|
| 119 |
with ToolCollection.from_mcp(server_parameters, trust_remote_code=True) as mcp_tools:
|
| 120 |
+
agent = CodeAgent(tools=[*mcp_tools.tools[:2]], model=model)
|
| 121 |
for event in agent.run(task, stream=True, max_steps=5):
|
| 122 |
if isinstance(event, ActionStep):
|
| 123 |
+
result += f"\n## ======Step {event.step_number}======\n### Action\n```python\n{event.code_action}\n```\n### Observation\n{event.observations}"
|
| 124 |
# yield result
|
| 125 |
if isinstance(event, FinalAnswerStep):
|
| 126 |
+
result += f"\n## ======Final======\n{event.output}"
|
| 127 |
# yield result
|
| 128 |
return result
|
| 129 |
|
|
|
|
| 142 |
page = gr.Number(label="Page Number", value=1, step=1)
|
| 143 |
search_button = gr.Button("Search")
|
| 144 |
search_results = gr.DataFrame(label="Search Results", headers=["Link", "Time", "Headline", "Text"])
|
| 145 |
+
# Examples for Search News tab
|
| 146 |
+
gr.Examples(
|
| 147 |
+
examples=[
|
| 148 |
+
["AI", 1],
|
| 149 |
+
["華碩", 2]
|
| 150 |
+
],
|
| 151 |
+
inputs=[keyword, page],
|
| 152 |
+
outputs=search_results,
|
| 153 |
+
fn=search_news,
|
| 154 |
+
cache_examples=False
|
| 155 |
+
)
|
| 156 |
search_button.click(search_news, inputs=[keyword, page], outputs=search_results)
|
| 157 |
+
|
| 158 |
|
| 159 |
with gr.Tab("Get Content from URL"):
|
| 160 |
url_input = gr.Textbox(label="URL", placeholder="Enter URL to fetch content")
|
| 161 |
content_output = gr.JSON(label="Content Output")
|
| 162 |
+
# Examples for Get Content of News tab
|
| 163 |
+
gr.Examples(
|
| 164 |
+
examples=[
|
| 165 |
+
["https://money.udn.com/money/story/5722/8870335?from=edn_search_result"],
|
| 166 |
+
["https://money.udn.com/money/story/5612/8868152?from=edn_search_result"]
|
| 167 |
+
],
|
| 168 |
+
inputs=[url_input],
|
| 169 |
+
outputs=content_output,
|
| 170 |
+
fn=get_content,
|
| 171 |
+
cache_examples=False
|
| 172 |
+
)
|
| 173 |
url_input.submit(get_content, inputs=url_input, outputs=content_output)
|
| 174 |
|
| 175 |
with gr.Tab("News Agent"):
|
| 176 |
agent_input = gr.Textbox(label="Task", placeholder="Enter the task")
|
| 177 |
+
# run_button = gr.Button("Run")
|
| 178 |
+
result_output = gr.Markdown(label="Result")
|
| 179 |
+
# Examples for Get Content of News tab
|
| 180 |
+
gr.Examples(
|
| 181 |
+
examples=[
|
| 182 |
+
["華碩今日新聞"],
|
| 183 |
+
["華碩和Nvidia今日新聞"]
|
| 184 |
+
],
|
| 185 |
+
inputs=[agent_input],
|
| 186 |
+
outputs=result_output,
|
| 187 |
+
fn=newsAgent,
|
| 188 |
+
cache_examples=True
|
| 189 |
+
)
|
| 190 |
+
agent_input.submit(newsAgent, inputs=agent_input, outputs=result_output)
|
| 191 |
|
| 192 |
demo.launch(mcp_server=True, server_name="0.0.0.0",allowed_paths=["/"], share=True)
|
| 193 |
|