File size: 3,639 Bytes
44b5c36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Setup and run script for the llama.cpp Hugging Face Space
"""

import subprocess
import sys
import os

def install_dependencies():
    """Install required dependencies"""
    print("πŸ“¦ Installing dependencies...")
    
    try:
        # Upgrade pip first
        subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"], check=True)
        
        # Install requirements
        subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"], check=True)
        
        print("βœ… Dependencies installed successfully!")
        return True
        
    except subprocess.CalledProcessError as e:
        print(f"❌ Error installing dependencies: {e}")
        return False

def test_installation():
    """Test if llama.cpp is properly installed"""
    print("πŸ§ͺ Testing llama.cpp installation...")
    
    try:
        # Test import
        subprocess.run([sys.executable, "-c", "from llama_cpp import Llama; print('βœ… llama-cpp-python imported successfully')"], check=True)
        
        # Test other dependencies
        test_imports = [
            "import gradio; print('βœ… Gradio imported')",
            "import huggingface_hub; print('βœ… Hugging Face Hub imported')",
            "from config import get_recommended_model; print('βœ… Config imported')"
        ]
        
        for test_import in test_imports:
            subprocess.run([sys.executable, "-c", test_import], check=True)
        
        print("βœ… All tests passed!")
        return True
        
    except subprocess.CalledProcessError as e:
        print(f"❌ Installation test failed: {e}")
        return False

def run_app():
    """Run the Gradio app"""
    print("πŸš€ Starting the Gradio app...")
    print("πŸ“ Note: The Osmosis model will be downloaded on first use")
    print("🌐 The app will be available at http://localhost:7860")
    print("⏹️  Press Ctrl+C to stop the app")
    
    try:
        subprocess.run([sys.executable, "app.py"], check=True)
    except KeyboardInterrupt:
        print("\nπŸ‘‹ App stopped by user")
    except subprocess.CalledProcessError as e:
        print(f"❌ Error running app: {e}")

def main():
    """Main setup function"""
    print("πŸ”§ llama.cpp Hugging Face Space Setup")
    print("=" * 50)
    
    # Check Python version
    if sys.version_info < (3, 8):
        print("❌ Python 3.8 or higher is required")
        sys.exit(1)
    
    print(f"βœ… Python version: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
    
    # Install dependencies
    if not install_dependencies():
        print("❌ Failed to install dependencies")
        sys.exit(1)
    
    # Test installation
    if not test_installation():
        print("❌ Installation test failed")
        sys.exit(1)
    
    print("\nπŸŽ‰ Setup completed successfully!")
    print("\nπŸ“‹ What's installed:")
    print("   - llama-cpp-python for efficient CPU inference")
    print("   - Gradio for the web interface")
    print("   - Hugging Face Hub for model downloading")
    print("   - Osmosis Structure 0.6B model (will download on first use)")
    
    # Ask if user wants to run the app
    print("\n❓ Would you like to run the app now? (y/n): ", end="")
    try:
        response = input().lower().strip()
        if response in ['y', 'yes']:
            run_app()
        else:
            print("πŸ‘ Setup complete! Run 'python app.py' when ready.")
    except (EOFError, KeyboardInterrupt):
        print("\nπŸ‘ Setup complete! Run 'python app.py' when ready.")

if __name__ == "__main__":
    main()