keynes42 commited on
Commit
674fb03
·
verified ·
1 Parent(s): 9e8b36d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -165
app.py CHANGED
@@ -1,177 +1,23 @@
1
- import os, sys
2
- from enum import Enum
3
  import gradio as gr
4
  import requests
5
  import inspect
6
- import subprocess
7
- import dateparser
8
- from bs4 import BeautifulSoup
9
- import regex
10
  import pandas as pd
11
- import torch
12
- from functools import lru_cache
13
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
14
- from smolagents import CodeAgent, WebSearchTool, WikipediaSearchTool, VisitWebpageTool, PythonInterpreterTool
15
- import smolagents.tools as _tools
16
- from smolagents.models import ChatMessage
17
- # from huggingface_hub import InferenceClient, hf_hub_download
18
-
19
- subprocess.run(["playwright", "install"], check=True)
20
-
21
- print(dir(_tools))
22
 
23
  # (Keep Constants as is)
24
  # --- Constants ---
25
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
26
 
27
- # class LocalLLM:
28
- # def __init__(self, pipe):
29
- # self.pipe = pipe
30
-
31
- # def generate(self, prompt, **kwargs):
32
- # unsupported_keys = ["stop_sequences"] # Remove keys not accepted by HF pipelines
33
- # cleaned_kwargs = {k: v for k, v in kwargs.items() if k not in unsupported_keys}
34
- # # print(f"🧪 kwargs cleaned: {cleaned_kwargs.keys()}")
35
- # try:
36
- # outputs = self.pipe(prompt, **cleaned_kwargs)
37
- # # print(f"🧪 Raw output from pipe: {outputs}")
38
- # if isinstance(outputs, list) and isinstance(outputs[0], dict):
39
- # out = outputs[0]["generated_text"]
40
- # elif isinstance(outputs, list):
41
- # out = outputs[0] # fallback if it's just a list of strings
42
- # else:
43
- # out = str(outputs)
44
- # print("🧪 Final object to return:", type(out), out[:100])
45
- # return {'role': 'assistant', 'content': [{'type':'text', 'text': out}]}
46
- # except Exception as e:
47
- # print(f"❌ Error in LocalLLM.generate(): {e}")
48
- # raise
49
-
50
- def check_token_access():
51
- token = os.environ.get("HF_TOKEN", "")
52
- if not token:
53
- print("❌ No token found")
54
- return
55
- headers = {"Authorization": f"Bearer {token}"}
56
- url = "https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/resolve/main/config.json"
57
- try:
58
- r = requests.get(url, headers=headers, timeout=10)
59
- print(f"🔍 Token test response: {r.status_code}")
60
- if r.status_code == 200:
61
- print("✅ Token access confirmed for gated model.")
62
- elif r.status_code == 403:
63
- print("❌ 403 Forbidden: Token does not have access.")
64
- else:
65
- print("⚠️ Unexpected status:", r.status_code)
66
- except Exception as e:
67
- print("❌ Token check failed:", e)
68
-
69
- class CachedWebSearchTool(WebSearchTool):
70
- @lru_cache(maxsize=128)
71
- def run(self, query: str):
72
- # identical queries return instantly
73
- return super().run(query)
74
-
75
- class CachedWikiTool(WikipediaSearchTool):
76
- @lru_cache(maxsize=128)
77
- def run(self, page: str):
78
- return super().run(page)
79
-
80
  # --- Basic Agent Definition ---
81
- # ----- THIS IS WHERE YOU CAN BUILD WHAT YOU WANT ------
82
  class BasicAgent:
83
- def __init__(self, model_id="meta-llama/Llama-3.1-8B-Instruct", hf_token=""):
84
  print("BasicAgent initialized.")
85
- print("ENV-HF_TOKEN-LEN", len(hf_token), file=sys.stderr)
86
- check_token_access()
87
-
88
- # Local test
89
- # client = InferenceClient(
90
- # model="meta-llama/Llama-3.1-8B-Instruct",
91
- # token=os.environ["HF_TOKEN"]
92
- # )
93
- # print(client.text_generation("Hello, my name is", max_new_tokens=20))
94
-
95
- # Initialize the model
96
- # model = HfApiModel(model_id="meta-llama/Llama-3.1-8B-Instruct",
97
- # # format="text-generation",
98
- # token=os.environ["HF_TOKEN"],
99
- # max_tokens=2048,
100
- # temperature=0.0
101
- # )
102
-
103
- # Initialize the tools other than the base tools
104
- # See list of base tools in https://github.com/huggingface/smolagents/blob/main/src/smolagents/default_tools.py
105
-
106
- # Download the model weights and build the pipeline
107
- tok = AutoTokenizer.from_pretrained(model_id, token=hf_token)
108
- mod = AutoModelForCausalLM.from_pretrained(
109
- model_id,
110
- torch_dtype=torch.float16,
111
- device_map="auto", # auto-distributes to GPU
112
- token=hf_token
113
- )
114
- self.pipe = pipeline(
115
- "text-generation",
116
- model=mod,
117
- tokenizer=tok,
118
- max_new_tokens=512,
119
- return_full_text=False, # <— only get the completion, not the prompt + completion
120
- # temperature=1.0,
121
- )
122
- # Introduce tools
123
- wiki_tool = CachedWikiTool()
124
- search_tool = CachedWebSearchTool()
125
- python_tool = PythonInterpreterTool()
126
- html_parse_tool = VisitWebpageTool()
127
- # Initialize the agent
128
- self.agent = CodeAgent(model=self,
129
- tools=[wiki_tool, search_tool, python_tool, html_parse_tool],
130
- add_base_tools=True,
131
- additional_authorized_imports=["dateparser", "bs4", "regex"])
132
-
133
- def _serialize_messages(self, messages):
134
- prompt = []
135
- for m in messages:
136
- r = m["role"]
137
- role = r.value if isinstance(r, Enum) and hasattr(r, "value") else r # "system" / "user" / "assistant"
138
- text = "".join([c['text'] for c in m['content']])
139
- prompt.append(f"{role}: {text}")
140
- return "\n".join(prompt)
141
-
142
- def generate(self, question: str, stop_sequences=None, **kwargs) -> str:
143
  print(f"Agent received question (first 50 chars): {question[:50]}...")
144
- # 1. Build the HF kwargs
145
- allowed = {"max_new_tokens", "temperature", "top_k", "top_p"}
146
- gen_kwargs = {k: v for k, v in kwargs.items() if k in allowed}
147
-
148
- # 2. Serialize the message and get the response
149
- prompt_str = (
150
- self._serialize_messages(question)
151
- if isinstance(question, list)
152
- else question
153
- )
154
- outputs = self.pipe(prompt_str, **gen_kwargs)
155
- response = outputs[0]["generated_text"]
156
- # response = self.agent.run(question)
157
-
158
- # 3. Optionally map SmolAgents’ stop_sequences → HF pipeline’s 'stop'
159
- if stop_sequences:
160
- # find the earliest occurrence of any stop token
161
- cuts = [response.find(s) for s in stop_sequences if response.find(s) != -1]
162
- if cuts:
163
- response = response[: min(cuts)]
164
-
165
- print(f"Agent returning its generated answer: {response}")
166
-
167
- # wrap back into a chat message dict
168
- return ChatMessage(role="assistant", content=response)
169
- # return {
170
- # "role": 'assistant',
171
- # "content": [{"type": "text", "text": response}],
172
- # }
173
-
174
- __call__ = generate
175
 
176
  def run_and_submit_all( profile: gr.OAuthProfile | None):
177
  """
@@ -180,7 +26,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
180
  """
181
  # --- Determine HF Space Runtime URL and Repo URL ---
182
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
183
- hf_token = os.getenv("HF_TOKEN")
184
 
185
  if profile:
186
  username= f"{profile.username}"
@@ -195,7 +40,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
195
 
196
  # 1. Instantiate Agent ( modify this part to create your agent)
197
  try:
198
- agent = BasicAgent(hf_token=hf_token).agent
199
  except Exception as e:
200
  print(f"Error instantiating agent: {e}")
201
  return f"Error initializing agent: {e}", None
@@ -224,8 +69,6 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
224
  print(f"An unexpected error occurred fetching questions: {e}")
225
  return f"An unexpected error occurred fetching questions: {e}", None
226
 
227
- questions_data = questions_data[:5]
228
-
229
  # 3. Run your Agent
230
  results_log = []
231
  answers_payload = []
 
1
+ import os
 
2
  import gradio as gr
3
  import requests
4
  import inspect
 
 
 
 
5
  import pandas as pd
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  # --- Basic Agent Definition ---
12
+ # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
13
  class BasicAgent:
14
+ def __init__(self):
15
  print("BasicAgent initialized.")
16
+ def __call__(self, question: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  print(f"Agent received question (first 50 chars): {question[:50]}...")
18
+ fixed_answer = "This is a default answer."
19
+ print(f"Agent returning fixed answer: {fixed_answer}")
20
+ return fixed_answer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
 
26
  """
27
  # --- Determine HF Space Runtime URL and Repo URL ---
28
  space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
 
29
 
30
  if profile:
31
  username= f"{profile.username}"
 
40
 
41
  # 1. Instantiate Agent ( modify this part to create your agent)
42
  try:
43
+ agent = BasicAgent()
44
  except Exception as e:
45
  print(f"Error instantiating agent: {e}")
46
  return f"Error initializing agent: {e}", None
 
69
  print(f"An unexpected error occurred fetching questions: {e}")
70
  return f"An unexpected error occurred fetching questions: {e}", None
71
 
 
 
72
  # 3. Run your Agent
73
  results_log = []
74
  answers_payload = []