Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
import random
|
6 |
+
import string
|
7 |
+
from html_generator import generate_website_html
|
8 |
+
|
9 |
+
# CSS for styling the interface
|
10 |
+
css = """
|
11 |
+
body {
|
12 |
+
font-family: 'Inter', sans-serif;
|
13 |
+
background-color: #f9fafb;
|
14 |
+
}
|
15 |
+
.container {
|
16 |
+
max-width: 900px;
|
17 |
+
margin: 0 auto;
|
18 |
+
}
|
19 |
+
.header {
|
20 |
+
text-align: center;
|
21 |
+
margin-bottom: 20px;
|
22 |
+
}
|
23 |
+
.header h1 {
|
24 |
+
font-size: 2.5rem;
|
25 |
+
font-weight: 700;
|
26 |
+
color: #1f2937;
|
27 |
+
}
|
28 |
+
.header p {
|
29 |
+
font-size: 1.1rem;
|
30 |
+
color: #6b7280;
|
31 |
+
}
|
32 |
+
.output-container {
|
33 |
+
margin-top: 20px;
|
34 |
+
border-radius: 8px;
|
35 |
+
overflow: hidden;
|
36 |
+
border: 1px solid #e5e7eb;
|
37 |
+
}
|
38 |
+
.prompt-box {
|
39 |
+
border: 1px solid #d1d5db;
|
40 |
+
border-radius: 8px;
|
41 |
+
padding: 15px;
|
42 |
+
}
|
43 |
+
.generate-btn {
|
44 |
+
background-color: #3b82f6 !important;
|
45 |
+
color: white !important;
|
46 |
+
}
|
47 |
+
.generate-btn:hover {
|
48 |
+
background-color: #2563eb !important;
|
49 |
+
}
|
50 |
+
.example-btn {
|
51 |
+
background-color: #f3f4f6 !important;
|
52 |
+
border: 1px solid #d1d5db !important;
|
53 |
+
color: #374151 !important;
|
54 |
+
}
|
55 |
+
"""
|
56 |
+
|
57 |
+
# Function to generate a random ID for the website
|
58 |
+
def generate_site_id():
|
59 |
+
letters = string.ascii_lowercase + string.digits
|
60 |
+
return ''.join(random.choice(letters) for i in range(8))
|
61 |
+
|
62 |
+
# List of example prompts
|
63 |
+
example_prompts = [
|
64 |
+
"Create a landing page for a fitness app with a modern design",
|
65 |
+
"Make a personal portfolio website for a photographer with a dark theme",
|
66 |
+
"Design a restaurant website with menu, about us, and contact sections",
|
67 |
+
"Build a simple e-commerce product page for a smart watch",
|
68 |
+
"Create a tech startup landing page with features and pricing sections"
|
69 |
+
]
|
70 |
+
|
71 |
+
# Function to handle website generation
|
72 |
+
def create_website(prompt):
|
73 |
+
site_id = generate_site_id()
|
74 |
+
|
75 |
+
try:
|
76 |
+
# Generate HTML content based on the prompt
|
77 |
+
html_content = generate_website_html(prompt)
|
78 |
+
|
79 |
+
# Save the HTML content to a file
|
80 |
+
os.makedirs('websites', exist_ok=True)
|
81 |
+
file_path = f"websites/{site_id}.html"
|
82 |
+
with open(file_path, "w") as f:
|
83 |
+
f.write(html_content)
|
84 |
+
|
85 |
+
# In a real Hugging Face Space, you might use a different mechanism to serve the file
|
86 |
+
# For demo purposes, we'll return the HTML directly
|
87 |
+
preview_html = f"""
|
88 |
+
<div style="padding: 10px; background-color: #f0f0f0; border-radius: 8px; margin-bottom: 10px;">
|
89 |
+
<p><strong>Site ID:</strong> {site_id}</p>
|
90 |
+
<p><strong>Prompt:</strong> {prompt}</p>
|
91 |
+
</div>
|
92 |
+
<div style="border: 1px solid #ddd; border-radius: 8px; overflow: hidden;">
|
93 |
+
<iframe srcdoc="{html_content.replace('"', '"')}" width="100%" height="600px"></iframe>
|
94 |
+
</div>
|
95 |
+
"""
|
96 |
+
|
97 |
+
return preview_html, html_content
|
98 |
+
|
99 |
+
except Exception as e:
|
100 |
+
return f"Error generating website: {str(e)}", ""
|
101 |
+
|
102 |
+
# Define the Gradio interface
|
103 |
+
with gr.Blocks(css=css) as demo:
|
104 |
+
with gr.Column(elem_classes="container"):
|
105 |
+
gr.HTML("""
|
106 |
+
<div class="header">
|
107 |
+
<h1>🚀 InstaSite</h1>
|
108 |
+
<p>Generate a complete website from a simple prompt. Describe what you want, and we'll create it.</p>
|
109 |
+
</div>
|
110 |
+
""")
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
prompt_input = gr.Textbox(
|
114 |
+
placeholder="Describe the website you want to create...",
|
115 |
+
label="Your Website Prompt",
|
116 |
+
elem_classes="prompt-box"
|
117 |
+
)
|
118 |
+
|
119 |
+
with gr.Row():
|
120 |
+
generate_btn = gr.Button("Generate Website", elem_classes="generate-btn")
|
121 |
+
|
122 |
+
with gr.Row():
|
123 |
+
gr.HTML("<p>Need inspiration? Try one of these examples:</p>")
|
124 |
+
|
125 |
+
with gr.Row():
|
126 |
+
example_buttons = [gr.Button(f"Example {i+1}", elem_classes="example-btn") for i in range(5)]
|
127 |
+
|
128 |
+
with gr.Accordion("Advanced Options", open=False):
|
129 |
+
gr.Checkbox(label="Include responsive design", value=True)
|
130 |
+
gr.Checkbox(label="Generate CSS animations", value=False)
|
131 |
+
gr.Dropdown(["Light", "Dark", "Colorful"], label="Color Theme", value="Light")
|
132 |
+
|
133 |
+
output_preview = gr.HTML(elem_classes="output-container")
|
134 |
+
output_code = gr.Code(language="html", label="HTML Code", visible=False)
|
135 |
+
|
136 |
+
# Set up button actions
|
137 |
+
generate_btn.click(create_website, inputs=[prompt_input], outputs=[output_preview, output_code])
|
138 |
+
|
139 |
+
# Set up example button actions
|
140 |
+
for i, btn in enumerate(example_buttons):
|
141 |
+
btn.click(lambda _, i=i: example_prompts[i], outputs=prompt_input)
|
142 |
+
|
143 |
+
# Launch the app
|
144 |
+
if __name__ == "__main__":
|
145 |
+
demo.launch(share=True)
|