Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
# Load model and tokenizer
|
| 5 |
+
model_name = "Salesforce/codet5-small"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Code generation function
|
| 10 |
+
def generate_code(prompt, history):
|
| 11 |
+
history = history or []
|
| 12 |
+
input_text = "\n".join(history + [prompt])
|
| 13 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=512)
|
| 14 |
+
outputs = model.generate(inputs, max_length=512)
|
| 15 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 16 |
+
|
| 17 |
+
# Extract HTML, CSS, JS
|
| 18 |
+
html = css = js = ""
|
| 19 |
+
if "<html>" in decoded:
|
| 20 |
+
html = "<html>" + decoded.split("<html>")[1].split("</html>")[0] + "</html>"
|
| 21 |
+
if "<style>" in decoded:
|
| 22 |
+
css = decoded.split("<style>")[1].split("</style>")[0]
|
| 23 |
+
if "<script>" in decoded:
|
| 24 |
+
js = decoded.split("<script>")[1].split("</script>")[0]
|
| 25 |
+
|
| 26 |
+
history += [prompt, decoded]
|
| 27 |
+
return html.strip(), css.strip(), js.strip(), history
|
| 28 |
+
|
| 29 |
+
# Function to clear chat
|
| 30 |
+
def clear_chat():
|
| 31 |
+
return "", "", "", []
|
| 32 |
+
|
| 33 |
+
# Build Gradio UI
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# Code Generator: HTML, CSS, JS")
|
| 36 |
+
with gr.Row():
|
| 37 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Describe what you want to build...")
|
| 38 |
+
generate_btn = gr.Button("Generate")
|
| 39 |
+
new_chat_btn = gr.Button("New Chat")
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
html_output = gr.Code(label="HTML")
|
| 43 |
+
css_output = gr.Code(label="CSS")
|
| 44 |
+
js_output = gr.Code(label="JavaScript")
|
| 45 |
+
|
| 46 |
+
chat_state = gr.State([])
|
| 47 |
+
|
| 48 |
+
generate_btn.click(fn=generate_code, inputs=[prompt, chat_state],
|
| 49 |
+
outputs=[html_output, css_output, js_output, chat_state])
|
| 50 |
+
new_chat_btn.click(fn=clear_chat,
|
| 51 |
+
outputs=[html_output, css_output, js_output, chat_state])
|
| 52 |
|
| 53 |
demo.launch()
|