CorneliusWang commited on
Commit
61fe8fc
Β·
verified Β·
1 Parent(s): c99cd73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -11
app.py CHANGED
@@ -7,7 +7,7 @@ import sys
7
  import os
8
  import subprocess
9
  from pathlib import Path
10
- import gradio as gr # Import directly, no patch needed
11
 
12
  # Add project paths
13
  sys.path.insert(0, str(Path(__file__).parent))
@@ -20,16 +20,21 @@ def run_comparison(iterations: int, seed: int, use_deterministic: bool, device:
20
  """
21
 
22
  # Set device environment variable for subprocess
 
23
  if device == "cuda":
24
  try:
25
  import torch
 
26
  if torch.cuda.is_available():
27
  try:
 
28
  gpu_name = torch.cuda.get_device_name(0)
29
  gpu_count = torch.cuda.device_count()
30
  print(f"βœ… GPU available: {gpu_name} (Count: {gpu_count})")
 
31
  except Exception as e:
32
  print(f"⚠️ GPU detection failed: {e}")
 
33
  else:
34
  print("⚠️ CUDA not available, falling back to CPU")
35
  device = "cpu"
@@ -40,7 +45,7 @@ def run_comparison(iterations: int, seed: int, use_deterministic: bool, device:
40
  print(f"⚠️ GPU check error: {e}, falling back to CPU")
41
  device = "cpu"
42
 
43
- # Set environment variable for subprocess
44
  os.environ["CUDA_DEVICE"] = device
45
  print(f"πŸ”§ Using device: {device}")
46
 
@@ -57,13 +62,14 @@ def run_comparison(iterations: int, seed: int, use_deterministic: bool, device:
57
  cmd.extend(["--seed", str(int(seed))])
58
 
59
  try:
 
60
  env = os.environ.copy()
61
  env["CUDA_DEVICE"] = os.environ.get("CUDA_DEVICE", device)
62
 
63
  result = subprocess.run(
64
  cmd,
65
  cwd=str(Path(__file__).parent),
66
- env=env,
67
  capture_output=True,
68
  text=True,
69
  timeout=3600 # 1 hour timeout
@@ -72,12 +78,13 @@ def run_comparison(iterations: int, seed: int, use_deterministic: bool, device:
72
  stdout_text = result.stdout
73
  stderr_text = result.stderr
74
 
 
75
  full_output = f"=== STDOUT ===\n{stdout_text}\n\n=== STDERR ===\n{stderr_text}"
76
 
77
  if result.returncode != 0:
78
  return f"❌ Error occurred:\n{full_output}", None
79
 
80
- # Check multiple possible locations for the plot
81
  plot_paths = [
82
  Path(__file__).parent / "teacher_agent_dev" / "comparison_all_strategies.png",
83
  Path(__file__).parent / "comparison_all_strategies.png",
@@ -93,6 +100,7 @@ def run_comparison(iterations: int, seed: int, use_deterministic: bool, device:
93
  if plot_path:
94
  return f"βœ… Comparison complete!\n\n{stdout_text}", str(plot_path)
95
  else:
 
96
  error_msg = f"⚠️ Plot not found at expected locations.\n"
97
  error_msg += f"Checked: {[str(p) for p in plot_paths]}\n\n"
98
  error_msg += f"Output:\n{full_output}"
@@ -109,6 +117,8 @@ def check_gpu():
109
  """Check if GPU is available on Hugging Face Spaces."""
110
  try:
111
  import torch
 
 
112
  if torch.cuda.is_available():
113
  try:
114
  gpu_name = torch.cuda.get_device_name(0)
@@ -116,9 +126,12 @@ def check_gpu():
116
  cuda_version = torch.version.cuda
117
  return f"βœ… GPU Available: {gpu_name} (Count: {gpu_count}, CUDA: {cuda_version})"
118
  except Exception as e:
 
119
  return f"βœ… GPU Detected (accessing: {str(e)[:50]}...)"
120
  else:
 
121
  if os.getenv("SPACE_ID"):
 
122
  hf_hardware = os.getenv("SPACE_HARDWARE", "unknown")
123
  if "gpu" in hf_hardware.lower() or "t4" in hf_hardware.lower() or "l4" in hf_hardware.lower():
124
  return f"⚠️ GPU Hardware ({hf_hardware}) allocated but not accessible yet. Try running anyway."
@@ -141,9 +154,12 @@ with gr.Blocks(title="MentorFlow - Strategy Comparison") as demo:
141
  3. **Teacher Strategy**: RL teacher agent learns optimal curriculum
142
 
143
  ## Usage
 
144
  1. Set parameters below
145
  2. Click "Run Comparison" to start training
146
  3. View results and generated plots
 
 
147
  """)
148
 
149
  # GPU Status
@@ -156,10 +172,33 @@ with gr.Blocks(title="MentorFlow - Strategy Comparison") as demo:
156
  # Parameters
157
  with gr.Row():
158
  with gr.Column():
159
- iterations = gr.Slider(minimum=50, maximum=500, value=100, step=50, label="Iterations")
160
- seed = gr.Number(value=42, label="Random Seed")
161
- use_deterministic = gr.Checkbox(value=True, label="Deterministic Mode")
162
- device = gr.Radio(choices=["cuda", "cpu"], value="cuda", label="Device")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  with gr.Column():
165
  run_btn = gr.Button("πŸš€ Run Comparison", variant="primary", size="lg")
@@ -167,9 +206,19 @@ with gr.Blocks(title="MentorFlow - Strategy Comparison") as demo:
167
  # Output
168
  with gr.Row():
169
  with gr.Column(scale=1):
170
- output_text = gr.Textbox(label="Output", lines=15, max_lines=30, interactive=False)
 
 
 
 
 
 
171
  with gr.Column(scale=1):
172
- output_plot = gr.Image(label="Comparison Plot", type="filepath", height=500)
 
 
 
 
173
 
174
  # Run comparison
175
  run_btn.click(
@@ -178,7 +227,19 @@ with gr.Blocks(title="MentorFlow - Strategy Comparison") as demo:
178
  outputs=[output_text, output_plot],
179
  api_name="run_comparison"
180
  )
 
 
 
 
 
 
 
 
 
 
 
 
181
 
182
  if __name__ == "__main__":
183
- # Standard HF Spaces configuration
184
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
7
  import os
8
  import subprocess
9
  from pathlib import Path
10
+ import gradio as gr # Import directly, do not use the patch
11
 
12
  # Add project paths
13
  sys.path.insert(0, str(Path(__file__).parent))
 
20
  """
21
 
22
  # Set device environment variable for subprocess
23
+ # On Hugging Face Spaces with GPU, try to use CUDA
24
  if device == "cuda":
25
  try:
26
  import torch
27
+ # Check if CUDA is available
28
  if torch.cuda.is_available():
29
  try:
30
+ # Try to get device name to verify GPU works
31
  gpu_name = torch.cuda.get_device_name(0)
32
  gpu_count = torch.cuda.device_count()
33
  print(f"βœ… GPU available: {gpu_name} (Count: {gpu_count})")
34
+ # Keep device as "cuda"
35
  except Exception as e:
36
  print(f"⚠️ GPU detection failed: {e}")
37
+ print(" Attempting to use CUDA anyway (may work)...")
38
  else:
39
  print("⚠️ CUDA not available, falling back to CPU")
40
  device = "cpu"
 
45
  print(f"⚠️ GPU check error: {e}, falling back to CPU")
46
  device = "cpu"
47
 
48
+ # Set environment variable for subprocess to pick up
49
  os.environ["CUDA_DEVICE"] = device
50
  print(f"πŸ”§ Using device: {device}")
51
 
 
62
  cmd.extend(["--seed", str(int(seed))])
63
 
64
  try:
65
+ # Ensure environment variables are passed to subprocess
66
  env = os.environ.copy()
67
  env["CUDA_DEVICE"] = os.environ.get("CUDA_DEVICE", device)
68
 
69
  result = subprocess.run(
70
  cmd,
71
  cwd=str(Path(__file__).parent),
72
+ env=env, # Pass environment variables
73
  capture_output=True,
74
  text=True,
75
  timeout=3600 # 1 hour timeout
 
78
  stdout_text = result.stdout
79
  stderr_text = result.stderr
80
 
81
+ # Combine outputs
82
  full_output = f"=== STDOUT ===\n{stdout_text}\n\n=== STDERR ===\n{stderr_text}"
83
 
84
  if result.returncode != 0:
85
  return f"❌ Error occurred:\n{full_output}", None
86
 
87
+ # Find output plot (check multiple possible locations)
88
  plot_paths = [
89
  Path(__file__).parent / "teacher_agent_dev" / "comparison_all_strategies.png",
90
  Path(__file__).parent / "comparison_all_strategies.png",
 
100
  if plot_path:
101
  return f"βœ… Comparison complete!\n\n{stdout_text}", str(plot_path)
102
  else:
103
+ # Return output even if plot not found (might still be useful)
104
  error_msg = f"⚠️ Plot not found at expected locations.\n"
105
  error_msg += f"Checked: {[str(p) for p in plot_paths]}\n\n"
106
  error_msg += f"Output:\n{full_output}"
 
117
  """Check if GPU is available on Hugging Face Spaces."""
118
  try:
119
  import torch
120
+
121
+ # Check CUDA availability
122
  if torch.cuda.is_available():
123
  try:
124
  gpu_name = torch.cuda.get_device_name(0)
 
126
  cuda_version = torch.version.cuda
127
  return f"βœ… GPU Available: {gpu_name} (Count: {gpu_count}, CUDA: {cuda_version})"
128
  except Exception as e:
129
+ # GPU might be available but not immediately accessible
130
  return f"βœ… GPU Detected (accessing: {str(e)[:50]}...)"
131
  else:
132
+ # On Hugging Face Spaces, check environment
133
  if os.getenv("SPACE_ID"):
134
+ # Check if GPU hardware is allocated
135
  hf_hardware = os.getenv("SPACE_HARDWARE", "unknown")
136
  if "gpu" in hf_hardware.lower() or "t4" in hf_hardware.lower() or "l4" in hf_hardware.lower():
137
  return f"⚠️ GPU Hardware ({hf_hardware}) allocated but not accessible yet. Try running anyway."
 
154
  3. **Teacher Strategy**: RL teacher agent learns optimal curriculum
155
 
156
  ## Usage
157
+
158
  1. Set parameters below
159
  2. Click "Run Comparison" to start training
160
  3. View results and generated plots
161
+
162
+ **Note**: With LM Student, this will take 15-30 minutes for 500 iterations.
163
  """)
164
 
165
  # GPU Status
 
172
  # Parameters
173
  with gr.Row():
174
  with gr.Column():
175
+ iterations = gr.Slider(
176
+ minimum=50,
177
+ maximum=500,
178
+ value=100,
179
+ step=50,
180
+ label="Iterations",
181
+ info="Number of training iterations (higher = longer runtime)"
182
+ )
183
+
184
+ seed = gr.Number(
185
+ value=42,
186
+ label="Random Seed",
187
+ info="Seed for reproducibility (ignored if deterministic)"
188
+ )
189
+
190
+ use_deterministic = gr.Checkbox(
191
+ value=True,
192
+ label="Deterministic Mode",
193
+ info="Use fixed seed=42 for reproducible results"
194
+ )
195
+
196
+ device = gr.Radio(
197
+ choices=["cuda", "cpu"],
198
+ value="cuda", # Default to GPU for HF Spaces with Nvidia 4xL4
199
+ label="Device",
200
+ info="GPU (cuda) recommended for Nvidia 4xL4, CPU fallback available"
201
+ )
202
 
203
  with gr.Column():
204
  run_btn = gr.Button("πŸš€ Run Comparison", variant="primary", size="lg")
 
206
  # Output
207
  with gr.Row():
208
  with gr.Column(scale=1):
209
+ output_text = gr.Textbox(
210
+ label="Output",
211
+ lines=15,
212
+ max_lines=30,
213
+ interactive=False
214
+ )
215
+
216
  with gr.Column(scale=1):
217
+ output_plot = gr.Image(
218
+ label="Comparison Plot",
219
+ type="filepath",
220
+ height=500
221
+ )
222
 
223
  # Run comparison
224
  run_btn.click(
 
227
  outputs=[output_text, output_plot],
228
  api_name="run_comparison"
229
  )
230
+
231
+ gr.Markdown("""
232
+ ## πŸ“Š Understanding Results
233
+
234
+ The comparison plot shows:
235
+ - **Learning Curves**: How each strategy improves over time
236
+ - **Difficult Question Performance**: Accuracy on hard questions
237
+ - **Curriculum Diversity**: Topic coverage over time
238
+ - **Learning Efficiency**: Iterations to reach target vs final performance
239
+
240
+ The **Teacher Strategy** should ideally outperform Random and Progressive strategies.
241
+ """)
242
 
243
  if __name__ == "__main__":
244
+ # Ensure the app binds to all interfaces for HF Spaces
245
  demo.launch(server_name="0.0.0.0", server_port=7860)