davanstrien HF Staff commited on
Commit
f289ebb
·
1 Parent(s): b24a29b

Fix timeout issues and improve error handling

Browse files

- Increase HTTP timeout from 30s to 60s for MCP usage
- Update default base URL to point to actual HF Space instead of localhost
- Add comprehensive error handling for timeouts and HTTP errors
- Add logging to help debug connection issues
- Display backend API URL in the UI for transparency

Files changed (1) hide show
  1. app.py +35 -14
app.py CHANGED
@@ -4,14 +4,19 @@ MCP Server for Hugging Face Dataset and Model Search API using Gradio
4
  """
5
 
6
  import os
 
7
  from typing import Optional
8
 
9
  import gradio as gr
10
  import httpx
11
 
12
- # Initialize HTTP client
13
- client = httpx.Client(timeout=30.0)
14
- base_url = os.getenv("HF_SEARCH_API_URL", "http://localhost:8000")
 
 
 
 
15
 
16
 
17
  def search_datasets(
@@ -37,17 +42,32 @@ def search_datasets(
37
  Returns:
38
  Formatted search results with dataset IDs, summaries, and metadata
39
  """
40
- params = {
41
- "query": query,
42
- "k": k,
43
- "sort_by": sort_by,
44
- "min_likes": min_likes,
45
- "min_downloads": min_downloads
46
- }
47
-
48
- response = client.get(f"{base_url}/search/datasets", params=params)
49
- response.raise_for_status()
50
- data = response.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  results = data.get("results", [])
53
  if not results:
@@ -383,6 +403,7 @@ def download_dataset_card(dataset_id: str) -> str:
383
  with gr.Blocks(title="HuggingFace Search MCP Server") as demo:
384
  gr.Markdown("# HuggingFace Search MCP Server")
385
  gr.Markdown("This server provides semantic search capabilities for HuggingFace models and datasets.")
 
386
 
387
  with gr.Tab("Search Datasets"):
388
  gr.Interface(
 
4
  """
5
 
6
  import os
7
+ import logging
8
  from typing import Optional
9
 
10
  import gradio as gr
11
  import httpx
12
 
13
+ # Set up logging
14
+ logging.basicConfig(level=logging.INFO)
15
+ logger = logging.getLogger(__name__)
16
+
17
+ # Initialize HTTP client with longer timeout for MCP usage
18
+ client = httpx.Client(timeout=60.0) # Increased timeout
19
+ base_url = os.getenv("HF_SEARCH_API_URL", "https://davanstrien-huggingface-datasets-search-v2.hf.space")
20
 
21
 
22
  def search_datasets(
 
42
  Returns:
43
  Formatted search results with dataset IDs, summaries, and metadata
44
  """
45
+ try:
46
+ logger.info(f"Searching datasets: query='{query}', k={k}, sort_by='{sort_by}'")
47
+
48
+ params = {
49
+ "query": query,
50
+ "k": k,
51
+ "sort_by": sort_by,
52
+ "min_likes": min_likes,
53
+ "min_downloads": min_downloads
54
+ }
55
+
56
+ logger.info(f"Making request to: {base_url}/search/datasets")
57
+ response = client.get(f"{base_url}/search/datasets", params=params)
58
+ response.raise_for_status()
59
+ data = response.json()
60
+
61
+ logger.info(f"Successfully retrieved {len(data.get('results', []))} results")
62
+ except httpx.TimeoutException:
63
+ logger.error(f"Request timed out for query: {query}")
64
+ return "Request timed out. The search service may be slow or unavailable."
65
+ except httpx.HTTPStatusError as e:
66
+ logger.error(f"HTTP error {e.response.status_code}: {e.response.text}")
67
+ return f"Search failed with HTTP error {e.response.status_code}: {e.response.text}"
68
+ except Exception as e:
69
+ logger.error(f"Unexpected error: {str(e)}")
70
+ return f"Search failed: {str(e)}"
71
 
72
  results = data.get("results", [])
73
  if not results:
 
403
  with gr.Blocks(title="HuggingFace Search MCP Server") as demo:
404
  gr.Markdown("# HuggingFace Search MCP Server")
405
  gr.Markdown("This server provides semantic search capabilities for HuggingFace models and datasets.")
406
+ gr.Markdown(f"**Backend API:** {base_url}")
407
 
408
  with gr.Tab("Search Datasets"):
409
  gr.Interface(