prakhardoneria commited on
Commit
2ea1871
·
verified ·
1 Parent(s): 227c037

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
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
- input_text = f"Generate HTML, CSS, and JavaScript code for: {prompt}"
12
- inputs = tokenizer(input_text, return_tensors="pt", truncation=True, max_length=512)
 
 
 
 
 
 
 
 
 
 
13
  outputs = model.generate(**inputs, max_new_tokens=512)
14
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
15
 
16
- html = css = js = ""
17
- if "<html>" in decoded and "</html>" in decoded:
18
- html = decoded.split("<html>")[1].split("</html>")[0]
19
- html = f"<html>{html}</html>"
20
- if "<style>" in decoded and "</style>" in decoded:
21
- css = decoded.split("<style>")[1].split("</style>")[0]
22
- if "<script>" in decoded and "</script>" in decoded:
23
- js = decoded.split("<script>")[1].split("</script>")[0]
24
 
25
- if not (html or css or js):
26
- html = decoded
27
 
28
- history += [prompt, decoded]
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.Markdown(f"# Code Generator using {MODEL_NAME}")
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
- 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()
 
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()