Spaces:
Configuration error
Configuration error
Download examples/python_generator.py from carefullycraftedtree/cc_site_maker: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/spaces/carefullycraftedtree/cc_site_maker/resolve/main/examples/python_generator.py
- Command line
-
hf download hf://spaces/carefullycraftedtree/cc_site_maker/examples/python_generator.py
-
curl -L -o python_generator.py https://huggingface.co/spaces/carefullycraftedtree/cc_site_maker/resolve/main/examples/python_generator.py
2.75 kB
| #!/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() |