dshamika commited on
Commit
21bc4d3
·
verified ·
1 Parent(s): 7eb678b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -60
app.py CHANGED
@@ -1,65 +1,63 @@
1
  import gradio as gr
2
- from diffusers import StableDiffusionPipeline
3
- import torch
4
- from PIL import Image, ImageDraw
5
- import langid
6
- from transformers import pipeline # Optional fallback for lang detection
7
-
8
- # Load Stable Diffusion model (use GPU if available)
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
11
- pipe = pipe.to(device)
12
-
13
- # Language mapping
14
- lang_map = {
15
- "si": "Sinhala",
16
- "en": "English",
17
- "ta": "Tamil"
18
- }
19
-
20
- def detect_language(text):
21
- lang, _ = langid.classify(text)
22
- if lang in lang_map:
23
- return lang_map[lang]
24
- return "English" # Default fallback
25
-
26
- def generate_design(text, main_image=None, logo=None):
27
- # Detect language
28
- language = detect_language(text)
29
-
30
- # Create prompt for design generation
31
- prompt = f"Beautiful social media post graphic design with the text '{text}' in {language} font, elegant layout, modern shapes, vibrant colors, clean background, high resolution"
32
-
33
- # Generate base image (512x512 for social media square post)
34
- image = pipe(prompt).images[0]
35
-
36
- # If main image uploaded, resize and paste centered
37
- if main_image:
38
- main_img = Image.open(main_image).resize((300, 300)) # Resize to fit
39
- image.paste(main_img, ( (image.width - main_img.width) // 2, (image.height - main_img.height) // 2 ))
40
-
41
- # If logo uploaded, resize and paste in bottom-right corner
42
- if logo:
43
- logo_img = Image.open(logo).resize((100, 100)) # Small logo size
44
- image.paste(logo_img, (image.width - logo_img.width - 10, image.height - logo_img.height - 10), logo_img if logo_img.mode == 'RGBA' else None)
45
-
46
- # Add subtle border or shapes (optional enhancement with PIL)
47
- draw = ImageDraw.Draw(image)
48
- draw.rectangle([(10, 10), (image.width-10, image.height-10)], outline="white", width=5)
49
-
50
- return image
51
-
52
- # Gradio interface
 
 
53
  iface = gr.Interface(
54
- fn=generate_design,
55
- inputs=[
56
- gr.Textbox(label="Enter Text for the Post"),
57
- gr.Image(type="filepath", label="Upload Main Image (e.g., Product/Human) - Optional"),
58
- gr.Image(type="filepath", label="Upload Business Logo - Optional")
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()