File size: 4,757 Bytes
3eb00a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Example usage of the ProjectImporter module

This script demonstrates how to use the standalone project importer
to fetch code from various sources without Gradio.
"""

import sys
import os

# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from project_importer import ProjectImporter


def example_import_space():
    """Example: Import a HuggingFace Space"""
    print("=" * 80)
    print("Example 1: Importing a HuggingFace Space")
    print("=" * 80)
    
    importer = ProjectImporter()
    result = importer.import_space("akhaliq", "anycoder")
    
    print(f"Status: {result['status']}")
    print(f"Message: {result['message']}")
    print(f"Language: {result['language']}")
    print(f"Files: {len(result['metadata'].get('files', []))}")
    print(f"\nFirst 500 characters of code:\n{result['code'][:500]}...")
    print()


def example_import_model():
    """Example: Import a HuggingFace Model"""
    print("=" * 80)
    print("Example 2: Importing a HuggingFace Model")
    print("=" * 80)
    
    importer = ProjectImporter()
    result = importer.import_model("meta-llama/Llama-3.2-1B-Instruct")
    
    print(f"Status: {result['status']}")
    print(f"Message: {result['message']}")
    print(f"Language: {result['language']}")
    print(f"Pipeline Tag: {result['metadata'].get('pipeline_tag')}")
    print(f"\nCode:\n{result['code']}")
    print()


def example_import_github():
    """Example: Import a GitHub Repository"""
    print("=" * 80)
    print("Example 3: Importing from GitHub")
    print("=" * 80)
    
    importer = ProjectImporter()
    result = importer.import_github_repo("huggingface", "transformers")
    
    print(f"Status: {result['status']}")
    print(f"Message: {result['message']}")
    print(f"Language: {result['language']}")
    print(f"\nFirst 500 characters of code:\n{result['code'][:500]}...")
    print()


def example_import_from_url():
    """Example: Import from any URL"""
    print("=" * 80)
    print("Example 4: Import from URL (automatic detection)")
    print("=" * 80)
    
    importer = ProjectImporter()
    
    # Test different URL types
    urls = [
        "https://huggingface.co/spaces/akhaliq/anycoder",
        "https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct",
        "https://github.com/huggingface/diffusers"
    ]
    
    for url in urls:
        print(f"\nImporting: {url}")
        result = importer.import_from_url(url)
        print(f"  Status: {result['status']}")
        print(f"  Language: {result['language']}")
        print(f"  Message: {result['message']}")


def example_save_to_file():
    """Example: Save imported code to a file"""
    print("=" * 80)
    print("Example 5: Save imported code to file")
    print("=" * 80)
    
    importer = ProjectImporter()
    result = importer.import_model("stabilityai/stable-diffusion-3.5-large")
    
    if result['status'] == 'success':
        output_file = "imported_sd3.5_code.py"
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(result['code'])
        print(f"Code saved to: {output_file}")
    else:
        print(f"Failed to import: {result['message']}")
    print()


def example_with_metadata():
    """Example: Working with metadata"""
    print("=" * 80)
    print("Example 6: Working with metadata")
    print("=" * 80)
    
    importer = ProjectImporter()
    result = importer.import_model("Qwen/Qwen2.5-Coder-32B-Instruct")
    
    print(f"Status: {result['status']}")
    print(f"Message: {result['message']}")
    print(f"\nMetadata:")
    for key, value in result['metadata'].items():
        print(f"  {key}: {value}")
    
    # Check if there are alternatives
    if result['metadata'].get('has_alternatives'):
        print("\n✨ This model has multiple code options available!")
        print("  - Inference code (serverless)")
        print("  - Local code (transformers/diffusers)")
    print()


def main():
    """Run all examples"""
    print("\nπŸš€ ProjectImporter Examples\n")
    
    try:
        example_import_space()
    except Exception as e:
        print(f"❌ Space import failed: {e}\n")
    
    try:
        example_import_model()
    except Exception as e:
        print(f"❌ Model import failed: {e}\n")
    
    try:
        example_import_github()
    except Exception as e:
        print(f"❌ GitHub import failed: {e}\n")
    
    try:
        example_import_from_url()
    except Exception as e:
        print(f"❌ URL import failed: {e}\n")
    
    try:
        example_with_metadata()
    except Exception as e:
        print(f"❌ Metadata example failed: {e}\n")
    
    print("\nβœ… Examples completed!")


if __name__ == "__main__":
    main()