prompt / app.py
SHAILJA1's picture
Create app.py
ac734ef verified
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Load pretrained model (replace with a fine-tuned UI generator if available)
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codet5-small")
model = AutoModelForSeq2SeqLM.from_pretrained("Salesforce/codet5-small")
# Function to convert text to UI code
def text_to_ui(description):
inputs = tokenizer(description, return_tensors="pt")
outputs = model.generate(**inputs, max_length=256)
ui_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Wrap in HTML body if not already
if "<html>" not in ui_code.lower():
ui_code = f"""
<html>
<head>
<style>
body {{ font-family: Arial, sans-serif; padding: 20px; }}
input, button {{ padding: 10px; margin: 5px; }}
</style>
</head>
<body>
{ui_code}
</body>
</html>
"""
return ui_code, ui_code # return for both code display and HTML preview
# Gradio interface
iface = gr.Interface(
fn=text_to_ui,
inputs=gr.Textbox(lines=3, placeholder="Describe the UI you want..."),
outputs=[
gr.Code(label="Generated UI Code", language="html"),
gr.HTML(label="Preview")
],
title="Text-to-UI Generator",
description="Enter a text description and generate HTML/CSS UI code with live preview."
)
# Launch the interface
iface.launch()