File size: 5,319 Bytes
298d6c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Hugging Face Spaces App for Textilindo AI Assistant
Main entry point that can run scripts and serve the application
"""

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

# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def run_script(script_name, *args):
    """Run a script from the scripts directory"""
    script_path = Path("scripts") / f"{script_name}.py"
    
    if not script_path.exists():
        return f"❌ Script not found: {script_path}"
    
    try:
        # Run the script
        cmd = [sys.executable, str(script_path)] + list(args)
        result = subprocess.run(cmd, capture_output=True, text=True, timeout=300)
        
        if result.returncode == 0:
            return f"βœ… Script executed successfully:\n{result.stdout}"
        else:
            return f"❌ Script failed:\n{result.stderr}"
            
    except subprocess.TimeoutExpired:
        return "❌ Script timed out after 5 minutes"
    except Exception as e:
        return f"❌ Error running script: {e}"

def check_training_ready():
    """Check if everything is ready for training"""
    return run_script("check_training_ready")

def create_sample_dataset():
    """Create a sample dataset"""
    return run_script("create_sample_dataset")

def test_novita_connection():
    """Test Novita AI connection"""
    return run_script("test_novita_connection")

def setup_textilindo_training():
    """Setup Textilindo training environment"""
    return run_script("setup_textilindo_training")

def train_textilindo_ai():
    """Train Textilindo AI model"""
    return run_script("train_textilindo_ai")

def test_textilindo_ai():
    """Test the trained Textilindo AI model"""
    return run_script("test_textilindo_ai")

def list_available_scripts():
    """List all available scripts"""
    scripts_dir = Path("scripts")
    if not scripts_dir.exists():
        return "❌ Scripts directory not found"
    
    scripts = []
    for script_file in scripts_dir.glob("*.py"):
        if script_file.name != "__init__.py":
            scripts.append(f"πŸ“„ {script_file.name}")
    
    if scripts:
        return "πŸ“‹ Available Scripts:\n" + "\n".join(scripts)
    else:
        return "❌ No scripts found"

def create_interface():
    """Create the Gradio interface"""
    
    with gr.Blocks(title="Textilindo AI Assistant - Script Runner") as interface:
        gr.Markdown("""
        # πŸ€– Textilindo AI Assistant - Script Runner
        
        This interface allows you to run various scripts for the Textilindo AI Assistant.
        """)
        
        with gr.Tab("Setup & Training"):
            gr.Markdown("### Setup and Training Scripts")
            
            with gr.Row():
                check_btn = gr.Button("πŸ” Check Training Ready", variant="secondary")
                setup_btn = gr.Button("βš™οΈ Setup Training", variant="primary")
                train_btn = gr.Button("πŸš€ Train Model", variant="primary")
            
            with gr.Row():
                dataset_btn = gr.Button("πŸ“Š Create Sample Dataset", variant="secondary")
                test_btn = gr.Button("πŸ§ͺ Test Model", variant="secondary")
        
        with gr.Tab("External Services"):
            gr.Markdown("### External Service Integration")
            
            with gr.Row():
                novita_btn = gr.Button("πŸ”— Test Novita AI Connection", variant="secondary")
        
        with gr.Tab("Scripts Info"):
            gr.Markdown("### Available Scripts")
            
            with gr.Row():
                list_btn = gr.Button("πŸ“‹ List All Scripts", variant="secondary")
        
        # Output area
        output = gr.Textbox(
            label="Output",
            lines=20,
            max_lines=30,
            show_copy_button=True
        )
        
        # Event handlers
        check_btn.click(
            check_training_ready,
            outputs=output
        )
        
        setup_btn.click(
            setup_textilindo_training,
            outputs=output
        )
        
        train_btn.click(
            train_textilindo_ai,
            outputs=output
        )
        
        dataset_btn.click(
            create_sample_dataset,
            outputs=output
        )
        
        test_btn.click(
            test_textilindo_ai,
            outputs=output
        )
        
        novita_btn.click(
            test_novita_connection,
            outputs=output
        )
        
        list_btn.click(
            list_available_scripts,
            outputs=output
        )
    
    return interface

def main():
    """Main function"""
    print("πŸš€ Starting Textilindo AI Assistant - Hugging Face Spaces")
    print("=" * 60)
    
    # Check if we're in the right directory
    if not Path("scripts").exists():
        print("❌ Scripts directory not found. Please ensure you're in the correct directory.")
        sys.exit(1)
    
    # Create and launch the interface
    interface = create_interface()
    
    # Launch the interface
    interface.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        debug=False
    )

if __name__ == "__main__":
    main()