import gradio as gr from huggingface_hub import InferenceClient import os # --- Configuration --- # Use environment variable for token, fallback to default if not set # For Spaces, set the HF_TOKEN secret API_TOKEN = os.getenv("HF_TOKEN", None) MODEL = "HuggingFaceH4/zephyr-7b-beta" # --- Initialize Inference Client --- try: if API_TOKEN: print("Using HF Token.") client = InferenceClient(model=MODEL, token=API_TOKEN) else: print("HF Token not found. Running without token (may lead to rate limits).") client = InferenceClient(model=MODEL) except Exception as e: print(f"Error initializing Inference Client: {e}") # Optionally, raise the error or handle it gracefully raise gr.Error(f"Failed to initialize the AI model client. Please check model name and token. Error: {e}") # --- Core Code Generation Function --- def generate_code( prompt: str, backend_choice: str, file_structure: str, max_tokens: int, temperature: float, top_p: float, ): """ Generates website code based on user prompt and choices. Yields the code token by token for live updates. """ print(f"Received prompt: {prompt}") print(f"Backend choice: {backend_choice}") print(f"File structure: {file_structure}") print(f"Max tokens: {max_tokens}, Temp: {temperature}, Top-p: {top_p}") # --- System Message (Internal) --- # This guides the AI's behavior. It's not user-editable in the UI. system_message = ( "You are an expert frontend web developer AI. Your task is to generate HTML, CSS, and JavaScript code " "for a website based on the user's description. \n" "**Constraints:**\n" "- ALWAYS generate a complete `index.html` file.\n" "- ONLY output the raw code for the requested files.\n" "- Do NOT include any explanations, comments about the code, or introductory sentences.\n" "- Start the response *directly* with the code.\n" "- If 'Multiple Files' is selected, structure the output clearly:\n" " - Start the HTML section with a clear marker like ``\n" " - Start the CSS section with a clear marker like `/* style.css */`\n" " - Start the JavaScript section (if needed) with `// script.js`\n" " - Ensure each file's code follows its respective marker.\n" "- If 'Single File' is selected, embed CSS within `