dshamika commited on
Commit
b778309
·
verified ·
1 Parent(s): ff544ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -1,33 +1,36 @@
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()
@@ -57,7 +60,7 @@ iface = gr.Interface(
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()
 
1
  import gradio as gr
2
+ import os, zipfile, tempfile, json
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
 
4
 
5
  # ---------------------------
6
+ # 1️⃣ Load Public Code Model
7
  # ---------------------------
8
+ model_name = "Salesforce/codegen-350M-multi" # public model, no token required
9
  tokenizer = AutoTokenizer.from_pretrained(model_name)
10
  model = AutoModelForCausalLM.from_pretrained(model_name)
11
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
12
 
13
  # ---------------------------
14
  # 2️⃣ Website Generation Function
15
  # ---------------------------
16
  def generate_website(prompt):
17
+ # Instruction for multi-file code in JSON format
18
  instruction = f"""
19
+ Generate a full website project based on the user prompt.
20
+ Return the result as JSON where key = file path and value = file content.
21
+ Include folders: css/, js/, assets/, db/ if needed.
22
+ User prompt: {prompt}
23
  """
 
24
 
25
+ # Generate code text
26
+ gen = generator(instruction, max_new_tokens=1024)[0]['generated_text']
27
+
28
+ # Try parsing JSON from generated text
29
  try:
30
  files_dict = json.loads(gen)
31
  except:
32
+ # fallback: single index.html if parsing fails
33
+ files_dict = {"index.html": "<!-- Failed to generate valid code -->"}
34
 
35
  # Create temp folder and save files
36
  temp_dir = tempfile.mkdtemp()
 
60
  inputs=gr.Textbox(lines=4, placeholder="Describe your website: PHP/HTML/JS, admin panel, MySQL..."),
61
  outputs=gr.File(label="Download Generated Website ZIP"),
62
  title="Local AI Multi-file Website Generator",
63
+ description="Single Python file app. Generates multi-file website locally using public HF model. Download as ZIP."
64
  )
65
 
66
  iface.launch()