keynes42 commited on
Commit
cb50047
·
verified ·
1 Parent(s): 3555eb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -46
app.py CHANGED
@@ -6,7 +6,9 @@ import subprocess
6
  import pandas as pd
7
  import torch, spaces
8
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
9
- from smolagents import CodeAgent, HfApiModel
 
 
10
  from huggingface_hub import InferenceClient, hf_hub_download
11
 
12
  subprocess.run(["playwright", "install"], check=True)
@@ -15,40 +17,28 @@ subprocess.run(["playwright", "install"], check=True)
15
  # --- Constants ---
16
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
17
 
18
- class LocalLLM:
19
- def __init__(self, pipe):
20
- self.pipe = pipe
21
 
22
- def generate(self, prompt, **kwargs):
23
- unsupported_keys = ["stop_sequences"] # Remove keys not accepted by HF pipelines
24
- cleaned_kwargs = {k: v for k, v in kwargs.items() if k not in unsupported_keys}
25
- # print(f"🧪 kwargs cleaned: {cleaned_kwargs.keys()}")
26
- try:
27
- outputs = self.pipe(prompt, **cleaned_kwargs)
28
- # print(f"🧪 Raw output from pipe: {outputs}")
29
- if isinstance(outputs, list) and isinstance(outputs[0], dict):
30
- out = outputs[0]["generated_text"]
31
- elif isinstance(outputs, list):
32
- out = outputs[0] # fallback if it's just a list of strings
33
- else:
34
- out = str(outputs)
35
- print("🧪 Final object to return:", type(out), out[:100])
36
- return {'role': 'assistant', 'content': [{'type':'text', 'text': out}]}
37
- except Exception as e:
38
- print(f"❌ Error in LocalLLM.generate(): {e}")
39
- raise
40
-
41
- @spaces.GPU
42
- def load_llm(hf_token):
43
- model_id = "meta-llama/Llama-3.1-8B-Instruct"
44
- tok = AutoTokenizer.from_pretrained(model_id, token=hf_token)
45
- mod = AutoModelForCausalLM.from_pretrained(
46
- model_id,
47
- torch_dtype=torch.float16,
48
- device_map="auto", # auto-distributes to GPU
49
- token=hf_token
50
- )
51
- return pipeline("text-generation", model=mod, tokenizer=tok, max_new_tokens=512)
52
 
53
  def check_token_access():
54
  token = os.environ.get("HF_TOKEN", "")
@@ -71,10 +61,10 @@ def check_token_access():
71
 
72
  # --- Basic Agent Definition ---
73
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
74
- class BasicAgent:
75
- def __init__(self):
76
  print("BasicAgent initialized.")
77
- print("ENV-HF_TOKEN-LEN", len(os.environ["HF_TOKEN"]), file=sys.stderr)
78
  check_token_access()
79
 
80
  # Local test
@@ -96,9 +86,22 @@ class BasicAgent:
96
  # See list of base tools in https://github.com/huggingface/smolagents/blob/main/src/smolagents/default_tools.py
97
 
98
  # Initialize the agent
99
- pipe = load_llm(hf_token = os.environ["HF_TOKEN"])
100
- self.model = LocalLLM(pipe)
101
- self.agent = CodeAgent(model=self.model, tools=[], add_base_tools=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
102
 
103
  # self.agent = CodeAgent(
104
  # model=model,
@@ -106,11 +109,34 @@ class BasicAgent:
106
  # add_base_tools=True
107
  # )
108
 
 
 
 
 
 
 
 
 
109
  def __call__(self, question: str) -> str:
110
  print(f"Agent received question (first 50 chars): {question[:50]}...")
111
- response = self.agent.run(question)
 
 
 
 
 
 
 
 
 
 
112
  print(f"Agent returning its generated answer: {response}")
113
- return response
 
 
 
 
 
114
 
115
  def run_and_submit_all( profile: gr.OAuthProfile | None):
116
  """
@@ -119,6 +145,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
119
  """
120
  # --- Determine HF Space Runtime URL and Repo URL ---
121
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
 
122
 
123
  if profile:
124
  username= f"{profile.username}"
@@ -133,7 +160,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
133
 
134
  # 1. Instantiate Agent ( modify this part to create your agent)
135
  try:
136
- agent = BasicAgent()
137
  except Exception as e:
138
  print(f"Error instantiating agent: {e}")
139
  return f"Error initializing agent: {e}", None
@@ -173,7 +200,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
173
  print(f"Skipping item with missing task_id or question: {item}")
174
  continue
175
  try:
176
- submitted_answer = agent(question_text)
 
177
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
178
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
179
  except Exception as e:
@@ -269,6 +297,7 @@ if __name__ == "__main__":
269
  # Check for SPACE_HOST and SPACE_ID at startup for information
270
  space_host_startup = os.getenv("SPACE_HOST")
271
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
 
272
 
273
  if space_host_startup:
274
  print(f"✅ SPACE_HOST found: {space_host_startup}")
@@ -286,8 +315,8 @@ if __name__ == "__main__":
286
  print("-"*(60 + len(" App Starting ")) + "\n")
287
 
288
  # Test the agent
289
- agent = BasicAgent()
290
- agent.agent.run("What is 2+2?")
291
 
292
  print("Launching Gradio Interface for Basic Agent Evaluation...")
293
  demo.launch(debug=True, share=False)
 
6
  import pandas as pd
7
  import torch, spaces
8
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
9
+ # from smolagents import CodeAgent, HfApiModel
10
+ from smolagents.agent import LocalLLM, CodeAgent
11
+ from smolagents.message import MessageRole
12
  from huggingface_hub import InferenceClient, hf_hub_download
13
 
14
  subprocess.run(["playwright", "install"], check=True)
 
17
  # --- Constants ---
18
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
19
 
20
+ # class LocalLLM:
21
+ # def __init__(self, pipe):
22
+ # self.pipe = pipe
23
 
24
+ # def generate(self, prompt, **kwargs):
25
+ # unsupported_keys = ["stop_sequences"] # Remove keys not accepted by HF pipelines
26
+ # cleaned_kwargs = {k: v for k, v in kwargs.items() if k not in unsupported_keys}
27
+ # # print(f"🧪 kwargs cleaned: {cleaned_kwargs.keys()}")
28
+ # try:
29
+ # outputs = self.pipe(prompt, **cleaned_kwargs)
30
+ # # print(f"🧪 Raw output from pipe: {outputs}")
31
+ # if isinstance(outputs, list) and isinstance(outputs[0], dict):
32
+ # out = outputs[0]["generated_text"]
33
+ # elif isinstance(outputs, list):
34
+ # out = outputs[0] # fallback if it's just a list of strings
35
+ # else:
36
+ # out = str(outputs)
37
+ # print("🧪 Final object to return:", type(out), out[:100])
38
+ # return {'role': 'assistant', 'content': [{'type':'text', 'text': out}]}
39
+ # except Exception as e:
40
+ # print(f"❌ Error in LocalLLM.generate(): {e}")
41
+ # raise
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  def check_token_access():
44
  token = os.environ.get("HF_TOKEN", "")
 
61
 
62
  # --- Basic Agent Definition ---
63
  # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
64
+ class BasicAgent(LocalLLM):
65
+ def __init__(self, model_id="meta-llama/Llama-3.1-8B-Instruct", hf_token=None):
66
  print("BasicAgent initialized.")
67
+ print("ENV-HF_TOKEN-LEN", len(hf_token), file=sys.stderr)
68
  check_token_access()
69
 
70
  # Local test
 
86
  # See list of base tools in https://github.com/huggingface/smolagents/blob/main/src/smolagents/default_tools.py
87
 
88
  # Initialize the agent
89
+ tok = AutoTokenizer.from_pretrained(model_id, token=hf_token)
90
+ mod = AutoModelForCausalLM.from_pretrained(
91
+ model_id,
92
+ torch_dtype=torch.float16,
93
+ device_map="auto", # auto-distributes to GPU
94
+ token=hf_token
95
+ )
96
+ self.pipe = pipeline(
97
+ "text-generation",
98
+ model=mod,
99
+ tokenizer=tok,
100
+ max_new_tokens=512,
101
+ temperature=0.0,
102
+ )
103
+
104
+ self.agent = CodeAgent(model=self, tools=[], add_base_tools=True)
105
 
106
  # self.agent = CodeAgent(
107
  # model=model,
 
109
  # add_base_tools=True
110
  # )
111
 
112
+ def _serialize_messages(self, messages):
113
+ prompt = []
114
+ for m in messages:
115
+ role = m['role'].value # "system" / "user" / "assistant"
116
+ text = "".join([c['text'] for c in m['content']])
117
+ prompt.append(f"{role}: {text}")
118
+ return "\n".join(prompt)
119
+
120
  def __call__(self, question: str) -> str:
121
  print(f"Agent received question (first 50 chars): {question[:50]}...")
122
+
123
+ allowed = {"max_new_tokens", "temperature", "top_k", "top_p"}
124
+ gen_kwargs = {k: v for k, v in kwargs.items() if k in allowed}
125
+ prompt_str = (
126
+ self._serialize_messages(prompt)
127
+ if isinstance(prompt, list)
128
+ else prompt
129
+ )
130
+ outputs = self.pipe(prompt_str, **gen_kwargs)
131
+ response = outputs[0]["generated_text"]
132
+ # response = self.agent.run(question)
133
  print(f"Agent returning its generated answer: {response}")
134
+
135
+ # wrap back into a chat message dict
136
+ return {
137
+ "role": MessageRole.ASSISTANT,
138
+ "content": [{"type": "text", "text": response}],
139
+ }
140
 
141
  def run_and_submit_all( profile: gr.OAuthProfile | None):
142
  """
 
145
  """
146
  # --- Determine HF Space Runtime URL and Repo URL ---
147
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
148
+ hf_token = os.getenv("HF_TOKEN")
149
 
150
  if profile:
151
  username= f"{profile.username}"
 
160
 
161
  # 1. Instantiate Agent ( modify this part to create your agent)
162
  try:
163
+ agent = BasicAgent(hf_token=hf_token).agent
164
  except Exception as e:
165
  print(f"Error instantiating agent: {e}")
166
  return f"Error initializing agent: {e}", None
 
200
  print(f"Skipping item with missing task_id or question: {item}")
201
  continue
202
  try:
203
+ msg = agent.run(question_text)
204
+ submitted_answer = msg["content"][0]["text"]
205
  answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
206
  results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
207
  except Exception as e:
 
297
  # Check for SPACE_HOST and SPACE_ID at startup for information
298
  space_host_startup = os.getenv("SPACE_HOST")
299
  space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
300
+ hf_token = os.getenv("HF_TOKEN")
301
 
302
  if space_host_startup:
303
  print(f"✅ SPACE_HOST found: {space_host_startup}")
 
315
  print("-"*(60 + len(" App Starting ")) + "\n")
316
 
317
  # Test the agent
318
+ agent = BasicAgent(hf_token=hf_token).agent
319
+ agent.run("What is 2+2?")
320
 
321
  print("Launching Gradio Interface for Basic Agent Evaluation...")
322
  demo.launch(debug=True, share=False)