Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# === Imports ===
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
import torch
|
| 6 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
# === GROQ API KEY (you must insert manually or via secrets) ===
|
| 10 |
+
GROQ_API_KEY = "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" # Replace in Colab manually
|
| 11 |
+
MODEL_NAME = "llama3-8b-8192"
|
| 12 |
+
|
| 13 |
+
# === BLIP Image Captioning Model ===
|
| 14 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 15 |
+
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 16 |
+
|
| 17 |
+
def caption_image(image_path):
|
| 18 |
+
raw_image = Image.open(image_path).convert('RGB')
|
| 19 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 20 |
+
out = blip_model.generate(**inputs)
|
| 21 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 22 |
+
return caption
|
| 23 |
+
|
| 24 |
+
# === Build Prompt ===
|
| 25 |
+
def build_prompt(description):
|
| 26 |
+
return f"""
|
| 27 |
+
You are a front-end developer AI assistant.
|
| 28 |
+
Write clean, responsive HTML, CSS, and JavaScript code for the following UI design:
|
| 29 |
+
|
| 30 |
+
\"\"\"{description}\"\"\"
|
| 31 |
+
|
| 32 |
+
Return a single complete HTML file including <style> and <script> blocks.
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
# === Query Groq API ===
|
| 36 |
+
def query_groq(prompt):
|
| 37 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 38 |
+
headers = {
|
| 39 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 40 |
+
"Content-Type": "application/json"
|
| 41 |
+
}
|
| 42 |
+
body = {
|
| 43 |
+
"model": MODEL_NAME,
|
| 44 |
+
"messages": [
|
| 45 |
+
{"role": "system", "content": "You generate front-end code."},
|
| 46 |
+
{"role": "user", "content": prompt}
|
| 47 |
+
],
|
| 48 |
+
"temperature": 0.4
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
response = requests.post(url, headers=headers, json=body)
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
result = response.json()
|
| 55 |
+
if "choices" in result:
|
| 56 |
+
return result["choices"][0]["message"]["content"]
|
| 57 |
+
else:
|
| 58 |
+
# Print full error for debug
|
| 59 |
+
return f"❌ API Error: {result.get('error', 'Unknown error')}\n\nFull response:\n{result}"
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"❌ Exception occurred: {str(e)}"
|
| 62 |
+
|
| 63 |
+
# === Save Code to File ===
|
| 64 |
+
def save_code(code):
|
| 65 |
+
path = "/tmp/generated_ui.html"
|
| 66 |
+
with open(path, "w") as f:
|
| 67 |
+
f.write(code)
|
| 68 |
+
return path
|
| 69 |
+
|
| 70 |
+
# === Main Inference Function ===
|
| 71 |
+
def generate_ui_code(input_type, text_description, image_file):
|
| 72 |
+
if text_description.strip():
|
| 73 |
+
final_description = text_description
|
| 74 |
+
prompt = build_prompt(final_description)
|
| 75 |
+
code = query_groq(prompt)
|
| 76 |
+
filepath = save_code(code)
|
| 77 |
+
return code, filepath
|
| 78 |
+
else:
|
| 79 |
+
return "Please provide a description.", None
|
| 80 |
+
|
| 81 |
+
with gr.Blocks(title="Frontend UI Code Generator with Groq") as demo:
|
| 82 |
+
gr.Markdown("## ✨ Generate Frontend Code from Design Description or Image")
|
| 83 |
+
|
| 84 |
+
input_type = gr.Radio(["Text Description", "Design Image"], label="Select Input Type", value="Text Description")
|
| 85 |
+
|
| 86 |
+
with gr.Row(visible=True) as row_text:
|
| 87 |
+
text_description = gr.Textbox(label="Enter UI Design Description", lines=4, placeholder="e.g. A signup form with 3 fields and a button")
|
| 88 |
+
|
| 89 |
+
with gr.Row(visible=False) as row_image:
|
| 90 |
+
image_file = gr.Image(type="filepath", label="Upload UI Design Image")
|
| 91 |
+
|
| 92 |
+
input_type.change(fn=lambda t: (t == "Text Description", t == "Design Image"),
|
| 93 |
+
inputs=input_type,
|
| 94 |
+
outputs=[row_text, row_image])
|
| 95 |
+
|
| 96 |
+
submit_btn = gr.Button("🚀 Generate Code")
|
| 97 |
+
code_output = gr.Code(label="🧠 Generated HTML Code", language="html")
|
| 98 |
+
download_btn = gr.File(label="⬇️ Download HTML File")
|
| 99 |
+
|
| 100 |
+
submit_btn.click(
|
| 101 |
+
fn=generate_ui_code,
|
| 102 |
+
inputs=[input_type, text_description, image_file],
|
| 103 |
+
outputs=[code_output, download_btn]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
demo.launch(debug=True)
|