File size: 12,509 Bytes
aa6f369
 
 
8531a26
b647320
aa6f369
4d76afc
aa6f369
8941b06
aa6f369
4d76afc
aa6f369
4d76afc
8531a26
aa6f369
4d76afc
 
b647320
 
 
8941b06
 
b647320
 
8941b06
b647320
8941b06
b647320
 
8941b06
b647320
 
8941b06
b647320
 
 
 
8941b06
b647320
 
 
 
 
 
8941b06
b647320
 
 
8941b06
b647320
 
 
 
 
 
 
 
 
 
 
 
8941b06
b647320
8941b06
b647320
8941b06
 
4d76afc
 
 
 
 
 
 
b647320
4d76afc
 
b647320
4d76afc
b647320
aa6f369
b647320
 
 
 
 
 
 
 
 
 
 
 
 
aa6f369
4d76afc
8531a26
4d76afc
aa6f369
 
8531a26
aa6f369
4d76afc
8531a26
b647320
 
4d76afc
aa6f369
b647320
aa6f369
 
f5a64b7
aa6f369
 
 
 
b647320
 
aa6f369
 
 
8531a26
b647320
 
 
 
 
e8a0246
4d76afc
b647320
 
 
8941b06
aa6f369
 
8941b06
4d76afc
8941b06
b647320
8531a26
b647320
8531a26
4d76afc
8531a26
4d76afc
8531a26
8941b06
e8a0246
8941b06
4d76afc
8941b06
b647320
 
 
8941b06
b647320
 
8941b06
b647320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8a0246
aa6f369
8941b06
b647320
 
 
 
 
aa6f369
4d76afc
 
8531a26
 
aa6f369
4d76afc
b647320
aa6f369
4d76afc
aa6f369
 
 
 
b647320
aa6f369
 
 
8531a26
4d76afc
8531a26
8941b06
aa6f369
8531a26
4d76afc
aa6f369
b647320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3eee09
8531a26
aa6f369
8941b06
8531a26
aa6f369
4d76afc
8531a26
aa6f369
4d76afc
8531a26
e3eee09
b647320
aa6f369
4d76afc
8531a26
b647320
 
6541c57
f5a64b7
aa6f369
4d76afc
 
b647320
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import gradio as gr
from huggingface_hub import InferenceClient
import os
import re
# import traceback # Optional: for more detailed error logging if needed

# --- Configuration ---
API_TOKEN = os.getenv("HF_TOKEN", None)
MODEL = "Qwen/Qwen2.5-Coder-32B-Instruct"

# --- Initialize Inference Client ---
try:
    print(f"Initializing Inference Client for model: {MODEL}")
    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 for {MODEL}. Error: {e}. Check HF_TOKEN and model availability.")


# --- Helper Function to Parse Code into Files ---
def parse_code_into_files(raw_response: str) -> dict:
    """
    Parses raw AI output containing .TAB separators
    into a dictionary where keys are filenames and values are code blocks.
    Returns keys like 'index.html', 'backend_file', 'backend_filename', 'backend_language'.
    """
    files = {}
    # Default filename for the first block if no TAB is present or before the first TAB
    default_first_filename = "index.html"
    separator_pattern = r'\.TAB\[NAME=([^\]]+)\]\n?' # Capture filename

    # Find all separators and their positions
    matches = list(re.finditer(separator_pattern, raw_response))

    start_index = 0
    # Handle the first file (always assume index.html for now)
    first_separator_pos = matches[0].start() if matches else len(raw_response)
    first_block = raw_response[start_index:first_separator_pos].strip()
    if first_block:
         files[default_first_filename] = first_block

    # Handle the second file (if separator exists)
    if matches:
        backend_filename = matches[0].group(1).strip() # Get filename from first match
        start_index = matches[0].end() # Start after the first separator

        # Find the position of the *next* separator, or end of string
        second_separator_pos = matches[1].start() if len(matches) > 1 else len(raw_response)
        backend_code = raw_response[start_index:second_separator_pos].strip()

        if backend_code:
            files['backend_file'] = backend_code
            files['backend_filename'] = backend_filename
            # Determine language from filename extension
            if backend_filename.endswith(".py"):
                files['backend_language'] = 'python'
            elif backend_filename.endswith(".js"):
                files['backend_language'] = 'javascript'
            elif backend_filename.endswith(".css"):
                 files['backend_language'] = 'css'
            else:
                 files['backend_language'] = None # Default to plain text

    # If more files were generated (more separators), they are currently ignored by this simple parser.

    return files


# --- Core Code Generation Function ---
def generate_code(
    prompt: str,
    backend_choice: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    progress=gr.Progress(track_ τότε=True) # Add progress tracker
):
    print(f"Generating code for: {prompt[:100]}... | Backend: {backend_choice}")
    progress(0, desc="Initializing Request...")

    # System message remains the same - instructing the AI on format
    system_message = (
         "You are an AI that generates website code. You MUST ONLY output the raw code, without any conversational text like 'Here is the code' or explanations before or after the code blocks. "
         "You MUST NOT wrap the code in markdown fences like ```html, ```python, or ```js. "
         "If the user requests 'Static' or the prompt clearly implies only frontend code, generate ONLY the content for the `index.html` file. "
         "If the user requests 'Flask' or 'Node.js' and the prompt requires backend logic, you MUST generate both the `index.html` content AND the corresponding main backend file content (e.g., `app.py` for Flask, `server.js` or `app.js` for Node.js). "
         "When generating multiple files, you MUST separate them EXACTLY as follows: "
         "1. Output the complete code for the first file (e.g., `index.html`). "
         "2. On a new line immediately after the first file's code, add the separator '.TAB[NAME=filename.ext]' (e.g., '.TAB[NAME=app.py]' or '.TAB[NAME=server.js]'). "
         "3. On the next line, immediately start the code for the second file. "
         "Generate only the necessary files (usually index.html and potentially one backend file). "
         "The generated website code must be SFW and have minimal errors. "
         "Only include comments where user modification is strictly required. Avoid explanatory comments. "
         "If the user asks you to create code that is NOT for a website, you MUST respond ONLY with the exact phrase: "
         "'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 = ""
    token_count = 0
    est_total_tokens = max_tokens # Rough estimate for progress

    try:
        progress(0.1, desc="Sending Request to Model...")
        stream = client.chat_completion(
            messages=messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        )

        progress(0.2, desc="Receiving Stream...")
        for message in stream:
            token = message.choices[0].delta.content
            if isinstance(token, str):
                full_response += token
                token_count += 1
                # Update progress based on tokens received vs max_tokens
                # Adjust the scaling factor (e.g., 0.7) as needed
                prog = min(0.2 + (token_count / est_total_tokens) * 0.7, 0.9)
                progress(prog, desc="Generating Code...")


        progress(0.9, desc="Processing Response...")
        # --- Post-processing ---
        cleaned_response = full_response.strip()
        # Fallback fence removal
        cleaned_response = re.sub(r"^\s*```[a-z]*\s*\n?", "", cleaned_response)
        cleaned_response = re.sub(r"\n?\s*```\s*$", "", cleaned_response)
        # Remove potential chat markers
        cleaned_response = re.sub(r"<\s*\|?\s*(user|system|assistant)\s*\|?\s*>", "", cleaned_response, flags=re.IGNORECASE).strip()
        # Remove common conversational phrases (if they slip through)
        common_phrases = [ # Simplified list as prompt should handle most
            "Here is the code:", "Okay, here is the code:", "Here's the code:",
            "Sure, here is the code you requested:",
        ]
        temp_response_lower = cleaned_response.lower()
        for phrase in common_phrases:
            if temp_response_lower.startswith(phrase.lower()):
                cleaned_response = cleaned_response[len(phrase):].lstrip()
                temp_response_lower = cleaned_response.lower()

        # Check for refusal message
        refusal_message = "hey there! am here to create websites for you unfortunately am programmed to not create codes! otherwise I would go on the naughty list :-("
        if refusal_message in full_response:
             # Return updates to clear both code blocks and show refusal in the first
             progress(1, desc="Refusal Message Generated")
             return gr.update(value=refusal_message, language=None, visible=True), gr.update(value="", visible=False)

        # --- PARSE the final cleaned response into files ---
        parsed_files = parse_code_into_files(cleaned_response)

        html_code = parsed_files.get("index.html", "")
        backend_code = parsed_files.get("backend_file", "")
        backend_filename = parsed_files.get("backend_filename", "Backend")
        backend_language = parsed_files.get("backend_language", None)

        # --- Prepare Gradio Updates ---
        # Update for the HTML code block (always visible)
        html_update = gr.update(value=html_code, language='html', visible=True)

        # Update for the Backend code block (visible only if backend code exists)
        if backend_code:
            backend_update = gr.update(value=backend_code, language=backend_language, label=backend_filename, visible=True)
        else:
            backend_update = gr.update(value="", visible=False) # Hide if no backend code

        progress(1, desc="Done")
        # Return tuple of updates for the outputs list
        return html_update, backend_update

    except Exception as e:
        print(f"ERROR during code generation: {e}") # Log detailed error
        # traceback.print_exc() # Uncomment for full traceback
        progress(1, desc="Error Occurred")
        error_message = f"## Error\n\nFailed to generate or process code.\n**Reason:** {e}"
        # Return updates to show error in the first block and hide the second
        return gr.update(value=error_message, language=None, visible=True), gr.update(value="", visible=False)


# --- Build Gradio Interface ---
with gr.Blocks(css=".gradio-container { max-width: 90% !important; }") as demo:
    gr.Markdown("# ✨ Website Code Generator ✨")
    gr.Markdown(
        "Describe the website you want. The AI will generate the necessary code.\n"
        "If multiple files are generated (e.g., for Flask/Node.js), they will appear in separate tabs below." # Updated description
    )

    with gr.Row():
        with gr.Column(scale=2):
            prompt_input = gr.Textbox(
                label="Website Description",
                placeholder="e.g., A Flask app with a simple chat using Socket.IO",
                lines=6,
            )
            backend_radio = gr.Radio(
                ["Static", "Flask", "Node.js"],
                label="Backend Context",
                value="Static",
                info="Guides AI if backend code (like Python/JS) is needed alongside HTML."
            )
            generate_button = gr.Button("✨ Generate Website Code", variant="primary")

        with gr.Column(scale=3):
            # Define Tabs to hold the code outputs
            with gr.Tabs(elem_id="code-tabs") as code_tabs:
                # Tab 1: Always present for HTML
                with gr.Tab("index.html", elem_id="html-tab") as html_tab:
                     html_code_output = gr.Code(
                         label="index.html", # Label for the code block itself
                         language="html",
                         lines=25, # Adjusted lines slightly
                         interactive=False,
                         elem_id="html_code", # Unique ID for targeting
                     )
                # Tab 2: For Backend code, initially hidden
                with gr.Tab("Backend", elem_id="backend-tab", visible=False) as backend_tab:
                     backend_code_output = gr.Code(
                         label="Backend Code", # Label will be updated dynamically
                         language=None, # Language updated dynamically
                         lines=25,
                         interactive=False,
                         elem_id="backend_code", # Unique ID for targeting
                         visible=False # Component also starts hidden
                     )
                # Add more tabs here if needed (e.g., for CSS) following the same pattern


    with gr.Accordion("Advanced Settings", open=False):
        max_tokens_slider = gr.Slider(
            minimum=512, maximum=4096, value=3072, step=256, label="Max New Tokens"
        )
        temperature_slider = gr.Slider(
            minimum=0.1, maximum=1.2, value=0.7, step=0.1, label="Temperature"
        )
        top_p_slider = gr.Slider(
            minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-P"
        )

    # The click function now targets the specific code blocks within the tabs
    generate_button.click(
        fn=generate_code,
        inputs=[prompt_input, backend_radio, max_tokens_slider, temperature_slider, top_p_slider],
        # The outputs list MUST match the order and number of code blocks we want to update
        outputs=[html_code_output, backend_code_output],
    )

if __name__ == "__main__":
    if not API_TOKEN:
        print("Warning: HF_TOKEN environment variable not set. Using anonymous access.")
    demo.queue(max_size=10).launch() # Allow queueing