Download app.py from Ayan2010/Qoder32: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/spaces/Ayan2010/Qoder32/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ayan2010/Qoder32/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ayan2010/Qoder32/resolve/main/app.py
3.03 kB
| 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() |