Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
3 |
|
4 |
MODEL_NAME = "google/flan-t5-base"
|
5 |
|
@@ -8,46 +9,49 @@ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
8 |
|
9 |
def generate_code(prompt, history):
|
10 |
history = history or []
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
outputs = model.generate(**inputs, max_new_tokens=512)
|
14 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
html =
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
js = decoded.split("<script>")[1].split("</script>")[0]
|
24 |
|
25 |
-
|
26 |
-
|
27 |
|
28 |
-
|
29 |
-
return html.strip(), css.strip(), js.strip(), history
|
30 |
-
|
31 |
-
def clear_chat():
|
32 |
return "", "", "", []
|
33 |
|
34 |
with gr.Blocks() as demo:
|
35 |
-
gr.
|
36 |
-
with gr.Row():
|
37 |
-
prompt = gr.Textbox(label="Prompt", placeholder="Describe your web component...")
|
38 |
-
generate_btn = gr.Button("Generate")
|
39 |
-
new_chat_btn = gr.Button("New Chat")
|
40 |
|
41 |
with gr.Row():
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
new_chat_btn.click(fn=clear_chat,
|
51 |
-
outputs=[html_output, css_output, js_output, chat_state])
|
52 |
|
53 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import json
|
4 |
|
5 |
MODEL_NAME = "google/flan-t5-base"
|
6 |
|
|
|
9 |
|
10 |
def generate_code(prompt, history):
|
11 |
history = history or []
|
12 |
+
system_instruction = (
|
13 |
+
"You are a coding assistant. "
|
14 |
+
"Respond only in valid JSON format like this:\n"
|
15 |
+
"{\n"
|
16 |
+
" \"filename\": \"index.html\",\n"
|
17 |
+
" \"html\": \"...HTML code...\",\n"
|
18 |
+
" \"css\": \"...CSS code...\",\n"
|
19 |
+
" \"js\": \"...JavaScript code...\"\n"
|
20 |
+
"}\n"
|
21 |
+
)
|
22 |
+
full_prompt = system_instruction + "\nPrompt: " + prompt
|
23 |
+
inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512)
|
24 |
outputs = model.generate(**inputs, max_new_tokens=512)
|
25 |
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
26 |
|
27 |
+
try:
|
28 |
+
parsed = json.loads(decoded)
|
29 |
+
html = parsed.get("html", "")
|
30 |
+
css = parsed.get("css", "")
|
31 |
+
js = parsed.get("js", "")
|
32 |
+
except Exception:
|
33 |
+
html, css, js = "", "", ""
|
|
|
34 |
|
35 |
+
history.append((prompt, decoded))
|
36 |
+
return html, css, js, history
|
37 |
|
38 |
+
def clear_history():
|
|
|
|
|
|
|
39 |
return "", "", "", []
|
40 |
|
41 |
with gr.Blocks() as demo:
|
42 |
+
chat_history = gr.State([])
|
|
|
|
|
|
|
|
|
43 |
|
44 |
with gr.Row():
|
45 |
+
inp = gr.Textbox(label="Prompt", lines=2)
|
46 |
+
send_btn = gr.Button("Generate")
|
47 |
+
clear_btn = gr.Button("New Chat")
|
48 |
|
49 |
+
with gr.Row():
|
50 |
+
html_out = gr.Code(label="HTML", language="html")
|
51 |
+
css_out = gr.Code(label="CSS", language="css")
|
52 |
+
js_out = gr.Code(label="JavaScript", language="javascript")
|
53 |
|
54 |
+
send_btn.click(generate_code, [inp, chat_history], [html_out, css_out, js_out, chat_history])
|
55 |
+
clear_btn.click(clear_history, outputs=[html_out, css_out, js_out])
|
|
|
|
|
56 |
|
57 |
demo.launch()
|