Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ import torch
|
|
| 4 |
import warnings
|
| 5 |
|
| 6 |
# Suppress warnings
|
| 7 |
-
warnings.filterwarnings("ignore"
|
| 8 |
-
warnings.filterwarnings("ignore", category=UserWarning, module="transformers")
|
| 9 |
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
# Download and load the model
|
| 12 |
model_name = "baidu/ERNIE-4.5-21B-A3B-Thinking"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -18,94 +18,84 @@ try:
|
|
| 18 |
trust_remote_code=True
|
| 19 |
)
|
| 20 |
model_loaded = True
|
|
|
|
| 21 |
except Exception as e:
|
| 22 |
print(f"Model loading failed: {e}")
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def generate_code(prompt):
|
| 26 |
-
"""Generate HTML code
|
| 27 |
if not model_loaded:
|
| 28 |
-
return "<!-- Model not available -->\n<h1>Model Loading Failed</h1>"
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
start = result.find('<!DOCTYPE html>')
|
| 49 |
-
return result[start:]
|
| 50 |
-
return result
|
| 51 |
|
| 52 |
def improve_code(description, current_code):
|
| 53 |
"""Improve existing code"""
|
| 54 |
if not model_loaded:
|
| 55 |
return current_code
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Extract HTML if there's extra text
|
| 74 |
-
if '<!DOCTYPE html>' in result:
|
| 75 |
-
start = result.find('<!DOCTYPE html>')
|
| 76 |
-
return result[start:]
|
| 77 |
-
return result
|
| 78 |
|
| 79 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 80 |
gr.Markdown("# AI Website Builder")
|
| 81 |
-
gr.Markdown("Powered by
|
| 82 |
|
| 83 |
with gr.Tab("Builder"):
|
| 84 |
with gr.Row():
|
| 85 |
with gr.Column(scale=1):
|
| 86 |
-
gr.Markdown("### Instructions")
|
| 87 |
desc_input = gr.Textbox(
|
| 88 |
label="Describe your website",
|
| 89 |
-
placeholder="e.g., A modern portfolio with dark theme
|
| 90 |
lines=4
|
| 91 |
)
|
| 92 |
gen_btn = gr.Button("Generate Website", variant="primary")
|
| 93 |
imp_btn = gr.Button("Improve Current Code")
|
| 94 |
|
| 95 |
-
gr.Markdown("### Tips")
|
| 96 |
-
gr.Markdown("""
|
| 97 |
-
- Be specific about layout and features
|
| 98 |
-
- Mention color schemes and styling
|
| 99 |
-
- Describe interactive elements needed
|
| 100 |
-
- Request responsive design for mobile
|
| 101 |
-
""")
|
| 102 |
-
|
| 103 |
with gr.Column(scale=2):
|
| 104 |
code_editor = gr.Code(
|
| 105 |
label="HTML Code Editor",
|
| 106 |
language="html",
|
| 107 |
-
lines=30
|
| 108 |
-
value="<!DOCTYPE html>\n<html>\n<head>\n <title>AI Generated Website</title>\n</head>\n<body>\n <h1>Your website will appear here</h1>\n</body>\n</html>"
|
| 109 |
)
|
| 110 |
|
| 111 |
with gr.Row():
|
|
@@ -114,32 +104,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 114 |
with gr.Row():
|
| 115 |
preview = gr.HTML(label="Website Preview")
|
| 116 |
|
| 117 |
-
with gr.Tab("About"):
|
| 118 |
-
gr.Markdown("""
|
| 119 |
-
# AI Website Builder
|
| 120 |
-
|
| 121 |
-
This tool uses Baidu's ERNIE 4.5 model to generate complete websites from text descriptions.
|
| 122 |
-
|
| 123 |
-
## Features:
|
| 124 |
-
- Local model execution (no API keys required)
|
| 125 |
-
- Real-time code generation
|
| 126 |
-
- Live website preview
|
| 127 |
-
- Code editing and improvement
|
| 128 |
-
- Syntax-highlighted editor
|
| 129 |
-
|
| 130 |
-
## How to Use:
|
| 131 |
-
1. Describe your desired website in detail
|
| 132 |
-
2. Click "Generate Website" to create code
|
| 133 |
-
3. Edit the generated code if needed
|
| 134 |
-
4. See live preview of your website
|
| 135 |
-
5. Use "Improve Current Code" to refine specific aspects
|
| 136 |
-
|
| 137 |
-
## Model Information:
|
| 138 |
-
- Model: baidu/ERNIE-4.5-21B-A3B-Thinking
|
| 139 |
-
- Framework: Hugging Face Transformers
|
| 140 |
-
- Execution: Local inference
|
| 141 |
-
""")
|
| 142 |
-
|
| 143 |
# Event handling
|
| 144 |
gen_btn.click(
|
| 145 |
fn=generate_code,
|
|
|
|
| 4 |
import warnings
|
| 5 |
|
| 6 |
# Suppress warnings
|
| 7 |
+
warnings.filterwarnings("ignore")
|
|
|
|
| 8 |
|
| 9 |
+
# Try to load model with fallback
|
| 10 |
+
model_loaded = False
|
| 11 |
try:
|
|
|
|
| 12 |
model_name = "baidu/ERNIE-4.5-21B-A3B-Thinking"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 18 |
trust_remote_code=True
|
| 19 |
)
|
| 20 |
model_loaded = True
|
| 21 |
+
print("Model loaded successfully")
|
| 22 |
except Exception as e:
|
| 23 |
print(f"Model loading failed: {e}")
|
| 24 |
+
# Fallback to a working model
|
| 25 |
+
try:
|
| 26 |
+
model_name = "google/flan-t5-base"
|
| 27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 28 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 29 |
+
model_loaded = True
|
| 30 |
+
print("Fallback model loaded successfully")
|
| 31 |
+
except Exception as e2:
|
| 32 |
+
print(f"Fallback model also failed: {e2}")
|
| 33 |
|
| 34 |
def generate_code(prompt):
|
| 35 |
+
"""Generate HTML code"""
|
| 36 |
if not model_loaded:
|
| 37 |
+
return f"<!-- Model not available -->\n<!DOCTYPE html>\n<html>\n<head>\n <title>{prompt or 'Generated Site'}</title>\n</head>\n<body>\n <h1>Model Loading Failed</h1>\n <p>Using template instead</p>\n</body>\n</html>"
|
| 38 |
|
| 39 |
+
if "flan-t5" in str(type(model)):
|
| 40 |
+
# FLAN-T5 handling
|
| 41 |
+
full_prompt = f"Create a complete HTML file for: {prompt}. Include CSS and JS."
|
| 42 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 43 |
+
outputs = model.generate(**inputs, max_new_tokens=800)
|
| 44 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
return result
|
| 46 |
+
else:
|
| 47 |
+
# ERNIE handling
|
| 48 |
+
full_prompt = f"Create a complete single HTML file with embedded CSS and JavaScript for: {prompt}. Return only valid HTML code."
|
| 49 |
+
inputs = tokenizer(full_prompt, return_tensors="pt").to("cuda")
|
| 50 |
+
outputs = model.generate(**inputs, max_new_tokens=1000, temperature=0.7)
|
| 51 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
+
result = result[len(full_prompt):]
|
| 53 |
+
if '<!DOCTYPE html>' in result:
|
| 54 |
+
start = result.find('<!DOCTYPE html>')
|
| 55 |
+
return result[start:]
|
| 56 |
+
return result
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def improve_code(description, current_code):
|
| 59 |
"""Improve existing code"""
|
| 60 |
if not model_loaded:
|
| 61 |
return current_code
|
| 62 |
+
|
| 63 |
+
if "flan-t5" in str(type(model)):
|
| 64 |
+
prompt = f"Improve this HTML: {description}\n\n{current_code}"
|
| 65 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 66 |
+
outputs = model.generate(**inputs, max_new_tokens=600)
|
| 67 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
+
else:
|
| 69 |
+
prompt = f"Improve this HTML code based on: {description}\n\nCurrent code:\n{current_code}\n\nReturn only the improved HTML code."
|
| 70 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 71 |
+
outputs = model.generate(**inputs, max_new_tokens=800, temperature=0.7)
|
| 72 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 73 |
+
result = result[len(prompt):]
|
| 74 |
+
if '<!DOCTYPE html>' in result:
|
| 75 |
+
start = result.find('<!DOCTYPE html>')
|
| 76 |
+
return result[start:]
|
| 77 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 80 |
gr.Markdown("# AI Website Builder")
|
| 81 |
+
gr.Markdown("Powered by local AI models")
|
| 82 |
|
| 83 |
with gr.Tab("Builder"):
|
| 84 |
with gr.Row():
|
| 85 |
with gr.Column(scale=1):
|
|
|
|
| 86 |
desc_input = gr.Textbox(
|
| 87 |
label="Describe your website",
|
| 88 |
+
placeholder="e.g., A modern portfolio with dark theme",
|
| 89 |
lines=4
|
| 90 |
)
|
| 91 |
gen_btn = gr.Button("Generate Website", variant="primary")
|
| 92 |
imp_btn = gr.Button("Improve Current Code")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Column(scale=2):
|
| 95 |
code_editor = gr.Code(
|
| 96 |
label="HTML Code Editor",
|
| 97 |
language="html",
|
| 98 |
+
lines=30
|
|
|
|
| 99 |
)
|
| 100 |
|
| 101 |
with gr.Row():
|
|
|
|
| 104 |
with gr.Row():
|
| 105 |
preview = gr.HTML(label="Website Preview")
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# Event handling
|
| 108 |
gen_btn.click(
|
| 109 |
fn=generate_code,
|