9jini commited on
Commit
5c5f62c
·
verified ·
1 Parent(s): ab9a4f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -12
app.py CHANGED
@@ -1,33 +1,48 @@
1
  import os
 
2
  import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
-
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
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
15
  class BasicAgent:
16
  def __init__(self):
17
- model = OpenAIServerModel(model_id="deepseek-chat",api_key="sk-04da63f756c9468182d278f52e33ad15", api_base="https://api.deepseek.com")
18
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  search_tool = DuckDuckGoSearchTool()
20
- wiki_search = WikipediaSearchTool()
21
-
22
  self.agent = CodeAgent(
23
  model = model,
24
- tools=[
25
- search_tool,
26
- ]
27
  )
28
-
29
  def __call__(self, question: str) -> str:
30
- return self.agent.run(question) # type: ignore
 
 
 
31
 
32
  def run_and_submit_all( profile: gr.OAuthProfile | None):
33
  """
 
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
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
15
+ DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
16
+ DEEPSEEK_API_BASE = os.getenv("DEEPSEEK_API_BASE", "https://api.deepseek.com")
17
  class BasicAgent:
18
  def __init__(self):
19
+ print("BasicAgent initialized.")
20
+ # Initialize the model
21
+ #model = HfApiModel()
22
+ if not DEEPSEEK_API_KEY: # 添加一个检查,确保密钥已设置
23
+ raise ValueError("DEEPSEEK_API_KEY environment variable not set. Please set it before running.")
24
+
25
+ openai.api_key = DEEPSEEK_API_KEY # <--- 步骤 2: 全局设置 API 密钥
26
+
27
+ # 为 openai 库设置基础 URL
28
+ openai.api_base = DEEPSEEK_API_BASE
29
+
30
+ model = OpenAIServerModel(
31
+ model_id="deepseek-chat",
32
+ api_key=DEEPSEEK_API_KEY
33
+ )
34
+ # Initialize the search tool
35
  search_tool = DuckDuckGoSearchTool()
36
+ # Initialize Agent
 
37
  self.agent = CodeAgent(
38
  model = model,
39
+ tools=[search_tool]
 
 
40
  )
 
41
  def __call__(self, question: str) -> str:
42
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
43
+ fixed_answer =self.agent.run(question)
44
+ print(f"Agent returning fixed answer: {fixed_answer}")
45
+ return fixed_answer
46
 
47
  def run_and_submit_all( profile: gr.OAuthProfile | None):
48
  """