import gradio as gr from huggingface_hub import InferenceClient import os import re # For post-processing fallback # --- Configuration --- API_TOKEN = os.getenv("HF_TOKEN", None) MODEL = "HuggingFaceH4/zephyr-7b-beta" # Or choose another suitable model # --- Initialize Inference Client --- try: print(f"Attempting to initialize Inference Client for model: {MODEL}") if API_TOKEN: print("Using HF Token found in environment.") 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) print("Inference Client initialized successfully.") except Exception as e: print(f"Error initializing Inference Client: {e}") raise gr.Error(f"Failed to initialize the AI model client for '{MODEL}'. Check model name, network, and HF_TOKEN secret if applicable. 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. Aims for richer CSS and strictly outputs ONLY raw code. Yields the code token by token for live updates. """ print(f"--- Generating Code ---") print(f"Prompt: {prompt[:100]}...") print(f"Backend Context: {backend_choice}") print(f"File Structure: {file_structure}") print(f"Settings: Max Tokens={max_tokens}, Temp={temperature}, Top-P={top_p}") # --- Dynamically Build System Message Based on File Structure & Style Request --- if file_structure == "Single File": file_structure_instruction = ( "- **File Structure is 'Single File':** Generate ONLY a single, complete `index.html` file. " "Embed ALL CSS directly within `