akhaliq HF staff commited on
Commit
cad972e
1 Parent(s): 2c271aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -5,7 +5,6 @@ from io import BytesIO
5
  from PIL import Image
6
  import numpy as np
7
  import traceback
8
- import json
9
 
10
  def generate_gradio_app(api_key, image):
11
  if not api_key:
@@ -25,20 +24,19 @@ def generate_gradio_app(api_key, image):
25
  img_str = base64.b64encode(buffered.getvalue()).decode()
26
 
27
  # Prepare the prompt
28
- 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:
29
 
30
- 1. Describe the main elements of the UI, including layout, colors, and components.
31
- 2. Generate a complete Gradio Python code that recreates this UI as closely as possible.
32
- 3. Use appropriate Gradio components for each element in the UI.
33
- 4. Include all necessary imports at the beginning of the code.
34
- 5. Implement placeholder functions for any interactive elements (buttons, inputs, etc.).
35
- 6. Use gr.Blocks() to create a layout that matches the screenshot.
36
- 7. Add appropriate labels and descriptions for all components.
37
- 8. Include the gr.Blocks().launch() call at the end of the code.
38
- 9. Provide a complete, runnable Gradio application that can be executed as-is.
39
- 10. Add comments explaining the purpose of each major section or component.
40
 
41
- Please generate the entire Gradio code based on the provided image."""
42
 
43
  # Make the API call with the corrected message format
44
  response = client.chat.completions.create(
@@ -49,7 +47,7 @@ Please generate the entire Gradio code based on the provided image."""
49
  "content": prompt + f"\n\n<image>\ndata:image/png;base64,{img_str}\n</image>"
50
  }
51
  ],
52
- max_tokens=4096,
53
  temperature=0.7,
54
  top_p=0.7,
55
  top_k=50,
@@ -58,8 +56,9 @@ Please generate the entire Gradio code based on the provided image."""
58
  stream=False
59
  )
60
 
61
- # Debug: Print the entire response
62
- print("API Response:", json.dumps(response, indent=2))
 
63
 
64
  # Extract the generated code from the response
65
  if response.choices and response.choices[0].message:
@@ -78,8 +77,8 @@ Please generate the entire Gradio code based on the provided image."""
78
  return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease check your API key and try again."
79
 
80
  with gr.Blocks() as demo:
81
- gr.Markdown("# Generate Gradio App from Wireframe")
82
- gr.Markdown("Enter your Together API key, upload an image of your UI design, and we'll generate Gradio code to recreate it.")
83
 
84
  api_key_input = gr.Textbox(label="Enter your Together API Key", type="password")
85
 
 
5
  from PIL import Image
6
  import numpy as np
7
  import traceback
 
8
 
9
  def generate_gradio_app(api_key, image):
10
  if not api_key:
 
24
  img_str = base64.b64encode(buffered.getvalue()).decode()
25
 
26
  # Prepare the prompt
27
+ 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 concise Gradio code that recreates the main elements of this design. Follow these steps:
28
 
29
+ 1. Briefly describe the main elements of the UI.
30
+ 2. Generate a compact Gradio Python code that recreates the essential UI elements.
31
+ 3. Use appropriate Gradio components for key elements in the UI.
32
+ 4. Include necessary imports at the beginning of the code.
33
+ 5. Implement minimal placeholder functions for interactive elements.
34
+ 6. Use gr.Blocks() to create a basic layout that captures the essence of the screenshot.
35
+ 7. Include the gr.Blocks().launch() call at the end of the code.
36
+ 8. Provide a runnable Gradio application focusing on the most important aspects of the UI.
37
+ 9. Keep the code concise, aiming for no more than 1000 tokens.
 
38
 
39
+ Please generate the Gradio code based on the provided image, focusing on the most crucial elements to fit within the token limit."""
40
 
41
  # Make the API call with the corrected message format
42
  response = client.chat.completions.create(
 
47
  "content": prompt + f"\n\n<image>\ndata:image/png;base64,{img_str}\n</image>"
48
  }
49
  ],
50
+ max_tokens=1000,
51
  temperature=0.7,
52
  top_p=0.7,
53
  top_k=50,
 
56
  stream=False
57
  )
58
 
59
+ # Debug: Print relevant parts of the response
60
+ print("API Response Status:", response.status_code)
61
+ print("API Response Choices:", response.choices)
62
 
63
  # Extract the generated code from the response
64
  if response.choices and response.choices[0].message:
 
77
  return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease check your API key and try again."
78
 
79
  with gr.Blocks() as demo:
80
+ gr.Markdown("# Generate Concise Gradio App from Wireframe")
81
+ gr.Markdown("Enter your Together API key, upload an image of your UI design, and we'll generate a compact Gradio code to recreate its main elements.")
82
 
83
  api_key_input = gr.Textbox(label="Enter your Together API Key", type="password")
84