namaai commited on
Commit
a724b73
·
verified ·
1 Parent(s): 9894edb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +170 -0
app.py ADDED
@@ -0,0 +1,170 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain_groq import ChatGroq
3
+ from groq import Groq
4
+ import os
5
+ from streamlit_ace import st_ace
6
+ import re
7
+
8
+ # Initialize the Groq client
9
+ api_key=os.getenv("GROQ_KEY")
10
+ llm = Groq(api_key=api_key)
11
+
12
+ MODELS = [
13
+ "llama-3.1-70b-versatile",
14
+ "llama-3.1-8b-instant",
15
+ "llama3-groq-70b-8192-tool-use-preview",
16
+ "llama3-groq-8b-8192-tool-use-preview",
17
+ "llama-guard-3-8b",
18
+ "llama3-70b-8192",
19
+ "llama3-8b-8192",
20
+ "mixtral-8x7b-32768",
21
+ "gemma-7b-it",
22
+ "gemma2-9b-it"
23
+ ]
24
+
25
+ STYLES = [
26
+ "Modern", "Vintage", "Minimalist", "Futuristic", "Retro",
27
+ "Glassmorphism", "Neumorphism", "Material Design", "Flat Design", "Cyberpunk",
28
+ ]
29
+
30
+ CSS_FRAMEWORKS = ["Tailwind CSS", "SCSS", "Vanilla CSS"]
31
+
32
+ def generate_component(user_requirement: str, model_type: str, style: str, css_framework: str):
33
+ prompt = f"""
34
+ As a world-class Frontend Engineer, create a cutting-edge UI component based on:
35
+
36
+ User Requirement: {user_requirement}
37
+ Desired Style: {style}
38
+ CSS Framework: {css_framework}
39
+
40
+ Provide separate HTML and CSS/SCSS/Tailwind classes as applicable.
41
+ Ensure the code is production-ready, responsive, and follows best practices for accessibility and performance.
42
+
43
+ For 3D components, use appropriate CSS 3D transforms or consider integrating with Three.js.
44
+
45
+ Important: Ensure the component fits within a 600x400 pixel container for preview purposes.
46
+
47
+ Format your response as follows:
48
+
49
+ HTML:
50
+ ```html
51
+ <!-- Your HTML code here -->
52
+ ```
53
+
54
+ {css_framework}:
55
+ ```{'css' if css_framework != 'Tailwind CSS' else 'html'}
56
+ /* Your styling code here */
57
+ ```
58
+
59
+ Ensure the code aligns perfectly with the requested style, functionality, and chosen CSS framework.
60
+ For Tailwind CSS, include classes directly in the HTML.
61
+ Do not include any explanations or comments outside the code blocks.
62
+ """
63
+
64
+ completion = llm.chat.completions.create(
65
+ messages=[{"role": "user", "content": prompt}],
66
+ model=model_type,
67
+ temperature=0.4
68
+ )
69
+
70
+ return completion.choices[0].message.content
71
+
72
+ def extract_code(generated_code, language):
73
+ pattern = rf"```{language}(.*?)```"
74
+ match = re.search(pattern, generated_code, re.DOTALL)
75
+ return match.group(1).strip() if match else ""
76
+
77
+ def render_component(html_code, css_code, css_framework):
78
+ if css_framework == "Tailwind CSS":
79
+ combined_code = f"""
80
+ <script src="https://cdn.tailwindcss.com"></script>
81
+ <div style="width: 600px; height: 400px; overflow: auto;">
82
+ {html_code}
83
+ </div>
84
+ """
85
+ else:
86
+ combined_code = f"""
87
+ <style>{css_code}</style>
88
+ <div style="width: 600px; height: 400px; overflow: auto;">
89
+ {html_code}
90
+ </div>
91
+ """
92
+ return combined_code
93
+
94
+ st.set_page_config(layout="wide")
95
+
96
+ st.title("Advanced Frontend Component Generator")
97
+ st.write("By Namah AI")
98
+
99
+ col1, col2 = st.columns([1, 2])
100
+
101
+ with col1:
102
+ st.header("Component Settings")
103
+ llm_type = st.selectbox("Select Model", MODELS)
104
+ style = st.selectbox("Select Style", STYLES)
105
+ css_framework = st.selectbox("Select CSS Framework", CSS_FRAMEWORKS)
106
+
107
+ user_req = st.text_area("Describe your component", height=100,
108
+ placeholder="e.g., Create a 3D rotating cube button with hover effects")
109
+
110
+ if 'generated_html' not in st.session_state:
111
+ st.session_state.generated_html = ""
112
+ if 'generated_css' not in st.session_state:
113
+ st.session_state.generated_css = ""
114
+
115
+ if user_req:
116
+ if st.button("Generate Component"):
117
+ with st.spinner("Generating component..."):
118
+ generated_code = generate_component(user_req, llm_type, style, css_framework)
119
+ st.session_state.generated_html = extract_code(generated_code, "html")
120
+ st.session_state.generated_css = extract_code(generated_code, 'css' if css_framework != 'Tailwind CSS' else 'html')
121
+ st.success("Component generated successfully!")
122
+
123
+ if st.button("Regenerate"):
124
+ with st.spinner("Regenerating component..."):
125
+ generated_code = generate_component(user_req, llm_type, style, css_framework)
126
+ st.session_state.generated_html = extract_code(generated_code, "html")
127
+ st.session_state.generated_css = extract_code(generated_code, 'css' if css_framework != 'Tailwind CSS' else 'html')
128
+ st.success("Component regenerated successfully!")
129
+ st.rerun()
130
+
131
+ with col2:
132
+ if st.session_state.generated_html and st.session_state.generated_css:
133
+ st.header("Generated Component")
134
+
135
+ tabs = st.tabs(["Preview", "HTML", f"{css_framework}"])
136
+
137
+ with tabs[0]:
138
+ st.subheader("Live Preview")
139
+ st.components.v1.html(render_component(st.session_state.generated_html, st.session_state.generated_css, css_framework), height=450)
140
+
141
+ with tabs[1]:
142
+ st.subheader("Edit HTML")
143
+ edited_html = st_ace(value=st.session_state.generated_html, language="html", theme="github", key="html_editor", height=400)
144
+
145
+ with tabs[2]:
146
+ st.subheader(f"Edit {css_framework}")
147
+ edited_css = st_ace(value=st.session_state.generated_css,
148
+ language="css" if css_framework != "Tailwind CSS" else "html",
149
+ theme="github", key="css_editor", height=400)
150
+
151
+ if st.button("Update Live Preview"):
152
+ st.session_state.generated_html = edited_html
153
+ st.session_state.generated_css = edited_css
154
+ st.rerun()
155
+
156
+ st.sidebar.header("About")
157
+ st.sidebar.info("""
158
+ This app generates frontend components based on your description.
159
+ You can choose different styles and CSS frameworks, and see a live preview of the result.
160
+ You can also edit the generated code and update the preview in real-time.
161
+ """)
162
+
163
+ st.sidebar.header("Tips")
164
+ st.sidebar.info("""
165
+ 1. Be specific in your component description.
166
+ 2. Try different styles and frameworks for variety.
167
+ 3. Use the 'Regenerate' button for new ideas.
168
+ 4. Edit the code to fine-tune the component.
169
+ 5. The CSS will update automatically when you change the CSS framework.
170
+ """)