wt002 commited on
Commit
5019abb
·
verified ·
1 Parent(s): 7c3b427

update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -24
app.py CHANGED
@@ -21,44 +21,36 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
21
  # --- Basic Agent Definition ---
22
 
23
  class BasicAgent:
24
- def __init__(self, model="google/gemma-7b"):
25
- # Initialize the HF Agent with your model endpoint
26
- self.agent = HfAgent(
27
- endpoint=f"https://api-inference.huggingface.co/models/{model}",
28
- headers={"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
29
- )
30
- print("BasicAgent initialized.")
31
 
32
  def __call__(self, question: str) -> str:
33
  print(f"Question: {question[:50]}...")
34
- return self.agent.run(question) # For full agent workflow
35
- # OR for simple chat:
36
- # return self.agent.chat(question)
37
-
38
- def generate_response(self, prompt: str) -> str:
39
- """Get response from model using chat interface"""
40
- try:
41
- response = self.agent.chat(prompt)
42
- return response
43
- except Exception as e:
44
- return f"Error generating response: {str(e)}"
45
-
46
-
47
 
48
  def web_search(self, query: str) -> List[Dict]:
49
- """Use SearxNG meta-search engine"""
50
  params = {
51
  "q": query,
52
  "format": "json",
53
  "engines": "google,bing,duckduckgo"
54
  }
55
  try:
56
- response = requests.get(self.searx_url, params=params)
57
  response.raise_for_status()
58
  return response.json().get("results", [])
59
- except requests.RequestException:
 
60
  return []
61
-
 
 
 
62
  def wikipedia_search(self, query: str) -> str:
63
  """Get Wikipedia summary"""
64
  page = self.wiki.page(query)
 
21
  # --- Basic Agent Definition ---
22
 
23
  class BasicAgent:
24
+ def __init__(self, searx_url: str = "https://searx.example.org"):
25
+ # Initialize HF Agent
26
+ self.agent = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
27
+
28
+ # Initialize web search configuration
29
+ self.searx_url = searx_url # You should replace with an actual SearxNG instance
30
+ print("BasicAgent initialized with HfAgent and web search capabilities")
31
 
32
  def __call__(self, question: str) -> str:
33
  print(f"Question: {question[:50]}...")
34
+ return self.agent.chat(question)
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  def web_search(self, query: str) -> List[Dict]:
37
+ """Use SearxNG meta-search engine to search the web"""
38
  params = {
39
  "q": query,
40
  "format": "json",
41
  "engines": "google,bing,duckduckgo"
42
  }
43
  try:
44
+ response = requests.get(self.searx_url, params=params, timeout=10)
45
  response.raise_for_status()
46
  return response.json().get("results", [])
47
+ except requests.RequestException as e:
48
+ print(f"Search error: {str(e)}")
49
  return []
50
+ except Exception as e:
51
+ print(f"Unexpected error: {str(e)}")
52
+ return []
53
+
54
  def wikipedia_search(self, query: str) -> str:
55
  """Get Wikipedia summary"""
56
  page = self.wiki.page(query)