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 = """agentica-org/DeepCoder-14B-Preview""" # 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, emphasizes completeness, and strictly outputs ONLY raw code. Yields the code token by token for live updates. under development not fully completed! """ print(f"--- Generating Code ---") print(f"Prompt: {prompt[:100]}...") print(f"Backend Context: {backend_choice}") print(f"File Structure: {file_structure}") # Log the max_tokens value being used for this request print(f"Settings: Max Tokens={max_tokens}, Temp={temperature}, Top-P={top_p}") # --- Dynamically Build System Message --- 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 `