AiCoderv2 commited on
Commit
9d6a6b8
·
verified ·
1 Parent(s): 2712381

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -81
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
3
  import torch
 
4
  import warnings
5
 
6
  # Suppress warnings
@@ -11,42 +12,23 @@ model_loaded = False
11
  tokenizer = None
12
  model = None
13
 
14
- # Try ERNIE model first
15
  try:
16
- print("Loading ERNIE model...")
17
- model_name = "baidu/ERNIE-4.5-21B-A3B-Thinking"
18
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
19
- model = AutoModelForCausalLM.from_pretrained(
20
- model_name,
21
- torch_dtype=torch.bfloat16,
22
- device_map="auto",
23
- trust_remote_code=True
24
- )
25
  model_loaded = True
26
- print("ERNIE model loaded successfully")
27
  except Exception as e:
28
- print(f"ERNIE model failed: {e}")
29
-
30
- # Try FLAN-T5 model (seq2seq)
31
- try:
32
- print("Loading FLAN-T5 model...")
33
- model_name = "google/flan-t5-base"
34
- tokenizer = AutoTokenizer.from_pretrained(model_name)
35
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
36
- model_loaded = True
37
- print("FLAN-T5 model loaded successfully")
38
- except Exception as e2:
39
- print(f"FLAN-T5 model also failed: {e2}")
40
-
41
- # Final fallback - use a simple template system
42
- print("Using template-based generation")
43
- model_loaded = False
44
 
45
- def generate_code(prompt):
46
- """Generate HTML code"""
47
  if not model_loaded or not tokenizer or not model:
48
- # Template-based fallback
49
- return f"""<!DOCTYPE html>
50
  <html>
51
  <head>
52
  <title>{prompt or 'AI Generated Website'}</title>
@@ -65,39 +47,42 @@ def generate_code(prompt):
65
  </div>
66
  </body>
67
  </html>"""
 
 
 
 
 
 
68
 
69
  try:
70
- if "t5" in str(type(model)).lower():
71
- # FLAN-T5 handling (seq2seq)
72
- full_prompt = f"Create a complete HTML file with CSS and JavaScript for: {prompt}. Return only valid HTML code."
73
- inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512)
74
- outputs = model.generate(**inputs, max_new_tokens=800, temperature=0.7)
75
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
76
- return result
77
- else:
78
- # Causal LM handling (ERNIE)
79
- full_prompt = f"Create a complete single HTML file with embedded CSS and JavaScript for: {prompt}. Return only valid HTML code."
80
- inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512).to("cuda")
81
- outputs = model.generate(**inputs, max_new_tokens=1000, temperature=0.7, do_sample=True)
82
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
83
- result = result[len(full_prompt):]
84
- if '<!DOCTYPE html>' in result:
85
- start = result.find('<!DOCTYPE html>')
86
- return result[start:]
87
- return result
 
 
 
 
88
  except Exception as e:
89
- # Fallback template on error
90
- return f"""<!DOCTYPE html>
91
- <html>
92
- <head>
93
- <title>Error - {prompt or 'AI Generated Website'}</title>
94
- </head>
95
- <body>
96
- <h1>Generation Error</h1>
97
- <p>{str(e)}</p>
98
- <p>Using template instead...</p>
99
- </body>
100
- </html>"""
101
 
102
  def improve_code(description, current_code):
103
  """Improve existing code"""
@@ -105,29 +90,16 @@ def improve_code(description, current_code):
105
  return current_code
106
 
107
  try:
108
- if "t5" in str(type(model)).lower():
109
- # FLAN-T5 handling
110
- prompt = f"Improve this HTML code based on the request: {description}\n\nCurrent code:\n{current_code}"
111
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
112
- outputs = model.generate(**inputs, max_new_tokens=600, temperature=0.7)
113
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
114
- else:
115
- # ERNIE handling
116
- prompt = f"Improve this HTML code based on: {description}\n\nCurrent code:\n{current_code}\n\nReturn only the improved HTML code."
117
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to("cuda")
118
- outputs = model.generate(**inputs, max_new_tokens=800, temperature=0.7, do_sample=True)
119
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
120
- result = result[len(prompt):]
121
- if '<!DOCTYPE html>' in result:
122
- start = result.find('<!DOCTYPE html>')
123
- return result[start:]
124
- return result
125
  except Exception as e:
126
  return current_code
127
 
128
  with gr.Blocks(theme=gr.themes.Soft()) as app:
129
  gr.Markdown("# AI Website Builder")
130
- gr.Markdown("Powered by local AI models")
131
 
132
  with gr.Tab("Builder"):
133
  with gr.Row():
@@ -154,9 +126,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
154
  with gr.Row():
155
  preview = gr.HTML(label="Website Preview")
156
 
157
- # Event handling
158
  gen_btn.click(
159
- fn=generate_code,
160
  inputs=desc_input,
161
  outputs=code_editor
162
  )
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM
3
  import torch
4
+ import time
5
  import warnings
6
 
7
  # Suppress warnings
 
12
  tokenizer = None
13
  model = None
14
 
15
+ # Try FLAN-T5 model (more reliable)
16
  try:
17
+ print("Loading FLAN-T5 model...")
18
+ model_name = "google/flan-t5-base"
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
20
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
 
 
 
 
21
  model_loaded = True
22
+ print("FLAN-T5 model loaded successfully")
23
  except Exception as e:
24
+ print(f"FLAN-T5 model failed: {e}")
25
+ model_loaded = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ def stream_code(prompt):
28
+ """Stream HTML code generation token by token"""
29
  if not model_loaded or not tokenizer or not model:
30
+ # Template-based fallback with streaming effect
31
+ template = f"""<!DOCTYPE html>
32
  <html>
33
  <head>
34
  <title>{prompt or 'AI Generated Website'}</title>
 
47
  </div>
48
  </body>
49
  </html>"""
50
+
51
+ # Stream character by character
52
+ for i in range(len(template)):
53
+ time.sleep(0.01) # Fast streaming
54
+ yield template[:i+1]
55
+ return
56
 
57
  try:
58
+ full_prompt = f"Create a complete HTML file with CSS and JavaScript for: {prompt}. Return only valid HTML code."
59
+ inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512)
60
+
61
+ # Generate with streaming
62
+ outputs = model.generate(
63
+ **inputs,
64
+ max_new_tokens=88000,
65
+ temperature=0.7,
66
+ do_sample=True,
67
+ output_scores=True,
68
+ return_dict_in_generate=True
69
+ )
70
+
71
+ # Decode and stream token by token
72
+ generated_tokens = outputs.sequences[0]
73
+ decoded = tokenizer.decode(generated_tokens, skip_special_tokens=True)
74
+
75
+ # Stream character by character for smooth effect
76
+ for i in range(len(decoded)):
77
+ time.sleep(0.005) # Very fast streaming
78
+ yield decoded[:i+1]
79
+
80
  except Exception as e:
81
+ # Fallback streaming
82
+ error_template = f"<!-- Error occurred: {str(e)} -->\n<!DOCTYPE html>\n<html>\n<head>\n <title>Error</title>\n</head>\n<body>\n <h1>Generation Failed</h1>\n</body>\n</html>"
83
+ for i in range(len(error_template)):
84
+ time.sleep(0.01)
85
+ yield error_template[:i+1]
 
 
 
 
 
 
 
86
 
87
  def improve_code(description, current_code):
88
  """Improve existing code"""
 
90
  return current_code
91
 
92
  try:
93
+ prompt = f"Improve this HTML code based on the request: {description}\n\nCurrent code:\n{current_code}"
94
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
95
+ outputs = model.generate(**inputs, max_new_tokens=600, temperature=0.7)
96
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  except Exception as e:
98
  return current_code
99
 
100
  with gr.Blocks(theme=gr.themes.Soft()) as app:
101
  gr.Markdown("# AI Website Builder")
102
+ gr.Markdown("Powered by local AI models - Streaming Generation")
103
 
104
  with gr.Tab("Builder"):
105
  with gr.Row():
 
126
  with gr.Row():
127
  preview = gr.HTML(label="Website Preview")
128
 
129
+ # Event handling with streaming
130
  gen_btn.click(
131
+ fn=stream_code,
132
  inputs=desc_input,
133
  outputs=code_editor
134
  )