Spaces:
Runtime error
Runtime error
fixing
Browse files
app.py
CHANGED
@@ -10,6 +10,8 @@ import json
|
|
10 |
import tempfile
|
11 |
import urllib.parse
|
12 |
from pathlib import Path
|
|
|
|
|
13 |
|
14 |
# --- Constants ---
|
15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
@@ -29,6 +31,21 @@ class HfApiModel:
|
|
29 |
outputs = self.pipe(prompt, max_new_tokens=512, do_sample=True)
|
30 |
return outputs[0]["generated_text"]
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# --- Custom Tools ---
|
33 |
class SerperSearchTool:
|
34 |
"""Enhanced search tool using Serper API for more reliable results"""
|
@@ -44,7 +61,6 @@ class SerperSearchTool:
|
|
44 |
def __call__(self, query: str) -> str:
|
45 |
"""Search the web and return formatted results"""
|
46 |
if not self.api_key:
|
47 |
-
# Fallback to basic search if no Serper API key
|
48 |
return f"Search query: {query} - API key not available"
|
49 |
|
50 |
try:
|
@@ -64,22 +80,20 @@ class SerperSearchTool:
|
|
64 |
data = response.json()
|
65 |
results = []
|
66 |
|
67 |
-
# Process organic results
|
68 |
if 'organic' in data:
|
69 |
-
for item in data['organic'][:3]:
|
70 |
results.append(f"Title: {item.get('title', 'N/A')}")
|
71 |
results.append(f"Content: {item.get('snippet', 'N/A')}")
|
72 |
results.append(f"URL: {item.get('link', 'N/A')}")
|
73 |
results.append("---")
|
74 |
|
75 |
-
# Add answer box if available
|
76 |
if 'answerBox' in data:
|
77 |
answer = data['answerBox']
|
78 |
results.insert(0, f"Answer: {answer.get('answer', answer.get('snippet', 'N/A'))}")
|
79 |
results.insert(1, "---")
|
80 |
|
81 |
return "\n".join(results) if results else f"No results found for: {query}"
|
82 |
-
|
83 |
except Exception as e:
|
84 |
print(f"Serper search error: {e}")
|
85 |
return f"Search error for '{query}': {str(e)}"
|
|
|
10 |
import tempfile
|
11 |
import urllib.parse
|
12 |
from pathlib import Path
|
13 |
+
from duckduckgo_search import DDGS
|
14 |
+
|
15 |
|
16 |
# --- Constants ---
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
31 |
outputs = self.pipe(prompt, max_new_tokens=512, do_sample=True)
|
32 |
return outputs[0]["generated_text"]
|
33 |
|
34 |
+
class DuckDuckGoSearchTool:
|
35 |
+
name = "duckduckgo_search"
|
36 |
+
description = "Use DuckDuckGo to search the web."
|
37 |
+
|
38 |
+
def __call__(self, query: str) -> str:
|
39 |
+
try:
|
40 |
+
results = []
|
41 |
+
with DDGS() as ddgs:
|
42 |
+
for r in ddgs.text(query, max_results=3):
|
43 |
+
results.append(f"Title: {r['title']}\nURL: {r['href']}\nSnippet: {r['body']}\n---")
|
44 |
+
return "\n".join(results) if results else "No results found."
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error using DuckDuckGoSearchTool: {e}"
|
47 |
+
|
48 |
+
|
49 |
# --- Custom Tools ---
|
50 |
class SerperSearchTool:
|
51 |
"""Enhanced search tool using Serper API for more reliable results"""
|
|
|
61 |
def __call__(self, query: str) -> str:
|
62 |
"""Search the web and return formatted results"""
|
63 |
if not self.api_key:
|
|
|
64 |
return f"Search query: {query} - API key not available"
|
65 |
|
66 |
try:
|
|
|
80 |
data = response.json()
|
81 |
results = []
|
82 |
|
|
|
83 |
if 'organic' in data:
|
84 |
+
for item in data['organic'][:3]:
|
85 |
results.append(f"Title: {item.get('title', 'N/A')}")
|
86 |
results.append(f"Content: {item.get('snippet', 'N/A')}")
|
87 |
results.append(f"URL: {item.get('link', 'N/A')}")
|
88 |
results.append("---")
|
89 |
|
|
|
90 |
if 'answerBox' in data:
|
91 |
answer = data['answerBox']
|
92 |
results.insert(0, f"Answer: {answer.get('answer', answer.get('snippet', 'N/A'))}")
|
93 |
results.insert(1, "---")
|
94 |
|
95 |
return "\n".join(results) if results else f"No results found for: {query}"
|
96 |
+
|
97 |
except Exception as e:
|
98 |
print(f"Serper search error: {e}")
|
99 |
return f"Search error for '{query}': {str(e)}"
|