jarguello76 commited on
Commit
d8c0fa7
·
verified ·
1 Parent(s): e76f3be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -13
app.py CHANGED
@@ -8,10 +8,13 @@ from langchain.docstore.document import Document
8
  from langchain.text_splitter import RecursiveCharacterTextSplitter
9
  from langchain_community.retrievers import BM25Retriever
10
 
11
- from smolagents import Tool, CodeAgent, InferenceClientModel
 
 
 
12
 
13
  hf_token = os.getenv("HUGGINGFACE_API_KEY")
14
- print("Token from env var:", os.getenv("HUGGINGFACE_API_KEY"))
15
  os.environ["HUGGINGFACE_API_KEY"] = hf_token
16
 
17
  if hf_token:
@@ -19,10 +22,11 @@ if hf_token:
19
  print("Set HUGGINGFACE_API_KEY in env.")
20
  else:
21
  print("No HUGGINGFACE_API_KEY found in env.")
22
-
23
  print("HUGGINGFACE_API_KEY in env:", "HUGGINGFACE_API_KEY" in os.environ)
24
  print("HUGGINGFACE_API_KEY value (masked):", os.environ.get("HUGGINGFACE_API_KEY", "")[:5] + "...")
25
-
 
26
  def run_and_submit_all(profile: gr.OAuthProfile | None):
27
  """
28
  Fetches all questions, runs the agent on them, submits all answers,
@@ -94,15 +98,27 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
94
 
95
  retriever_tool = RetrieverTool(docs_processed)
96
 
97
- # Instantiate the smolagents CodeAgent
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  agent = CodeAgent(
99
  tools=[retriever_tool],
100
- model=InferenceClientModel(model_id="Qwen/Qwen2.5-VL-7B-Instruct"),
101
  max_steps=4,
102
  verbosity_level=2,
103
  stream_outputs=True,
104
-
105
-
106
  )
107
 
108
  except Exception as e:
@@ -245,9 +261,6 @@ if __name__ == "__main__":
245
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
246
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
247
  else:
248
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
249
-
250
- print("-" * (60 + len(" App Starting ")) + "\n")
251
 
252
- print("Launching Gradio Interface for Basic Agent Evaluation...")
253
- demo.launch(debug=True, share=False)
 
8
  from langchain.text_splitter import RecursiveCharacterTextSplitter
9
  from langchain_community.retrievers import BM25Retriever
10
 
11
+ from smolagents import Tool, CodeAgent
12
+
13
+ from huggingface_hub.inference_api import InferenceApi # <--- NEW import
14
+
15
 
16
  hf_token = os.getenv("HUGGINGFACE_API_KEY")
17
+ print("Token from env var:", hf_token)
18
  os.environ["HUGGINGFACE_API_KEY"] = hf_token
19
 
20
  if hf_token:
 
22
  print("Set HUGGINGFACE_API_KEY in env.")
23
  else:
24
  print("No HUGGINGFACE_API_KEY found in env.")
25
+
26
  print("HUGGINGFACE_API_KEY in env:", "HUGGINGFACE_API_KEY" in os.environ)
27
  print("HUGGINGFACE_API_KEY value (masked):", os.environ.get("HUGGINGFACE_API_KEY", "")[:5] + "...")
28
+
29
+
30
  def run_and_submit_all(profile: gr.OAuthProfile | None):
31
  """
32
  Fetches all questions, runs the agent on them, submits all answers,
 
98
 
99
  retriever_tool = RetrieverTool(docs_processed)
100
 
101
+ # Initialize HF Inference API client with your token
102
+ inference = InferenceApi(repo_id="Qwen/Qwen2.5-VL-7B-Instruct", token=hf_token)
103
+
104
+ # Wrap inference client in a callable class for smolagents compatibility
105
+ class HuggingFaceInferenceWrapper:
106
+ def __init__(self, client):
107
+ self.client = client
108
+
109
+ def __call__(self, prompt: str):
110
+ response = self.client(inputs=prompt)
111
+ if isinstance(response, dict):
112
+ # Usually HF text generation returns {'generated_text': "..."}
113
+ return response.get("generated_text") or str(response)
114
+ return str(response)
115
+
116
  agent = CodeAgent(
117
  tools=[retriever_tool],
118
+ model=HuggingFaceInferenceWrapper(inference),
119
  max_steps=4,
120
  verbosity_level=2,
121
  stream_outputs=True,
 
 
122
  )
123
 
124
  except Exception as e:
 
261
  print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
262
  print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
263
  else:
264
+ print("ℹ️ SPACE_ID environment variable not found (running locally?).")
 
 
265
 
266
+ demo.launch(debug=True)