Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Gradio app for mmBERT training on HuggingFace Spaces.
|
| 3 |
+
Runs training in background and shows status via web interface.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import subprocess
|
| 8 |
+
import threading
|
| 9 |
+
import time
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
# Training status
|
| 13 |
+
training_status = {
|
| 14 |
+
'stage': 'Starting...',
|
| 15 |
+
'progress': 0,
|
| 16 |
+
'logs': [],
|
| 17 |
+
'complete': False,
|
| 18 |
+
'error': None
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
def run_training():
|
| 22 |
+
"""Run training in background thread."""
|
| 23 |
+
global training_status
|
| 24 |
+
|
| 25 |
+
training_status['stage'] = 'Starting training...'
|
| 26 |
+
training_status['logs'].append('Launching train.py...')
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
# Run training script and capture output
|
| 30 |
+
process = subprocess.Popen(
|
| 31 |
+
['python', 'train.py'],
|
| 32 |
+
stdout=subprocess.PIPE,
|
| 33 |
+
stderr=subprocess.STDOUT,
|
| 34 |
+
text=True,
|
| 35 |
+
bufsize=1
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Read output line by line
|
| 39 |
+
for line in iter(process.stdout.readline, ''):
|
| 40 |
+
line = line.strip()
|
| 41 |
+
if line:
|
| 42 |
+
training_status['logs'].append(line)
|
| 43 |
+
# Keep last 100 lines
|
| 44 |
+
if len(training_status['logs']) > 100:
|
| 45 |
+
training_status['logs'] = training_status['logs'][-100:]
|
| 46 |
+
|
| 47 |
+
# Update stage based on output
|
| 48 |
+
if 'Loading data' in line:
|
| 49 |
+
training_status['stage'] = 'Loading data...'
|
| 50 |
+
elif 'Loading model' in line:
|
| 51 |
+
training_status['stage'] = 'Loading model...'
|
| 52 |
+
elif 'Starting training' in line:
|
| 53 |
+
training_status['stage'] = 'Training in progress...'
|
| 54 |
+
elif 'Evaluating' in line:
|
| 55 |
+
training_status['stage'] = 'Evaluating...'
|
| 56 |
+
elif 'Pushing to Hub' in line:
|
| 57 |
+
training_status['stage'] = 'Uploading model...'
|
| 58 |
+
elif 'Done!' in line:
|
| 59 |
+
training_status['stage'] = 'Complete!'
|
| 60 |
+
training_status['complete'] = True
|
| 61 |
+
elif 'loss' in line.lower():
|
| 62 |
+
training_status['stage'] = f'Training: {line[-80:]}'
|
| 63 |
+
|
| 64 |
+
process.wait()
|
| 65 |
+
|
| 66 |
+
if process.returncode == 0:
|
| 67 |
+
training_status['stage'] = '✅ Training complete! Model uploaded.'
|
| 68 |
+
training_status['complete'] = True
|
| 69 |
+
else:
|
| 70 |
+
training_status['stage'] = f'❌ Training failed with exit code {process.returncode}'
|
| 71 |
+
training_status['error'] = f'Exit code: {process.returncode}'
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
training_status['stage'] = f'❌ Error: {str(e)}'
|
| 75 |
+
training_status['error'] = str(e)
|
| 76 |
+
training_status['logs'].append(f'ERROR: {str(e)}')
|
| 77 |
+
|
| 78 |
+
def get_status():
|
| 79 |
+
"""Get current training status."""
|
| 80 |
+
logs = '\n'.join(training_status['logs'][-50:])
|
| 81 |
+
status = f"## Status: {training_status['stage']}\n\n### Recent Logs:\n```\n{logs}\n```"
|
| 82 |
+
return status
|
| 83 |
+
|
| 84 |
+
def refresh_status():
|
| 85 |
+
"""Refresh the status display."""
|
| 86 |
+
return get_status()
|
| 87 |
+
|
| 88 |
+
# Start training in background thread
|
| 89 |
+
training_thread = threading.Thread(target=run_training, daemon=True)
|
| 90 |
+
training_thread.start()
|
| 91 |
+
|
| 92 |
+
# Create Gradio interface
|
| 93 |
+
with gr.Blocks(title="FTM Zone Trainer") as demo:
|
| 94 |
+
gr.Markdown("# 🚀 FTM Email Zone Classifier Training")
|
| 95 |
+
gr.Markdown("Training mmBERT model on 38,809 email examples...")
|
| 96 |
+
|
| 97 |
+
status_display = gr.Markdown(value=get_status)
|
| 98 |
+
|
| 99 |
+
refresh_btn = gr.Button("🔄 Refresh Status")
|
| 100 |
+
refresh_btn.click(fn=refresh_status, outputs=status_display)
|
| 101 |
+
|
| 102 |
+
# Auto-refresh every 10 seconds
|
| 103 |
+
demo.load(fn=refresh_status, outputs=status_display, every=10)
|
| 104 |
+
|
| 105 |
+
if __name__ == '__main__':
|
| 106 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|