wt002 commited on
Commit
202ae29
·
verified ·
1 Parent(s): 1d3f805

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -8,7 +8,8 @@ import requests
8
  import wikipediaapi
9
  import pandas as pd
10
  from transformers import pipeline # or HfAgent if you want the higher-level agent
11
- from huggingface_hub import HfAgent
 
12
 
13
  load_dotenv()
14
 
@@ -20,20 +21,22 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
20
  # --- Basic Agent Definition ---
21
 
22
  class BasicAgent:
23
- def __init__(self, searx_url: str = "https://searx.example.org"):
24
- # Initialize HF Agent
25
- self.agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
26
-
27
- # Initialize web search configuration
28
- self.searx_url = searx_url # You should replace with an actual SearxNG instance
29
- print("BasicAgent initialized with HfAgent and web search capabilities")
30
 
31
  def __call__(self, question: str) -> str:
32
  print(f"Question: {question[:50]}...")
33
- return self.agent.chat(question)
 
 
 
 
34
 
35
  def web_search(self, query: str) -> List[Dict]:
36
- """Use SearxNG meta-search engine to search the web"""
37
  params = {
38
  "q": query,
39
  "format": "json",
@@ -46,9 +49,6 @@ class BasicAgent:
46
  except requests.RequestException as e:
47
  print(f"Search error: {str(e)}")
48
  return []
49
- except Exception as e:
50
- print(f"Unexpected error: {str(e)}")
51
- return []
52
 
53
  def wikipedia_search(self, query: str) -> str:
54
  """Get Wikipedia summary"""
 
8
  import wikipediaapi
9
  import pandas as pd
10
  from transformers import pipeline # or HfAgent if you want the higher-level agent
11
+ from huggingface_hub import InferenceClient # Updated import
12
+ from huggingface_hub.inference._text_generation import TextGenerationResponse
13
 
14
  load_dotenv()
15
 
 
21
  # --- Basic Agent Definition ---
22
 
23
  class BasicAgent:
24
+ def __init__(self, searx_url: str = "https://search.example.org"):
25
+ # Initialize HF Inference Client
26
+ self.client = InferenceClient(model="bigcode/starcoder")
27
+ self.searx_url = searx_url
28
+ print("BasicAgent initialized with Inference API")
 
 
29
 
30
  def __call__(self, question: str) -> str:
31
  print(f"Question: {question[:50]}...")
32
+ response: TextGenerationResponse = self.client.text_generation(
33
+ prompt=question,
34
+ max_new_tokens=100
35
+ )
36
+ return response
37
 
38
  def web_search(self, query: str) -> List[Dict]:
39
+ """Use SearxNG meta-search engine"""
40
  params = {
41
  "q": query,
42
  "format": "json",
 
49
  except requests.RequestException as e:
50
  print(f"Search error: {str(e)}")
51
  return []
 
 
 
52
 
53
  def wikipedia_search(self, query: str) -> str:
54
  """Get Wikipedia summary"""