Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,100 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
import re
|
| 6 |
import json
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
offload_folder="offload" # For memory offloading
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# Template snippets for common components
|
| 25 |
-
self.templates = self._load_templates()
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"navbar": (template_dir / "navbar.html").read_text() if (template_dir / "navbar.html").exists() else "",
|
| 40 |
-
"footer": (template_dir / "footer.html").read_text() if (template_dir / "footer.html").exists() else ""
|
| 41 |
-
}
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
import json
|
| 5 |
+
from pathlib import Path
|
| 6 |
|
| 7 |
+
# Enable Streamlit caching
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_model():
|
| 10 |
+
# Using a smaller model for faster loading
|
| 11 |
+
model_name = "Salesforce/codegen-350M-mono" # Much smaller than CodeLlama
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
model_name,
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
low_cpu_mem_usage=True,
|
| 17 |
+
device_map="auto"
|
| 18 |
+
)
|
| 19 |
+
return tokenizer, model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def generate_code(model, tokenizer, prompt, max_length=500):
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 23 |
+
with torch.no_grad(): # Disable gradient calculation for inference
|
| 24 |
+
outputs = model.generate(
|
| 25 |
+
inputs.input_ids,
|
| 26 |
+
max_length=max_length,
|
| 27 |
+
num_return_sequences=1,
|
| 28 |
+
temperature=0.7,
|
| 29 |
+
top_p=0.95,
|
| 30 |
+
do_sample=True
|
| 31 |
+
)
|
| 32 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
def main():
|
| 35 |
+
st.set_page_config(page_title="Fast Web Generator", layout="wide")
|
| 36 |
+
|
| 37 |
+
st.title("⚡ Fast Web Generator")
|
| 38 |
+
|
| 39 |
+
# Initialize model with caching
|
| 40 |
+
with st.spinner("Loading model... (first run only)"):
|
| 41 |
+
tokenizer, model = load_model()
|
| 42 |
+
|
| 43 |
+
# Simplified interface
|
| 44 |
+
col1, col2 = st.columns([2, 1])
|
| 45 |
+
|
| 46 |
+
with col1:
|
| 47 |
+
description = st.text_area(
|
| 48 |
+
"Describe your website",
|
| 49 |
+
height=150,
|
| 50 |
+
placeholder="E.g., Create a responsive landing page with a navigation bar, hero section, and contact form"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
generate_type = st.radio(
|
| 54 |
+
"What would you like to generate?",
|
| 55 |
+
["HTML/CSS", "JavaScript", "Full Stack"],
|
| 56 |
+
horizontal=True
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
with col2:
|
| 60 |
+
st.subheader("Quick Settings")
|
| 61 |
+
use_tailwind = st.checkbox("Use Tailwind CSS", value=True)
|
| 62 |
+
responsive = st.checkbox("Make Responsive", value=True)
|
| 63 |
+
dark_mode = st.checkbox("Add Dark Mode", value=False)
|
| 64 |
+
|
| 65 |
+
if st.button("🚀 Generate Code"):
|
| 66 |
+
with st.spinner("Generating your code..."):
|
| 67 |
+
# Construct prompt based on settings
|
| 68 |
+
prompt_template = f"""
|
| 69 |
+
Create a website with these specifications:
|
| 70 |
+
Description: {description}
|
| 71 |
+
CSS Framework: {"Tailwind" if use_tailwind else "Plain CSS"}
|
| 72 |
+
Features: {"Responsive, " if responsive else ""}{"Dark mode, " if dark_mode else ""}
|
| 73 |
+
|
| 74 |
+
Generate the code:
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
generated_code = generate_code(model, tokenizer, prompt_template)
|
| 78 |
+
|
| 79 |
+
# Display code
|
| 80 |
+
st.code(generated_code, language="html")
|
| 81 |
+
|
| 82 |
+
# Add download button
|
| 83 |
+
st.download_button(
|
| 84 |
+
"📥 Download Code",
|
| 85 |
+
generated_code,
|
| 86 |
+
file_name="generated_website.html",
|
| 87 |
+
mime="text/html"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Add helpful tips
|
| 91 |
+
with st.expander("💡 Tips for better results"):
|
| 92 |
+
st.markdown("""
|
| 93 |
+
- Be specific in your description
|
| 94 |
+
- Mention key features you want
|
| 95 |
+
- Specify color schemes if important
|
| 96 |
+
- Describe the layout structure
|
| 97 |
+
""")
|
| 98 |
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|