File size: 3,026 Bytes
567b3f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient
import os

# Free model - Code ke liye best
MODEL_ID = "Qwen/Qwen2.5-Coder-7B-Instruct"
client = InferenceClient(model=MODEL_ID, token=os.getenv("HF_TOKEN"))

def generate_code(prompt, language):
    if not prompt.strip():
        return "Prompt likho bhai!", "<h3>Yahan preview ayega</h3>"

    system_prompt = f"""
    You are an expert {language} developer.
    User prompt: {prompt}
    Rules:
    1. Only give clean, working {language} code.
    2. No extra explanation, only code inside ```{language.lower()} block.
    3. Code should be modern, responsive, beautiful UI.
    4. If HTML, include CSS inside <style> and JS inside <script> in single file.
    """

    try:
        response = client.chat_completion(
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": prompt}
            ],
            max_tokens=3000,
            temperature=0.2
        )
        code = response.choices[0].message.content

        # Clean code from markdown
        if "```" in code:
            code = code.split("```")[1]
            if code.startswith(language.lower()) or code.startswith("html"):
                code = "\n".join(code.split("\n")[1:])
            code = code.replace("```", "").strip()

        # Preview
        if language == "HTML":
            preview = code
        else:
            preview = f"<pre style='background:#0f0f0f;color:#00ff88;padding:15px;border-radius:10px;overflow:auto;'>{code}</pre>"

        return code, preview

    except Exception as e:
        return f"Error: {str(e)}\nHF_TOKEN add karo Space secrets me.", "<h3>Error - Token missing</h3>"

# UI
with gr.Blocks(theme=gr.themes.Monochrome(), title="Prompt2Code") as app:
    gr.Markdown("# ⚡ Prompt2Code - Prompt likho, Code pao")
    gr.Markdown("Videomen jaisa hi, par video ki jagah Website ka code")

    with gr.Row():
        with gr.Column():
            prompt_box = gr.Textbox(
                label="Tumhara Prompt",
                placeholder="Ex: ek glassmorphism login page banao, dark theme me...",
                lines=4
            )
            lang = gr.Dropdown(
                choices=["HTML", "React", "Python", "JavaScript", "CSS"],
                value="HTML",
                label="Language"
            )
            btn = gr.Button("🚀 Code Banao", variant="primary")
            code_output = gr.Code(label="Generated Code", language="html")

        with gr.Column():
            gr.Markdown("### 👁️ Live Preview")
            preview_output = gr.HTML(label="Preview")

    btn.click(fn=generate_code, inputs=[prompt_box, lang], outputs=[code_output, preview_output])

    gr.Examples(
        examples=[
            ["ek to-do list banao local storage ke saath", "HTML"],
            ["instagram jaisa login page", "HTML"],
            ["ek calculator banao neon design me", "React"],
        ],
        inputs=[prompt_box, lang]
    )

app.launch()