divython commited on
Commit
3c849b2
·
verified ·
1 Parent(s): db7579c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -1,13 +1,14 @@
1
- # Updated UI Generator with Better Open-Source Model
2
 
3
  import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
5
  import torch
 
6
 
7
- # Use a better model for code generation, such as DeepSeek-Coder or Codestral
8
- model_id = "deepseek-ai/deepseek-coder-6.7b-instruct" # Change to another if desired
9
 
10
- # Ensure torch uses CPU if no GPU
11
  device = 0 if torch.cuda.is_available() else -1
12
 
13
  # Load tokenizer and model
@@ -15,19 +16,29 @@ print("Loading model...")
15
  tokenizer = AutoTokenizer.from_pretrained(model_id)
16
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
17
 
18
- # Build the text generation pipeline
19
  generator = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=device)
20
 
21
  def generate_ui(platform, framework, ui_prompt):
22
- prompt = f"""
23
- You are an expert mobile app developer.
24
- Generate a complete {framework} UI code snippet for a {platform} app based on the description:
25
- "{ui_prompt}"
26
- Include all required imports, a main method, and best practices for UI structure.
27
  """
28
- response = generator(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
29
- # Trim the echoed prompt and just return the generated code
30
- return response.split(""""""")[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  interface = gr.Interface(
33
  fn=generate_ui,
@@ -36,9 +47,12 @@ interface = gr.Interface(
36
  gr.Dropdown(["Flutter", "Kotlin XML", "SwiftUI", "React Native"], label="Framework"),
37
  gr.Textbox(lines=4, label="UI Prompt", placeholder="e.g. Login screen with email & password, dark theme")
38
  ],
39
- outputs=gr.Code(label="Generated UI Code"),
 
 
 
40
  title="Prompt-to-UI Code Generator",
41
- description="Generate Android/iOS UI code in Flutter, SwiftUI, XML, or React Native by just describing the layout."
42
  )
43
 
44
  interface.launch()
 
1
+ # Updated UI Generator with Better Open-Source Model and DartPad/Expo Preview Support
2
 
3
  import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline
5
  import torch
6
+ import re
7
 
8
+ # Use a smaller model for CPU support
9
+ model_id = "deepseek-ai/deepseek-coder-1.3b-instruct"
10
 
11
+ # Use CPU or GPU if available
12
  device = 0 if torch.cuda.is_available() else -1
13
 
14
  # Load tokenizer and model
 
16
  tokenizer = AutoTokenizer.from_pretrained(model_id)
17
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
18
 
19
+ # Build the generation pipeline
20
  generator = TextGenerationPipeline(model=model, tokenizer=tokenizer, device=device)
21
 
22
  def generate_ui(platform, framework, ui_prompt):
23
+ prompt_template = f"""
24
+ You are a professional mobile developer.
25
+ Create a full {framework} UI code for {platform} based on this request:
 
 
26
  """
27
+ full_prompt = prompt_template + f"\n{ui_prompt}\n" + "\nEnsure to include imports, widgets, and app structure."
28
+ response = generator(full_prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
29
+
30
+ # Extract only code block (removes echoed prompt if needed)
31
+ code_match = re.search(r"(?s)(import .*?)$", response)
32
+ code = code_match.group(1).strip() if code_match else response.strip()
33
+ return code, generate_preview_link(code, framework)
34
+
35
+ def generate_preview_link(code, framework):
36
+ if framework == "Flutter":
37
+ return f"https://dartpad.dev/?id=custom&code={gr.utils.sanitize_html(code)[:1500]}"
38
+ elif framework == "React Native":
39
+ return f"https://snack.expo.dev/?code={gr.utils.sanitize_html(code)[:1500]}"
40
+ else:
41
+ return "Preview not available for this framework."
42
 
43
  interface = gr.Interface(
44
  fn=generate_ui,
 
47
  gr.Dropdown(["Flutter", "Kotlin XML", "SwiftUI", "React Native"], label="Framework"),
48
  gr.Textbox(lines=4, label="UI Prompt", placeholder="e.g. Login screen with email & password, dark theme")
49
  ],
50
+ outputs=[
51
+ gr.Code(label="Generated UI Code"),
52
+ gr.Textbox(label="Preview Link")
53
+ ],
54
  title="Prompt-to-UI Code Generator",
55
+ description="Describe your screen and get UI code + preview for Flutter/React Native apps."
56
  )
57
 
58
  interface.launch()