alanwang166 commited on
Commit
142bac6
·
1 Parent(s): d690705

Switch model backend by environment, add smolagents dependency

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. app.py +17 -4
  3. requirements.txt +1 -0
  4. runner.py +40 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+ questions.json
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import os
 
2
  import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
  import spaces
 
7
 
 
8
  @spaces.GPU
9
  def _zerogpu_startup_check():
10
  return None
@@ -17,12 +20,22 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
  class BasicAgent:
19
  def __init__(self):
20
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
21
  def __call__(self, question: str) -> str:
22
  print(f"Agent received question (first 50 chars): {question[:50]}...")
23
- fixed_answer = "This is a default answer."
24
- print(f"Agent returning fixed answer: {fixed_answer}")
25
- return fixed_answer
26
 
27
  def run_and_submit_all( profile: gr.OAuthProfile | None):
28
  """
 
1
  import os
2
+ from pyexpat import model
3
  import gradio as gr
4
  import requests
5
  import inspect
6
  import pandas as pd
7
  import spaces
8
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, LiteLLMModel, InferenceClientModel
9
 
10
+ # for huggingface gpu run
11
  @spaces.GPU
12
  def _zerogpu_startup_check():
13
  return None
 
20
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
21
  class BasicAgent:
22
  def __init__(self):
23
+ if os.getenv("SPACE_ID"):
24
+ self.model = InferenceClientModel(
25
+ model_id="moonshotai/Kimi-K2.5",
26
+ token=os.getenv("HF_TOKEN"),
27
+ )
28
+ else:
29
+ print("BasicAgent initialized.")
30
+ self.model = LiteLLMModel(model_id="ollama_chat/qwen3:4b", api_base="http://127.0.0.1:11434", num_ctx=8192)
31
+
32
+ self.agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=self.model)
33
+
34
  def __call__(self, question: str) -> str:
35
  print(f"Agent received question (first 50 chars): {question[:50]}...")
36
+ answer = str(self.agent.run(question))
37
+ print(f"Agent returning fixed answer: {answer}")
38
+ return answer
39
 
40
  def run_and_submit_all( profile: gr.OAuthProfile | None):
41
  """
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  gradio
2
  requests
3
  spaces
 
 
1
  gradio
2
  requests
3
  spaces
4
+ smolagents==1.26.0
runner.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import requests
3
+ from pathlib import Path
4
+
5
+ # API = "https://agents-course-unit4-scoring.hf.space"
6
+
7
+ # questions = requests.get(f"{API}/questions").json()
8
+
9
+ # print(len(questions))
10
+ # print(questions[0])
11
+
12
+
13
+ API = "https://agents-course-unit4-scoring.hf.space"
14
+ CACHE = Path(__file__).parent / "questions.json"
15
+
16
+
17
+ def load_questions():
18
+ if CACHE.exists():
19
+ return json.loads(CACHE.read_text())
20
+
21
+ questions = requests.get(f"{API}/questions").json()
22
+ CACHE.write_text(json.dumps(questions, indent=2, ensure_ascii=False))
23
+ return questions
24
+
25
+
26
+ if __name__ == "__main__":
27
+ qs = load_questions()
28
+ print(f" {len(qs)} questions fetched from {API}/questions")
29
+ for q in qs:
30
+ mark = "📎" if q["file_name"] else " "
31
+ print(f'{mark} {q["task_id"][:8]} {q["question"][:70]}')
32
+
33
+ from app import BasicAgent
34
+
35
+ qs = load_questions()
36
+ q = qs[2]
37
+ print("Q:", q["question"])
38
+
39
+ agent = BasicAgent()
40
+ print("A:", agent(q["question"]))