akhaliq HF staff commited on
Commit
d107cdf
1 Parent(s): a5d0818

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -26,11 +26,24 @@ def generate_gradio_app(image):
26
  img_str = base64.b64encode(buffered.getvalue()).decode()
27
 
28
  # Prepare the prompt
29
- prompt = """You are a UX/UI designer. Describe the attached screenshot or UI mockup in detail. I will feed in the output you give me to a coding model that will attempt to recreate this mockup as a Gradio app, so please think step by step and describe the UI in detail. Pay close attention to background color, text color, font size, font family, padding, margin, border, etc. Match the colors and sizes exactly. Make sure to mention every part of the screenshot including any headers, footers, etc. Use the exact text from the screenshot. After describing the UI, suggest how this could be implemented using Gradio components."""
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  # Make the API call
32
  stream = client.chat.completions.create(
33
- model="meta-llama/Llama-Vision-Free",
34
  messages=[
35
  {
36
  "role": "user",
@@ -45,7 +58,7 @@ def generate_gradio_app(image):
45
  ],
46
  }
47
  ],
48
- max_tokens=2048,
49
  temperature=0.7,
50
  top_p=0.7,
51
  top_k=50,
@@ -55,16 +68,16 @@ def generate_gradio_app(image):
55
  )
56
 
57
  # Collect the streamed response
58
- generated_text = ""
59
  for chunk in stream:
60
  if chunk.choices[0].delta.content is not None:
61
- generated_text += chunk.choices[0].delta.content
62
- yield f"Generating... (Current length: {len(generated_text)} characters)\n\n{generated_text}"
63
 
64
- if not generated_text:
65
- return "Error: No response generated from the model. Please try again."
66
 
67
- return generated_text
68
 
69
  except Exception as e:
70
  error_message = str(e)
@@ -72,21 +85,21 @@ def generate_gradio_app(image):
72
  return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease try again or check your API key and connection."
73
 
74
  with gr.Blocks() as demo:
75
- gr.Markdown("# Analyze wireframe and suggest Gradio app layout")
76
- gr.Markdown("Upload an image of your UI design for analysis and suggestions.")
77
 
78
  with gr.Row():
79
  with gr.Column(scale=1):
80
  image_input = gr.Image(label="Upload a screenshot", elem_id="image_upload")
81
- generate_button = gr.Button("Analyze and Suggest", variant="primary")
82
 
83
  with gr.Column(scale=2):
84
- text_output = gr.Textbox(label="Analysis and Suggestions", lines=20)
85
 
86
  generate_button.click(
87
  fn=generate_gradio_app,
88
  inputs=[image_input],
89
- outputs=[text_output]
90
  )
91
 
92
  if __name__ == "__main__":
 
26
  img_str = base64.b64encode(buffered.getvalue()).decode()
27
 
28
  # Prepare the prompt
29
+ prompt = """You are an AI assistant specialized in UI/UX design and Gradio app development. Analyze the attached screenshot or UI mockup and generate a complete Gradio code that recreates this design. Follow these steps:
30
+
31
+ 1. Describe the main elements of the UI, including layout, colors, and components.
32
+ 2. Generate a complete Gradio Python code that recreates this UI as closely as possible.
33
+ 3. Use appropriate Gradio components for each element in the UI.
34
+ 4. Include all necessary imports at the beginning of the code.
35
+ 5. Implement placeholder functions for any interactive elements (buttons, inputs, etc.).
36
+ 6. Use gr.Blocks() to create a layout that matches the screenshot.
37
+ 7. Add appropriate labels and descriptions for all components.
38
+ 8. Include the gr.Blocks().launch() call at the end of the code.
39
+ 9. Provide a complete, runnable Gradio application that can be executed as-is.
40
+ 10. Add comments explaining the purpose of each major section or component.
41
+
42
+ Please generate the entire Gradio code based on the provided image."""
43
 
44
  # Make the API call
45
  stream = client.chat.completions.create(
46
+ model="meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo",
47
  messages=[
48
  {
49
  "role": "user",
 
58
  ],
59
  }
60
  ],
61
+ max_tokens=4096,
62
  temperature=0.7,
63
  top_p=0.7,
64
  top_k=50,
 
68
  )
69
 
70
  # Collect the streamed response
71
+ generated_code = ""
72
  for chunk in stream:
73
  if chunk.choices[0].delta.content is not None:
74
+ generated_code += chunk.choices[0].delta.content
75
+ yield f"Generating Gradio code... (Current length: {len(generated_code)} characters)\n\n{generated_code}"
76
 
77
+ if not generated_code:
78
+ return "Error: No code generated from the model. Please try again."
79
 
80
+ return generated_code
81
 
82
  except Exception as e:
83
  error_message = str(e)
 
85
  return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease try again or check your API key and connection."
86
 
87
  with gr.Blocks() as demo:
88
+ gr.Markdown("# Generate Gradio App from Wireframe")
89
+ gr.Markdown("Upload an image of your UI design, and we'll generate Gradio code to recreate it.")
90
 
91
  with gr.Row():
92
  with gr.Column(scale=1):
93
  image_input = gr.Image(label="Upload a screenshot", elem_id="image_upload")
94
+ generate_button = gr.Button("Generate Gradio Code", variant="primary")
95
 
96
  with gr.Column(scale=2):
97
+ code_output = gr.Code(language="python", label="Generated Gradio Code", lines=30)
98
 
99
  generate_button.click(
100
  fn=generate_gradio_app,
101
  inputs=[image_input],
102
+ outputs=[code_output]
103
  )
104
 
105
  if __name__ == "__main__":