AiCoderv2 commited on
Commit
1febf1f
·
verified ·
1 Parent(s): f1607ac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -39
app.py CHANGED
@@ -19,15 +19,14 @@ def load_model():
19
  return True
20
 
21
  try:
22
- # Try CodeLlama 7B (faster loading)
23
  print("Loading CodeLlama 7B model...")
24
  model_name = "codellama/CodeLlama-7b-hf"
25
  tokenizer = AutoTokenizer.from_pretrained(model_name)
26
  model = AutoModelForCausalLM.from_pretrained(
27
  model_name,
28
  torch_dtype=torch.float16,
29
- device_map="auto",
30
- low_cpu_mem_usage=True
31
  )
32
  model_loaded = True
33
  print("Model loaded successfully")
@@ -37,100 +36,97 @@ def load_model():
37
  return False
38
 
39
  def generate_code_fast(prompt):
40
- """Fast HTML code generation with max tokens"""
41
  # Load model if not loaded
42
  if not load_model():
43
- return "<!-- Model loading failed -->\n<h1>Model Error</h1>\n<p>Could not load AI model</p>"
44
 
45
  try:
46
- full_prompt = f"Create a complete HTML file with embedded CSS and JavaScript for: {prompt}. Return only valid HTML code."
47
- inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=512).to("cuda" if torch.cuda.is_available() else "cpu")
 
48
 
49
- # Fast generation with maximum tokens
50
  outputs = model.generate(
51
  **inputs,
52
- max_new_tokens=88000, # 88k tokens
53
  temperature=0.7,
54
- do_sample=True,
55
- top_p=0.9,
56
- repetition_penalty=1.2
57
  )
58
 
59
  # Decode result
60
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
61
  result = decoded[len(full_prompt):]
62
 
63
- # Clean up result
64
- if result.startswith('"') and result.endswith('"'):
65
- result = result[1:-1]
66
-
67
- # Ensure it's valid HTML
68
- if not result.strip().startswith('<!DOCTYPE html>'):
69
- result = f"<!DOCTYPE html>\n<html>\n<head>\n <title>{prompt or 'Generated Site'}</title>\n</head>\n<body>\n <h1>{prompt or 'Generated Content'}</h1>\n</body>\n</html>"
70
 
71
  return result
72
 
73
  except Exception as e:
74
- return f"<!-- Error: {str(e)} -->\n<h1>Generation Failed</h1>\n<p>Please try again</p>"
75
 
76
  def run_code(html_code):
77
  """Run the generated code in preview"""
78
  return html_code
79
 
80
  def improve_code(description, current_code):
81
- """Improve existing code"""
82
  # Load model if not loaded
83
  if not load_model():
84
- return "<!-- Model not loaded -->\n<h1>Model Error</h1>"
85
 
86
  try:
87
- prompt = f"Improve this HTML code based on: {description}\n\nCurrent code:\n{current_code}\n\nReturn only the improved HTML code."
88
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512).to("cuda" if torch.cuda.is_available() else "cpu")
89
- outputs = model.generate(**inputs, max_new_tokens=88000, temperature=0.7, do_sample=True)
 
90
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
91
  result = decoded[len(prompt):]
92
 
93
- # Clean up result
94
- if result.startswith('"') and result.endswith('"'):
95
- result = result[1:-1]
 
96
  return result
97
  except Exception as e:
98
- return f"<!-- Error: {str(e)} -->\n{current_code}"
99
 
100
  with gr.Blocks(theme=gr.themes.Soft()) as app:
101
  gr.Markdown("# AI Website Builder")
102
- gr.Markdown("Ultra-Fast Generation - 88K Token Limit")
103
 
104
  with gr.Tab("Builder"):
105
  with gr.Row():
106
  with gr.Column(scale=1):
107
  desc_input = gr.Textbox(
108
  label="Describe your website",
109
- placeholder="e.g., Portfolio with dark theme",
110
- lines=3
111
  )
112
- gen_btn = gr.Button("Generate Website", variant="primary")
113
- imp_btn = gr.Button("Improve Code")
114
- run_btn = gr.Button("Run Website", variant="secondary")
115
 
116
  with gr.Column(scale=2):
117
  code_editor = gr.Code(
118
  label="HTML Code Editor",
119
  language="html",
120
- lines=25
121
  )
122
 
123
  # Preview area (initially hidden)
124
  with gr.Row(visible=False) as preview_row:
125
  gr.Markdown("### Live Preview")
126
  with gr.Row(visible=False) as preview_content:
127
- preview = gr.HTML(label="Website Preview")
128
 
129
  # Back to editor button (initially hidden)
130
  with gr.Row(visible=False) as back_row:
131
- back_btn = gr.Button("Back to Editor", variant="secondary")
132
 
133
- # Event handling - ultra fast generation
134
  gen_btn.click(
135
  fn=generate_code_fast,
136
  inputs=desc_input,
 
19
  return True
20
 
21
  try:
22
+ # Try CodeLlama 7B
23
  print("Loading CodeLlama 7B model...")
24
  model_name = "codellama/CodeLlama-7b-hf"
25
  tokenizer = AutoTokenizer.from_pretrained(model_name)
26
  model = AutoModelForCausalLM.from_pretrained(
27
  model_name,
28
  torch_dtype=torch.float16,
29
+ device_map="auto"
 
30
  )
31
  model_loaded = True
32
  print("Model loaded successfully")
 
36
  return False
37
 
38
  def generate_code_fast(prompt):
39
+ """Instant HTML code generation"""
40
  # Load model if not loaded
41
  if not load_model():
42
+ return "<!DOCTYPE html>\n<html>\n<head>\n <title>Error</title>\n</head>\n<body>\n <h1>Model Loading Failed</h1>\n</body>\n</html>"
43
 
44
  try:
45
+ # Minimal prompt for instant generation
46
+ full_prompt = f"HTML for {prompt}:"
47
+ inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=128).to("cuda" if torch.cuda.is_available() else "cpu")
48
 
49
+ # Instant generation with minimal processing
50
  outputs = model.generate(
51
  **inputs,
52
+ max_new_tokens=500,
53
  temperature=0.7,
54
+ do_sample=True
 
 
55
  )
56
 
57
  # Decode result
58
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
59
  result = decoded[len(full_prompt):]
60
 
61
+ # Ensure valid HTML
62
+ if '<!DOCTYPE html>' not in result:
63
+ result = f"<!DOCTYPE html>\n<html>\n<head>\n <title>{prompt or 'Generated Site'}</title>\n</head>\n<body>\n <h1>{prompt or 'Content'}</h1>\n</body>\n</html>"
 
 
 
 
64
 
65
  return result
66
 
67
  except Exception as e:
68
+ return "<!DOCTYPE html>\n<html>\n<head>\n <title>Error</title>\n</head>\n<body>\n <h1>Generation Failed</h1>\n</body>\n</html>"
69
 
70
  def run_code(html_code):
71
  """Run the generated code in preview"""
72
  return html_code
73
 
74
  def improve_code(description, current_code):
75
+ """Improve existing code instantly"""
76
  # Load model if not loaded
77
  if not load_model():
78
+ return current_code
79
 
80
  try:
81
+ # Minimal prompt for instant improvement
82
+ prompt = f"Improve: {description}"
83
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=128).to("cuda" if torch.cuda.is_available() else "cpu")
84
+ outputs = model.generate(**inputs, max_new_tokens=400, temperature=0.7)
85
  decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
86
  result = decoded[len(prompt):]
87
 
88
+ # Ensure valid HTML
89
+ if '<!DOCTYPE html>' in result:
90
+ start = result.find('<!DOCTYPE html>')
91
+ return result[start:]
92
  return result
93
  except Exception as e:
94
+ return current_code
95
 
96
  with gr.Blocks(theme=gr.themes.Soft()) as app:
97
  gr.Markdown("# AI Website Builder")
98
+ gr.Markdown("Instant Code Generation")
99
 
100
  with gr.Tab("Builder"):
101
  with gr.Row():
102
  with gr.Column(scale=1):
103
  desc_input = gr.Textbox(
104
  label="Describe your website",
105
+ placeholder="e.g., Portfolio site",
106
+ lines=2
107
  )
108
+ gen_btn = gr.Button("Generate Instantly", variant="primary")
109
+ imp_btn = gr.Button("Quick Improve")
110
+ run_btn = gr.Button("Run Website")
111
 
112
  with gr.Column(scale=2):
113
  code_editor = gr.Code(
114
  label="HTML Code Editor",
115
  language="html",
116
+ lines=20
117
  )
118
 
119
  # Preview area (initially hidden)
120
  with gr.Row(visible=False) as preview_row:
121
  gr.Markdown("### Live Preview")
122
  with gr.Row(visible=False) as preview_content:
123
+ preview = gr.HTML()
124
 
125
  # Back to editor button (initially hidden)
126
  with gr.Row(visible=False) as back_row:
127
+ back_btn = gr.Button("Back to Editor")
128
 
129
+ # Event handling - instant generation
130
  gen_btn.click(
131
  fn=generate_code_fast,
132
  inputs=desc_input,