Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,48 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
-
from
|
| 7 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, WikipediaSearchTool
|
| 8 |
-
|
| 9 |
# (Keep Constants as is)
|
| 10 |
# --- Constants ---
|
| 11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 12 |
|
|
|
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
-
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
-
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
search_tool = DuckDuckGoSearchTool()
|
| 20 |
-
|
| 21 |
self.agent = CodeAgent(
|
| 22 |
-
model = model,
|
| 23 |
-
tools=[search_tool
|
| 24 |
-
|
| 25 |
)
|
| 26 |
-
|
| 27 |
def __call__(self, question: str) -> str:
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 31 |
"""
|
|
|
|
| 1 |
import os
|
| 2 |
+
import openai
|
| 3 |
import gradio as gr
|
| 4 |
import requests
|
| 5 |
import inspect
|
| 6 |
import pandas as pd
|
| 7 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel
|
|
|
|
|
|
|
| 8 |
# (Keep Constants as is)
|
| 9 |
# --- Constants ---
|
| 10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
| 11 |
|
| 12 |
+
|
| 13 |
# --- Basic Agent Definition ---
|
| 14 |
+
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
+
print("BasicAgent initialized.")
|
| 18 |
|
| 19 |
+
# 初始化 OpenAIServerModel 实例,用于与 DeepSeek 的语言模型交互
|
| 20 |
+
model = OpenAIServerModel(
|
| 21 |
+
model_id="deepseek-chat", # 使用 DeepSeek 的聊天模型
|
| 22 |
+
api_key="sk-04da63f756c9468182d278f52e33ad15",
|
| 23 |
+
api_base="https://api.deepseek.com", # 传入 API 密钥进行认证
|
| 24 |
+
)
|
| 25 |
+
# 初始化搜索工具,允许 Agent 执行网络搜索来查找信息
|
| 26 |
search_tool = DuckDuckGoSearchTool()
|
| 27 |
+
# 初始化 CodeAgent 实例,这是执行代码理解和生成任务的主要代理
|
| 28 |
self.agent = CodeAgent(
|
| 29 |
+
model = model, # 指定使用的语言模型
|
| 30 |
+
tools=[search_tool] # 为代理提供搜索工具,增强其获取信息的能力
|
|
|
|
| 31 |
)
|
|
|
|
| 32 |
def __call__(self, question: str) -> str:
|
| 33 |
+
"""
|
| 34 |
+
让 BasicAgent 实例可以像函数一样被调用,处理用户的问题
|
| 35 |
+
|
| 36 |
+
Args:
|
| 37 |
+
question: 用户提出的问题或请求字符串
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
str: Agent 生成的回答
|
| 41 |
+
"""
|
| 42 |
+
print(f"Agent received question (first 50 chars): {question[:50]}...") # 打印接收到的问题(前50个字符)用于调试
|
| 43 |
+
fixed_answer = self.agent.run(question) # 调用 CodeAgent 的 run 方法处理问题并生成回答
|
| 44 |
+
print(f"Agent returning fixed answer: {fixed_answer}") # 打印生成的回答用于调试
|
| 45 |
+
return fixed_answer # 返回 CodeAgent 生成的回答
|
| 46 |
|
| 47 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 48 |
"""
|