Spaces:
Build error
Build error
| #!/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() | |