zineb-chgari commited on
Commit
6f8ffec
·
verified ·
1 Parent(s): 908bb13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -8,6 +8,7 @@ from smolagents import ToolCallingAgent
8
  from smolagents import DuckDuckGoSearchTool
9
  from smolagents import Tool
10
  import traceback
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
13
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
@@ -51,18 +52,32 @@ CalculatorTool=Tool(
51
  description="it calculates basic arithmetics",
52
  func=calculatorTool
53
  )
 
54
  class BasicAgent:
55
  def __init__(self):
56
  try:
57
- api_key=os.getenv("chatbotagenthf")
58
- model =InferenceClientModel(model_id="HuggingFaceH4/zephyr-7b-beta",token=api_key)
 
 
 
 
 
 
 
 
59
 
 
 
 
 
 
60
  self.agent = ToolCallingAgent(
61
  tools=[DuckDuckGoSearchTool()],
62
- model=model
63
  )
64
 
65
- print("✅ Agent initialized successfully.")
66
 
67
  except Exception as e:
68
  print(f"❌ Error initializing agent: {e}")
@@ -70,17 +85,30 @@ class BasicAgent:
70
  raise e
71
 
72
  def __call__(self, question: str) -> str:
 
73
  try:
74
  print(f"\n➡️ Agent received question: {question[:80]}")
75
- agent_answer = self.agent.run(question)
76
- print(f"✅ Agent returning answer: {agent_answer}")
77
- return agent_answer
 
 
 
 
 
 
 
 
 
 
 
78
  except Exception as e:
79
  print(f"❌ Agent encountered an error: {e}")
80
  traceback.print_exc()
81
  return f"Error generating answer: {e}"
82
 
83
 
 
84
  def run_and_submit_all( profile: gr.OAuthProfile | None):
85
  """
86
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
8
  from smolagents import DuckDuckGoSearchTool
9
  from smolagents import Tool
10
  import traceback
11
+ from openai import OpenAI
12
  # (Keep Constants as is)
13
  # --- Constants ---
14
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
 
52
  description="it calculates basic arithmetics",
53
  func=calculatorTool
54
  )
55
+
56
  class BasicAgent:
57
  def __init__(self):
58
  try:
59
+ # Load your HF token
60
+ hf_token = os.getenv("chatbotagenthf")
61
+ if not hf_token:
62
+ raise ValueError("HF token not found in environment variable 'chatbotagenthf'.")
63
+
64
+ # --- Connect to Hugging Face router ---
65
+ self.client = OpenAI(
66
+ base_url="https://router.huggingface.co/v1",
67
+ api_key=hf_token
68
+ )
69
 
70
+ # --- Choose any accessible instruct model ---
71
+ self.model_id = "moonshotai/Kimi-K2-Thinking:novita"
72
+ # (You can change to: mistralai/Mistral-7B-Instruct-v0.3, zephyr-7b-beta, etc.)
73
+
74
+ # --- Define your ToolCallingAgent as before ---
75
  self.agent = ToolCallingAgent(
76
  tools=[DuckDuckGoSearchTool()],
77
+ model=None # Model handled manually via OpenAI client
78
  )
79
 
80
+ print("✅ Agent initialized successfully using Hugging Face Router.")
81
 
82
  except Exception as e:
83
  print(f"❌ Error initializing agent: {e}")
 
85
  raise e
86
 
87
  def __call__(self, question: str) -> str:
88
+ """Run a chat completion on the HF model."""
89
  try:
90
  print(f"\n➡️ Agent received question: {question[:80]}")
91
+
92
+ # Send the prompt to the model
93
+ response = self.client.chat.completions.create(
94
+ model=self.model_id,
95
+ messages=[
96
+ {"role": "system", "content": "You are a helpful assistant."},
97
+ {"role": "user", "content": question}
98
+ ],
99
+ )
100
+
101
+ answer = response.choices[0].message["content"].strip()
102
+ print(f"✅ Agent returning answer: {answer}")
103
+ return answer
104
+
105
  except Exception as e:
106
  print(f"❌ Agent encountered an error: {e}")
107
  traceback.print_exc()
108
  return f"Error generating answer: {e}"
109
 
110
 
111
+
112
  def run_and_submit_all( profile: gr.OAuthProfile | None):
113
  """
114
  Fetches all questions, runs the BasicAgent on them, submits all answers,