schoolkithub commited on
Commit
a75a23e
·
verified ·
1 Parent(s): e79359e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -10,13 +10,17 @@ import wikipediaapi
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
 
13
  CONVERSATIONAL_MODELS = [
14
  "deepseek-ai/DeepSeek-LLM",
15
  "HuggingFaceH4/zephyr-7b-beta",
16
  "mistralai/Mistral-7B-Instruct-v0.2"
17
  ]
18
 
19
- wiki_api = wikipediaapi.Wikipedia(language="en", user_agent="SmartAgent/1.0 ([email protected])")
 
 
 
20
 
21
  # ==== SEARCH TOOLS ====
22
  def duckduckgo_search(query):
@@ -28,16 +32,29 @@ def wikipedia_search(query):
28
  page = wiki_api.page(query)
29
  return page.summary if page.exists() and page.summary else "No Wikipedia page found."
30
 
 
31
  def hf_chat_model(question):
32
  last_error = ""
33
  for model_id in CONVERSATIONAL_MODELS:
34
  try:
35
  hf_client = InferenceClient(model_id, token=HF_TOKEN)
36
- result = hf_client.conversational(
37
- messages=[{"role": "user", "content": question}],
38
- max_new_tokens=384,
39
- )
40
- # Acceptable result types: dict, obj with 'generated_text', or str
 
 
 
 
 
 
 
 
 
 
 
 
41
  if isinstance(result, dict) and "generated_text" in result:
42
  return f"[{model_id}] " + result["generated_text"]
43
  elif hasattr(result, "generated_text"):
@@ -47,8 +64,8 @@ def hf_chat_model(question):
47
  else:
48
  return f"[{model_id}] " + str(result)
49
  except Exception as e:
50
- last_error = f"({model_id}) {e}"
51
- return f"HF LLM error: {last_error}"
52
 
53
  # ==== SMART AGENT ====
54
  class SmartAgent:
@@ -58,7 +75,9 @@ class SmartAgent:
58
  def __call__(self, question: str) -> str:
59
  q_lower = question.lower()
60
  # DuckDuckGo for current/event/internet questions
61
- if any(term in q_lower for term in ["current", "latest", "2024", "2025", "who is the president", "recent", "live", "now", "today"]):
 
 
62
  duck_result = duckduckgo_search(question)
63
  if duck_result and "No DuckDuckGo" not in duck_result:
64
  return duck_result
 
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
 
13
+ # Supported conversational/text-gen models in order of preference
14
  CONVERSATIONAL_MODELS = [
15
  "deepseek-ai/DeepSeek-LLM",
16
  "HuggingFaceH4/zephyr-7b-beta",
17
  "mistralai/Mistral-7B-Instruct-v0.2"
18
  ]
19
 
20
+ wiki_api = wikipediaapi.Wikipedia(
21
+ language="en",
22
+ user_agent="SmartAgent/1.0 ([email protected])"
23
+ )
24
 
25
  # ==== SEARCH TOOLS ====
26
  def duckduckgo_search(query):
 
32
  page = wiki_api.page(query)
33
  return page.summary if page.exists() and page.summary else "No Wikipedia page found."
34
 
35
+ # ==== HUGGING FACE CHAT/TEXT-GEN TOOL ====
36
  def hf_chat_model(question):
37
  last_error = ""
38
  for model_id in CONVERSATIONAL_MODELS:
39
  try:
40
  hf_client = InferenceClient(model_id, token=HF_TOKEN)
41
+ # Try conversational endpoint first, if it exists
42
+ if hasattr(hf_client, "conversational"):
43
+ try:
44
+ result = hf_client.conversational(
45
+ messages=[{"role": "user", "content": question}],
46
+ max_new_tokens=384,
47
+ )
48
+ if isinstance(result, dict) and "generated_text" in result:
49
+ return f"[{model_id}] " + result["generated_text"]
50
+ elif hasattr(result, "generated_text"):
51
+ return f"[{model_id}] " + result.generated_text
52
+ elif isinstance(result, str):
53
+ return f"[{model_id}] " + result
54
+ except Exception as e:
55
+ last_error += f"({model_id}: conversational) {e}\n"
56
+ # Fall back to text_generation for all other models
57
+ result = hf_client.text_generation(question, max_new_tokens=384)
58
  if isinstance(result, dict) and "generated_text" in result:
59
  return f"[{model_id}] " + result["generated_text"]
60
  elif hasattr(result, "generated_text"):
 
64
  else:
65
  return f"[{model_id}] " + str(result)
66
  except Exception as e:
67
+ last_error += f"({model_id}: text_generation) {e}\n"
68
+ return f"HF LLM error: {last_error or 'No models produced output.'}"
69
 
70
  # ==== SMART AGENT ====
71
  class SmartAgent:
 
75
  def __call__(self, question: str) -> str:
76
  q_lower = question.lower()
77
  # DuckDuckGo for current/event/internet questions
78
+ if any(term in q_lower for term in [
79
+ "current", "latest", "2024", "2025", "who is the president", "recent", "live", "now", "today"
80
+ ]):
81
  duck_result = duckduckgo_search(question)
82
  if duck_result and "No DuckDuckGo" not in duck_result:
83
  return duck_result