Update app.py
Browse files
app.py
CHANGED
@@ -1,65 +1,63 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
return
|
51 |
-
|
52 |
-
#
|
|
|
|
|
53 |
iface = gr.Interface(
|
54 |
-
fn=
|
55 |
-
inputs=
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
],
|
60 |
-
outputs=gr.Image(label="Generated Social Media Post Design"),
|
61 |
-
title="AI Graphic Design Tool for Social Media",
|
62 |
-
description="Input text, optionally upload images/logo. Detects Sinhala/English/Tamil and generates a design."
|
63 |
)
|
64 |
|
65 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os, zipfile, tempfile
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
+
import json
|
5 |
+
|
6 |
+
# ---------------------------
|
7 |
+
# 1️⃣ Load Local Model
|
8 |
+
# ---------------------------
|
9 |
+
model_name = "TheBloke/WizardCoder-7B-GPTQ"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
+
generator = pipeline('text-generation', model=model, tokenizer=tokenizer)
|
13 |
+
|
14 |
+
# ---------------------------
|
15 |
+
# 2️⃣ Website Generation Function
|
16 |
+
# ---------------------------
|
17 |
+
def generate_website(prompt):
|
18 |
+
# Generate multi-file code as JSON
|
19 |
+
instruction = f"""
|
20 |
+
Generate a full website project structure based on the user prompt.
|
21 |
+
Provide the response in JSON format where key = file path, value = file content.
|
22 |
+
Include folders: css/, js/, assets/, db/ if needed. User prompt: {prompt}
|
23 |
+
"""
|
24 |
+
gen = generator(instruction, max_new_tokens=2048)[0]['generated_text']
|
25 |
+
|
26 |
+
# Parse JSON safely
|
27 |
+
try:
|
28 |
+
files_dict = json.loads(gen)
|
29 |
+
except:
|
30 |
+
files_dict = {"index.html": "<!-- Failed to generate code -->"}
|
31 |
+
|
32 |
+
# Create temp folder and save files
|
33 |
+
temp_dir = tempfile.mkdtemp()
|
34 |
+
for fname, content in files_dict.items():
|
35 |
+
file_path = os.path.join(temp_dir, fname)
|
36 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
37 |
+
with open(file_path, "w", encoding="utf-8") as f:
|
38 |
+
f.write(content)
|
39 |
+
|
40 |
+
# Zip the folder
|
41 |
+
zip_path = os.path.join(temp_dir, "website.zip")
|
42 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
|
43 |
+
for root, _, files in os.walk(temp_dir):
|
44 |
+
for file in files:
|
45 |
+
if file != os.path.basename(zip_path):
|
46 |
+
file_path = os.path.join(root, file)
|
47 |
+
arcname = os.path.relpath(file_path, temp_dir)
|
48 |
+
zipf.write(file_path, arcname=arcname)
|
49 |
+
|
50 |
+
return zip_path
|
51 |
+
|
52 |
+
# ---------------------------
|
53 |
+
# 3️⃣ Gradio UI
|
54 |
+
# ---------------------------
|
55 |
iface = gr.Interface(
|
56 |
+
fn=generate_website,
|
57 |
+
inputs=gr.Textbox(lines=4, placeholder="Describe your website: PHP/HTML/JS, admin panel, MySQL..."),
|
58 |
+
outputs=gr.File(label="Download Generated Website ZIP"),
|
59 |
+
title="Local AI Multi-file Website Generator",
|
60 |
+
description="Single Python file app. Generates multi-file website locally with folders (css/, js/, assets/, db/). Download as ZIP."
|
|
|
|
|
|
|
|
|
61 |
)
|
62 |
|
63 |
iface.launch()
|