cmulgy commited on
Commit
3468847
·
1 Parent(s): be0b6e7

Fix dropdown functionality for Hugging Face Spaces - improve dataset loading with multiple fallback approaches and better error handling

Browse files
Files changed (2) hide show
  1. .gitignore +5 -1
  2. demo.py +107 -15
.gitignore CHANGED
@@ -59,4 +59,8 @@ logs/
59
  *.temp
60
 
61
  # Hugging Face Spaces specific
62
- .cache/
 
 
 
 
 
59
  *.temp
60
 
61
  # Hugging Face Spaces specific
62
+ .cache/
63
+
64
+ # HuggingFace datasets cache
65
+ cache/
66
+ .huggingface/
demo.py CHANGED
@@ -250,20 +250,51 @@ def get_template_subset_name(model_size: str, template_size: str) -> str:
250
 
251
  def load_template_dataset(model_size: str, template_size: str) -> pd.DataFrame:
252
  """
253
- Load thought templates from HuggingFace dataset.
254
  """
255
  subset_name = get_template_subset_name(model_size, template_size)
256
 
257
- try:
258
- print(f"Loading templates from HuggingFace dataset: ulab-ai/FusionBench, subset: {subset_name}")
259
- dataset = load_dataset("ulab-ai/FusionBench", subset_name)
260
- template_df = pd.DataFrame(dataset['data'])
261
- print(f"Loaded {len(template_df)} templates from {subset_name}")
262
-
263
- return template_df
264
-
265
- except Exception as e:
266
- raise ValueError(f"Failed to load template dataset {subset_name}: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
  def enhance_query_with_templates(
269
  model_size: str,
@@ -286,6 +317,11 @@ def enhance_query_with_templates(
286
  # Load template data from HuggingFace dataset
287
  template_df = load_template_dataset(model_size, template_size)
288
 
 
 
 
 
 
289
  # Generate embedding for the query if not provided
290
  if query_embedding is None:
291
  try:
@@ -296,8 +332,9 @@ def enhance_query_with_templates(
296
  return query, []
297
 
298
  # Filter templates by task description if provided
299
- if task_description is None:
300
  matching_templates = template_df
 
301
  else:
302
  matching_templates = template_df[template_df['task_description'] == task_description]
303
 
@@ -307,15 +344,17 @@ def enhance_query_with_templates(
307
 
308
  if not partial_matches.empty:
309
  matching_templates = partial_matches
310
- print(f"Found partial matches for task: {task_description[:50]}...")
311
  else:
312
- print(f"No matching templates found for task: {task_description[:50]}...")
313
  matching_templates = template_df
314
 
315
  if matching_templates.empty:
316
  print("No matching templates found. Returning original query.")
317
  return query, []
318
 
 
 
319
  # Calculate similarities with template embeddings
320
  similarities = []
321
 
@@ -324,7 +363,11 @@ def enhance_query_with_templates(
324
 
325
  # Try to parse existing template embedding
326
  if 'query_embedding' in t_row and not pd.isna(t_row['query_embedding']):
327
- template_embedding = parse_embedding(t_row['query_embedding'])
 
 
 
 
328
 
329
  # If no valid embedding found, generate one for the template query
330
  if template_embedding is None and 'query' in t_row:
@@ -992,6 +1035,55 @@ def process_thought_template_query(query, template_style, task_description, top_
992
  return error_msg, "", ""
993
 
994
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
995
  # Create Gradio interface
996
  def create_interface():
997
  with gr.Blocks(
 
250
 
251
  def load_template_dataset(model_size: str, template_size: str) -> pd.DataFrame:
252
  """
253
+ Load thought templates from HuggingFace dataset with robust error handling for Spaces deployment.
254
  """
255
  subset_name = get_template_subset_name(model_size, template_size)
256
 
257
+ # Try multiple approaches to load the dataset
258
+ approaches = [
259
+ # Approach 1: Direct load with timeout
260
+ lambda: load_dataset("ulab-ai/FusionBench", subset_name, trust_remote_code=True),
261
+ # Approach 2: Load with cache_dir specification
262
+ lambda: load_dataset("ulab-ai/FusionBench", subset_name, cache_dir="./cache", trust_remote_code=True),
263
+ # Approach 3: Load with streaming (for large datasets)
264
+ lambda: load_dataset("ulab-ai/FusionBench", subset_name, streaming=True, trust_remote_code=True),
265
+ ]
266
+
267
+ for i, approach in enumerate(approaches, 1):
268
+ try:
269
+ print(f"Attempting to load templates (approach {i}): ulab-ai/FusionBench, subset: {subset_name}")
270
+
271
+ dataset = approach()
272
+
273
+ # Handle streaming dataset
274
+ if hasattr(dataset, 'iter') and callable(dataset.iter):
275
+ # Convert streaming dataset to list
276
+ data_list = list(dataset['data'])
277
+ template_df = pd.DataFrame(data_list)
278
+ else:
279
+ # Regular dataset
280
+ template_df = pd.DataFrame(dataset['data'])
281
+
282
+ print(f"✅ Successfully loaded {len(template_df)} templates from {subset_name}")
283
+ return template_df
284
+
285
+ except Exception as e:
286
+ print(f"❌ Approach {i} failed: {str(e)}")
287
+ if i == len(approaches):
288
+ # All approaches failed, provide detailed error
289
+ error_msg = f"Failed to load template dataset {subset_name} after trying {len(approaches)} approaches. Last error: {str(e)}"
290
+ print(error_msg)
291
+
292
+ # Return empty DataFrame with warning
293
+ print("⚠️ Returning empty template DataFrame - functionality will be limited")
294
+ return pd.DataFrame(columns=['query', 'thought_template', 'task_description', 'query_embedding'])
295
+
296
+ # This should never be reached, but just in case
297
+ return pd.DataFrame(columns=['query', 'thought_template', 'task_description', 'query_embedding'])
298
 
299
  def enhance_query_with_templates(
300
  model_size: str,
 
317
  # Load template data from HuggingFace dataset
318
  template_df = load_template_dataset(model_size, template_size)
319
 
320
+ # Check if dataset is empty (failed to load)
321
+ if template_df.empty:
322
+ print("⚠️ Template dataset is empty - returning original query")
323
+ return query, []
324
+
325
  # Generate embedding for the query if not provided
326
  if query_embedding is None:
327
  try:
 
332
  return query, []
333
 
334
  # Filter templates by task description if provided
335
+ if task_description is None or not task_description.strip():
336
  matching_templates = template_df
337
+ print(f"Using all {len(matching_templates)} templates (no task filter)")
338
  else:
339
  matching_templates = template_df[template_df['task_description'] == task_description]
340
 
 
344
 
345
  if not partial_matches.empty:
346
  matching_templates = partial_matches
347
+ print(f"Found partial matches for task: {task_description[:50]}... ({len(matching_templates)} templates)")
348
  else:
349
+ print(f"No matching templates found for task: {task_description[:50]}... - using all templates")
350
  matching_templates = template_df
351
 
352
  if matching_templates.empty:
353
  print("No matching templates found. Returning original query.")
354
  return query, []
355
 
356
+ print(f"Processing {len(matching_templates)} templates for similarity calculation...")
357
+
358
  # Calculate similarities with template embeddings
359
  similarities = []
360
 
 
363
 
364
  # Try to parse existing template embedding
365
  if 'query_embedding' in t_row and not pd.isna(t_row['query_embedding']):
366
+ try:
367
+ template_embedding = parse_embedding(t_row['query_embedding'])
368
+ except Exception as e:
369
+ print(f"Failed to parse template embedding: {str(e)}")
370
+ template_embedding = None
371
 
372
  # If no valid embedding found, generate one for the template query
373
  if template_embedding is None and 'query' in t_row:
 
1035
  return error_msg, "", ""
1036
 
1037
 
1038
+ # Test function to verify dropdown functionality
1039
+ def test_dropdown_functionality():
1040
+ """Test function to verify dropdown components are working"""
1041
+ print("Testing dropdown functionality...")
1042
+
1043
+ # Test template style mapping
1044
+ style_mapping = {
1045
+ "8b_full": ("8b", "full"),
1046
+ "8b_small": ("8b", "small"),
1047
+ "70b_full": ("70b", "full"),
1048
+ "70b_small": ("70b", "small")
1049
+ }
1050
+
1051
+ for style, (model_size, template_size) in style_mapping.items():
1052
+ print(f"✅ Template style '{style}' maps to model_size='{model_size}', template_size='{template_size}'")
1053
+
1054
+ # Test benchmark task options
1055
+ benchmark_tasks = [
1056
+ ("All Tasks", ""),
1057
+ ("ARC-Challenge", "ARC-Challenge"),
1058
+ ("BoolQ", "BoolQ"),
1059
+ ("CommonsenseQA", "CommonsenseQA"),
1060
+ ("GPQA", "GPQA"),
1061
+ ("GSM8K", "GSM8K"),
1062
+ ("HellaSwag", "HellaSwag"),
1063
+ ("HumanEval", "HumanEval"),
1064
+ ("MATH", "MATH"),
1065
+ ("MBPP", "MBPP"),
1066
+ ("MMLU", "MMLU"),
1067
+ ("Natural Questions", "Natural Questions"),
1068
+ ("OpenBookQA", "OpenBookQA"),
1069
+ ("SQuAD", "SQuAD"),
1070
+ ("TriviaQA", "TriviaQA")
1071
+ ]
1072
+
1073
+ print(f"✅ {len(benchmark_tasks)} benchmark task options available")
1074
+
1075
+ return True
1076
+
1077
+ # Run test on import
1078
+ if __name__ == "__main__":
1079
+ test_dropdown_functionality()
1080
+ else:
1081
+ # Run test when module is imported
1082
+ try:
1083
+ test_dropdown_functionality()
1084
+ except Exception as e:
1085
+ print(f"Warning: Dropdown functionality test failed: {e}")
1086
+
1087
  # Create Gradio interface
1088
  def create_interface():
1089
  with gr.Blocks(