File size: 5,372 Bytes
51a6af5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0cbd64e
5d72536
0cbd64e
51a6af5
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import gradio as gr
import os
import subprocess
import sys
import threading
import time
from pathlib import Path

# Import your training code
from fine_tune_cove import train_cove_model, test_cove_inference

def install_requirements():
    """Install required packages"""
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
        return "βœ… Requirements installed successfully!"
    except Exception as e:
        return f"❌ Error installing requirements: {str(e)}"

def start_training(hf_token, model_name="codellama/CodeLlama-7b-Instruct-hf"):
    """Start the CoVe training process"""
    if not hf_token:
        return "❌ Please provide a HuggingFace token"
    
    # Set environment variables
    os.environ["HF_TOKEN"] = hf_token
    os.environ["SPACE_ID"] = "1"  # Indicates running in Spaces
    
    try:
        # Start training in a separate thread
        training_thread = threading.Thread(target=train_cove_model)
        training_thread.daemon = True
        training_thread.start()
        
        return "πŸš€ Training started! Check the logs below for progress."
    except Exception as e:
        return f"❌ Error starting training: {str(e)}"

def get_training_logs():
    """Get training logs"""
    log_file = "training.log"
    if os.path.exists(log_file):
        with open(log_file, 'r') as f:
            return f.read()
    return "No logs available yet. Training may not have started."

def test_model():
    """Test the trained model"""
    try:
        result = test_cove_inference()
        return result
    except Exception as e:
        return f"❌ Error testing model: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="CoVe Fine-tuning on HuggingFace Spaces") as demo:
    gr.Markdown("# πŸ”— Chain of Verification (CoVe) Fine-tuning")
    gr.Markdown("Fine-tune CodeLlama with Chain of Verification for better code reasoning")
    
    with gr.Tab("Setup & Training"):
        with gr.Row():
            with gr.Column():
                gr.Markdown("### 1. Setup")
                install_btn = gr.Button("Install Requirements", variant="secondary")
                install_output = gr.Textbox(label="Installation Status", lines=2)
                
                gr.Markdown("### 2. Training Configuration")
                hf_token = gr.Textbox(
                    label="HuggingFace Token (Write Access)", 
                    type="password",
                    placeholder="hf_xxxxxxxxxxxxxxxxxxxx"
                )
                model_name = gr.Textbox(
                    label="Base Model", 
                    value="codellama/CodeLlama-7b-Instruct-hf",
                    interactive=True
                )
                
                start_btn = gr.Button("Start Training", variant="primary")
                training_status = gr.Textbox(label="Training Status", lines=2)
            
            with gr.Column():
                gr.Markdown("### Training Progress")
                logs = gr.Textbox(
                    label="Training Logs", 
                    lines=15, 
                    max_lines=20,
                    value="Logs will appear here once training starts..."
                )
                refresh_logs_btn = gr.Button("Refresh Logs", variant="secondary")
    
    with gr.Tab("Model Testing"):
        gr.Markdown("### Test the Fine-tuned Model")
        test_btn = gr.Button("Test CoVe Model", variant="primary")
        test_output = gr.Textbox(label="Model Output", lines=10)
    
    with gr.Tab("Instructions"):
        gr.Markdown("""
        ## How to Use This Space
        
        ### Step 1: Get Your HuggingFace Token
        1. Go to [HuggingFace Settings](https://huggingface.co/settings/tokens)
        2. Create a new token with **Write** access
        3. Copy the token (starts with `hf_`)
        
        ### Step 2: Install Requirements
        - Click "Install Requirements" button
        - Wait for installation to complete
        
        ### Step 3: Start Training
        1. Paste your HuggingFace token
        2. Click "Start Training"
        3. Monitor progress in the logs
        
        ### Step 4: Test the Model
        - Once training is complete, test the model in the "Model Testing" tab
        
        ### Important Notes:
        - **GPU Required**: This requires a paid GPU Space (T4 minimum)
        - **Training Time**: Approximately 2-4 hours depending on GPU
        - **Storage**: The model will be saved to your HuggingFace account
        - **Monitoring**: Check logs regularly for progress updates
        
        ### Troubleshooting:
        - If training fails, check the logs for error messages
        - Ensure your HuggingFace token has write permissions
        - Make sure you have sufficient GPU memory
        """)
    
    # Event handlers
    install_btn.click(install_requirements, outputs=install_output)
    start_btn.click(start_training, inputs=[hf_token, model_name], outputs=training_status)
    refresh_logs_btn.click(get_training_logs, outputs=logs)
    test_btn.click(test_model, outputs=test_output)
    
    # Auto-refresh logs every 30 seconds during training
    # Instead of demo.load(..., every=30)
    gr.Timer(30, lambda: logs.update(value=get_training_logs()))


if __name__ == "__main__":
    demo.launch(share=False)