dmfelder commited on
Commit
d8adac1
·
verified ·
1 Parent(s): fa8ab53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -17
app.py CHANGED
@@ -17,7 +17,10 @@ import soundfile as sf
17
  import numpy as np
18
  from requests.adapters import HTTPAdapter
19
  from urllib3.util.retry import Retry
20
- from huggingface_hub import InferenceClient, get_inference_client, Agent
 
 
 
21
 
22
  # Setup log file path
23
  log_path = os.path.join(os.getcwd(), f"agent_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
@@ -124,14 +127,8 @@ class SmartAgent:
124
  def _initialize_agent(self) -> Agent:
125
  logging.info("Initializing Hugging Face Agent...")
126
  try:
127
- # Ensure HF_TOKEN is available in the environment or via login
128
- hf_token = os.environ.get("HF_TOKEN")
129
- if not hf_token:
130
- # Fallback to get_inference_client's default token handling (e.g., from ~/.cache/huggingface/token)
131
- logging.warning("HF_TOKEN environment variable not set. Relying on Hugging Face Hub client token.")
132
- client = get_inference_client() # This will pick up token from env or cache
133
- else:
134
- client = InferenceClient(token=hf_token)
135
 
136
  # Check if agent already exists for this username
137
  list_agents_resp = self.http_session.get(f"{API_URL}/agents")
@@ -271,6 +268,7 @@ class SmartAgent:
271
  try:
272
  if tool_name == "image-to-text":
273
  image_bytes = self.http_session.get(inputs["image"]).content
 
274
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_img_file:
275
  tmp_img_file.write(image_bytes)
276
  image_path = tmp_img_file.name
@@ -340,14 +338,9 @@ class SmartAgent:
340
  logging.info("Cleaning up agent resources...")
341
  if self.agent and self.agent_id:
342
  try:
343
- # Assuming the Hugging Face Agent object has a delete method or similar
344
- # For this course, you might need to call a specific API endpoint or rely on
345
- # the agent session expiring if no direct delete method is provided by the lib.
346
- # If there's a specific API endpoint for deleting agents, use it here.
347
- # For now, we'll log its ID as deleted.
348
- # If agent.delete() exists, use it:
349
- # self.agent.delete()
350
- # For the course, simply logging and clearing internal state might be enough.
351
  logging.info(f"Agent with ID {self.agent_id} is conceptually deleted (or will expire).")
352
  except Exception as e:
353
  logging.warning(f"Failed to delete agent or clean up its remote state: {e}")
 
17
  import numpy as np
18
  from requests.adapters import HTTPAdapter
19
  from urllib3.util.retry import Retry
20
+
21
+ # CORRECTED IMPORTS:
22
+ from huggingface_hub import InferenceClient
23
+ from huggingface_hub.agents import Agent # Corrected import path for Agent
24
 
25
  # Setup log file path
26
  log_path = os.path.join(os.getcwd(), f"agent_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
 
127
  def _initialize_agent(self) -> Agent:
128
  logging.info("Initializing Hugging Face Agent...")
129
  try:
130
+ # InferenceClient will automatically pick up HF_TOKEN from env or cache
131
+ client = InferenceClient()
 
 
 
 
 
 
132
 
133
  # Check if agent already exists for this username
134
  list_agents_resp = self.http_session.get(f"{API_URL}/agents")
 
268
  try:
269
  if tool_name == "image-to-text":
270
  image_bytes = self.http_session.get(inputs["image"]).content
271
+ # Use NamedTemporaryFile for safer unique file handling
272
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_img_file:
273
  tmp_img_file.write(image_bytes)
274
  image_path = tmp_img_file.name
 
338
  logging.info("Cleaning up agent resources...")
339
  if self.agent and self.agent_id:
340
  try:
341
+ # If the Hugging Face Agent library provides a direct delete method, use it.
342
+ # Otherwise, the agent will typically expire after inactivity.
343
+ # Example if agent.delete() exists: self.agent.delete()
 
 
 
 
 
344
  logging.info(f"Agent with ID {self.agent_id} is conceptually deleted (or will expire).")
345
  except Exception as e:
346
  logging.warning(f"Failed to delete agent or clean up its remote state: {e}")