Iamvincent commited on
Commit
99fb7ee
·
verified ·
1 Parent(s): 4cd35a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -13,23 +13,36 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  def __init__(self):
16
- print("BasicAgent initialized.")
17
-
18
- hf_token = os.getenv("HF_TOKEN")
19
- if not hf_token:
20
- raise ValueError("HF_TOKEN environment variable is not set.")
 
 
 
 
 
21
 
22
- self.agent = CodeAgent(
23
- tools=[DuckDuckGoSearchTool()],
24
- model=HfApiModel(token=hf_token, model="microsoft/phi-2")
 
 
25
  )
26
 
27
  def __call__(self, question: str) -> str:
28
- print(f"Agent received question (first 50 chars): {question[:50]}...")
29
- fixed_answer = self.agent.run("You are a helpful assistant answering short factual questions. Answer the question: " + question)
30
- print(fixed_answer)
31
- print(f"Agent returning fixed answer: {fixed_answer}")
32
- return fixed_answer
 
 
 
 
 
 
33
 
34
  def run_and_submit_all( profile: gr.OAuthProfile | None):
35
  """
 
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
14
  class BasicAgent:
15
  def __init__(self):
16
+ token = os.getenv("HF_TOKEN")
17
+ if not token:
18
+ raise ValueError("HF_TOKEN secret missing in Space settings")
19
+
20
+ # load your **ONLY** model
21
+ self.llm = HfApiModel(
22
+ token=token,
23
+ model="microsoft/phi-2", # <— change this line for any other model
24
+ stop_sequences=["ANSWER:"], # stop as soon as it produces an answer
25
+ )
26
 
27
+ # single system prompt once
28
+ self.system = (
29
+ "You are a helpful assistant. "
30
+ "Answer each question in ONE line that starts with “ANSWER: ” "
31
+ "and nothing else."
32
  )
33
 
34
  def __call__(self, question: str) -> str:
35
+ messages = [
36
+ {"role": "system", "content": self.system},
37
+ {"role": "user", "content": question},
38
+ ]
39
+ reply = self.llm(messages) # one HTTP request
40
+ text = reply["content"].strip()
41
+ # ensure we only return the line after “ANSWER:”
42
+ if "ANSWER" in text:
43
+ text = text.split("ANSWER", 1)[-1].lstrip(": ").splitlines()[0]
44
+ text = f"ANSWER: {text}"
45
+ return text
46
 
47
  def run_and_submit_all( profile: gr.OAuthProfile | None):
48
  """