import gradio as gr from huggingface_hub import InferenceClient import os import re API_TOKEN = os.getenv("HF_TOKEN", None) MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct" try: client = InferenceClient(model=MODEL, token=API_TOKEN) if API_TOKEN else InferenceClient(model=MODEL) except Exception as e: raise gr.Error(f"Failed to initialize model client. Error: {e}") def generate_code(prompt: str, backend_choice: str, max_tokens: int, temperature: float, top_p: float): system_message = ( "You are an AI assistant programmed to generate website codes only. " "You must not use triple backticks (```html, ```python, etc.). " "If multiple files are needed, separate them clearly using:\n" "TAB.NAME={filename}\n" "Only generate code. No explanations, no phrases like 'Here is the code'. " "If user asks non-website code, reply:\n" "'hey there! am here to create websites for you unfortunately am programmed to not create codes! otherwise I would go on the naughty list :-('." ) user_prompt = f"USER_PROMPT = {prompt}\nUSER_BACKEND = {backend_choice}" messages = [ {"role": "system", "content": system_message}, {"role": "user", "content": user_prompt} ] full_response = "" try: stream = client.chat_completion( messages=messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ) for message in stream: token = message.choices[0].delta.content if isinstance(token, str): full_response += token cleaned_response = full_response.strip() cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response) cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response) cleaned_response = re.sub(r"<\s*\|?\s*(user|assistant|system|endoftext)\s*\|?\s*>", "", cleaned_response, flags=re.IGNORECASE) cleaned_response = cleaned_response.replace("<|im_end|>", "").replace("<|im_start|>", "").strip() common_phrases = [ "Here is the code:", "Okay, here is the code:", "Here's the code:", "Sure, here is the code you requested:", "Let me know if you need anything else.", "Here is the website code you requested:", "Here are the files for your website:", "Okay, here are the files:" ] lower_response = cleaned_response.lower() for phrase in common_phrases: if lower_response.startswith(phrase.lower()): cleaned_response = cleaned_response[len(phrase):].lstrip() lower_response = cleaned_response.lower() if not cleaned_response: return "Error: Empty response from model after cleaning." return cleaned_response except Exception as e: return f"## Error\n\nFailed to generate code.\n**Reason:** {e}" def split_files(full_code_text): file_blocks = [] splits = re.split(r'(TAB\.NAME=\{.+?\})', full_code_text) initial_content = splits[0].strip() if len(splits) == 1: if initial_content: default_name = "index.html" if "def " in initial_content or "import " in initial_content: default_name = "app.py" elif "function " in initial_content or "const " in initial_content or "let " in initial_content: default_name = "script.js" elif "" in initial_content or "