shaheerawan3 commited on
Commit
6294a96
·
verified ·
1 Parent(s): 0906714

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -38
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
- class WebsiteGenerator:
9
- def __init__(self):
10
- # Initialize the model and tokenizer
11
- self.model_name = "codellama/CodeLlama-34b-Instruct-hf"
12
- self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
13
-
14
- # Add memory optimization parameters
15
- self.model = AutoModelForCausalLM.from_pretrained(
16
- self.model_name,
17
- torch_dtype=torch.float16,
18
- low_cpu_mem_usage=True,
19
- device_map="auto",
20
- max_memory={0: "8GiB"}, # Adjust based on your GPU memory
21
- offload_folder="offload" # For memory offloading
22
- )
23
-
24
- # Template snippets for common components
25
- self.templates = self._load_templates()
26
 
27
- def _load_templates(self):
28
- template_dir = Path("templates")
29
- if not template_dir.exists():
30
- template_dir.mkdir(exist_ok=True)
31
- # Create default templates if they don't exist
32
- return {
33
- "base": "<!DOCTYPE html><html><head><title>Generated Site</title></head><body></body></html>",
34
- "navbar": "<nav></nav>",
35
- "footer": "<footer></footer>"
36
- }
37
- return {
38
- "base": (template_dir / "base.html").read_text() if (template_dir / "base.html").exists() else "",
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
- # Rest of the code remains the same...
44
- # (previous methods for generate_code, _construct_prompt, _clean_generated_code)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # Main Streamlit interface code remains the same...
 
 
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()