File size: 8,695 Bytes
22ac115
 
8d73c92
22ac115
8d73c92
 
22ac115
 
8d73c92
22ac115
 
 
8d73c92
 
22ac115
 
8d73c92
22ac115
8d73c92
 
 
22ac115
8d73c92
 
22ac115
 
8d73c92
 
22ac115
8d73c92
 
22ac115
8d73c92
 
22ac115
8d73c92
 
 
22ac115
8d73c92
 
 
22ac115
8d73c92
22ac115
8d73c92
 
 
 
22ac115
 
8d73c92
22ac115
8d73c92
 
22ac115
8d73c92
 
22ac115
8d73c92
 
 
22ac115
8d73c92
 
 
 
 
 
22ac115
 
8d73c92
22ac115
8d73c92
22ac115
 
8d73c92
22ac115
8d73c92
 
22ac115
8d73c92
 
 
22ac115
8d73c92
 
22ac115
8d73c92
22ac115
8d73c92
 
 
 
 
 
 
 
22ac115
8d73c92
 
 
 
 
 
22ac115
8d73c92
22ac115
8d73c92
22ac115
 
8d73c92
22ac115
8d73c92
 
 
 
22ac115
8d73c92
22ac115
8d73c92
22ac115
8d73c92
22ac115
8d73c92
22ac115
8d73c92
 
 
 
22ac115
8d73c92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22ac115
 
 
 
8d73c92
22ac115
8d73c92
 
 
 
 
 
22ac115
 
8d73c92
 
 
 
 
 
 
 
22ac115
8d73c92
 
 
22ac115
8d73c92
 
 
22ac115
8d73c92
22ac115
8d73c92
 
 
 
 
22ac115
8d73c92
22ac115
8d73c92
 
 
 
22ac115
8d73c92
 
 
 
 
 
 
22ac115
8d73c92
 
22ac115
 
8d73c92
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
"""
OpenLLM Training Space - Main Application

This is the main entry point for the Hugging Face Space.
It provides a web interface for running OpenLLM training with authentication.

Author: Louis Chua Bean Chong
License: GPLv3
"""

import os
import sys
import gradio as gr
from pathlib import Path

# Import our authentication and training modules
try:
    from space_auth_test import test_space_authentication
    from openllm_training_with_auth import OpenLLMTrainingManager
    MODULES_AVAILABLE = True
except ImportError as e:
    MODULES_AVAILABLE = False
    print(f"❌ Required modules not available: {e}")


def create_space_interface():
    """Create the Gradio interface for the Space."""
    
    def run_authentication_test():
        """Run the authentication test and return results."""
        try:
            if not MODULES_AVAILABLE:
                return "❌ Required modules not available. Please check deployment."
            
            # Capture output from authentication test
            import io
            import contextlib
            
            output = io.StringIO()
            with contextlib.redirect_stdout(output):
                success = test_space_authentication()
            
            result = output.getvalue()
            
            if success:
                return f"βœ… Authentication Test Results:\n\n{result}"
            else:
                return f"❌ Authentication Test Failed:\n\n{result}"
                
        except Exception as e:
            return f"❌ Error running authentication test: {e}"
    
    def run_training(model_size, training_steps):
        """Run the OpenLLM training with authentication."""
        try:
            if not MODULES_AVAILABLE:
                return "❌ Required modules not available. Please check deployment."
            
            # Capture output from training
            import io
            import contextlib
            
            output = io.StringIO()
            with contextlib.redirect_stdout(output):
                training_manager = OpenLLMTrainingManager()
                repo_id = training_manager.run_training(
                    model_size=model_size,
                    steps=int(training_steps)
                )
            
            result = output.getvalue()
            
            return f"βœ… Training Results:\n\n{result}\n\nπŸŽ‰ Model available at: https://huggingface.co/{repo_id}"
                
        except Exception as e:
            return f"❌ Error running training: {e}"
    
    def check_space_environment():
        """Check the Space environment and configuration."""
        try:
            # Check if we're in a Space
            space_vars = ["SPACE_ID", "SPACE_HOST", "SPACE_REPO_ID"]
            is_space = any(os.getenv(var) for var in space_vars)
            
            # Check HF_TOKEN
            hf_token = os.getenv("HF_TOKEN")
            
            result = "πŸ” Space Environment Check:\n\n"
            
            if is_space:
                result += "βœ… Running in Hugging Face Space environment\n"
                for var in space_vars:
                    value = os.getenv(var)
                    if value:
                        result += f"   - {var}: {value}\n"
            else:
                result += "ℹ️ Running in local environment\n"
            
            if hf_token:
                result += f"βœ… HF_TOKEN found: {hf_token[:8]}...{hf_token[-4:]}\n"
                result += "   - Source: GitHub secrets\n"
            else:
                result += "❌ HF_TOKEN not found\n"
                result += "   - Please set HF_TOKEN in GitHub repository secrets\n"
            
            result += f"\nπŸ“ Available modules: {'βœ…' if MODULES_AVAILABLE else '❌'}"
            
            return result
            
        except Exception as e:
            return f"❌ Error checking environment: {e}"
    
    # Create the Gradio interface
    with gr.Blocks(title="OpenLLM Training Space", theme=gr.themes.Soft()) as interface:
        gr.Markdown("""
        # πŸš€ OpenLLM Training Space
        
        Welcome to the OpenLLM Training Space! This Space provides a complete environment for training OpenLLM models with automatic Hugging Face authentication and model upload.
        
        ## πŸ” Authentication
        
        This Space uses GitHub secrets for secure authentication. The HF_TOKEN is automatically available from your GitHub repository secrets.
        
        ## πŸ“‹ Available Actions
        
        1. **Environment Check**: Verify Space configuration and authentication
        2. **Authentication Test**: Test Hugging Face authentication
        3. **Run Training**: Start OpenLLM training with automatic upload
        """)
        
        with gr.Tab("πŸ” Environment Check"):
            gr.Markdown("Check the Space environment and configuration.")
            env_check_btn = gr.Button("Check Environment", variant="primary")
            env_output = gr.Textbox(label="Environment Status", lines=10, interactive=False)
            env_check_btn.click(check_space_environment, outputs=env_output)
        
        with gr.Tab("πŸ” Authentication Test"):
            gr.Markdown("Test Hugging Face authentication using GitHub secrets.")
            auth_test_btn = gr.Button("Run Authentication Test", variant="primary")
            auth_output = gr.Textbox(label="Authentication Results", lines=15, interactive=False)
            auth_test_btn.click(run_authentication_test, outputs=auth_output)
        
        with gr.Tab("πŸš€ Run Training"):
            gr.Markdown("""
            Start OpenLLM training with automatic model upload.
            
            **Training Parameters:**
            - **Model Size**: Choose the model size (small, medium, large)
            - **Training Steps**: Number of training steps (default: 8000)
            
            **Expected Results:**
            - Training will complete successfully
            - Model will be uploaded to Hugging Face Hub
            - Repository will be created with proper model files
            """)
            
            with gr.Row():
                model_size = gr.Dropdown(
                    choices=["small", "medium", "large"],
                    value="small",
                    label="Model Size",
                    info="Choose the model size for training"
                )
                training_steps = gr.Number(
                    value=8000,
                    label="Training Steps",
                    info="Number of training steps",
                    minimum=1000,
                    maximum=50000
                )
            
            train_btn = gr.Button("Start Training", variant="primary", size="lg")
            train_output = gr.Textbox(label="Training Results", lines=20, interactive=False)
            
            train_btn.click(
                run_training,
                inputs=[model_size, training_steps],
                outputs=train_output
            )
        
        with gr.Tab("πŸ“š Documentation"):
            gr.Markdown("""
            ## πŸ“– Available Documentation
            
            - **HUGGINGFACE_SPACE_SETUP_GUIDE.md**: Complete setup guide
            - **SPACE_AUTHENTICATION_SUMMARY.md**: Authentication summary
            - **SPACE_READY_SUMMARY.md**: Deployment summary
            
            ## πŸ”§ Available Scripts
            
            - **space_auth_test.py**: Authentication verification
            - **openllm_training_with_auth.py**: Complete training script
            - **integrate_auth_into_training.py**: Integration guide
            - **setup_hf_space_auth.py**: Space authentication setup
            - **verify_space_auth.py**: Space verification script
            
            ## 🎯 Quick Start
            
            1. Check the environment to verify configuration
            2. Run authentication test to ensure GitHub secrets are working
            3. Start training with your desired parameters
            4. Monitor the training progress and model upload
            
            ## πŸ”’ Security
            
            - HF_TOKEN is securely stored in GitHub repository secrets
            - No hardcoded tokens in any scripts
            - Automatic cleanup of test repositories
            - Proper error handling and logging
            """)
    
    return interface


if __name__ == "__main__":
    # Create and launch the interface
    interface = create_space_interface()
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )