File size: 2,752 Bytes
753dbc7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
CC Site Maker Python Client

A simple Python script to generate HTML using the CC Site Maker API
Usage: python python_generator.py "Your prompt" output.html
"""

import sys
import os
import argparse
import requests

# Default configuration - modify as needed
CONFIG = {
    "api_url": os.environ.get("API_URL", "http://localhost:7860/api/external/ask-ai"),
    "api_key": os.environ.get("API_KEY", ""),
}

def generate_html(prompt, api_key):
    """Generate HTML from prompt using the API"""
    print("Generating HTML...")
    
    headers = {
        'Content-Type': 'application/json',
        'X-API-Key': api_key
    }
    
    data = {
        'prompt': prompt
    }
    
    # For streaming response
    response = requests.post(CONFIG["api_url"], json=data, headers=headers, stream=True)
    
    if response.status_code != 200:
        try:
            error_data = response.json()
            raise Exception(error_data.get('message', f'Failed with status: {response.status_code}'))
        except:
            response.raise_for_status()
    
    print("Receiving response: ", end="", flush=True)
    result = ''
    for chunk in response.iter_content(chunk_size=1024, decode_unicode=True):
        if chunk:
            result += chunk
            print(".", end="", flush=True)
    
    print("\nResponse received.")
    return result

def save_html_to_file(html, filename):
    """Save HTML to a file"""
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(html)
    print(f"HTML saved to: {os.path.abspath(filename)}")

def main():
    """Main function"""
    parser = argparse.ArgumentParser(description='Generate HTML using CC Site Maker API')
    parser.add_argument('prompt', nargs='?', help='Prompt for the AI')
    parser.add_argument('output', nargs='?', default='output.html', help='Output filename')
    parser.add_argument('--api-key', help='API Key for authentication')
    parser.add_argument('--api-url', help='API URL')
    
    args = parser.parse_args()
    
    # Update config with command line arguments
    if args.api_url:
        CONFIG["api_url"] = args.api_url
    
    if args.api_key:
        CONFIG["api_key"] = args.api_key
    
    prompt = args.prompt
    output_file = args.output
    
    # If no prompt provided, ask for it
    if not prompt:
        prompt = input("Enter your prompt: ")
    
    # If no API key, ask for it
    if not CONFIG["api_key"]:
        CONFIG["api_key"] = input("Enter your API key: ")
    
    try:
        html = generate_html(prompt, CONFIG["api_key"])
        save_html_to_file(html, output_file)
    except Exception as e:
        print(f"Error: {e}", file=sys.stderr)
        sys.exit(1)

if __name__ == "__main__":
    main()