File size: 4,179 Bytes
7efd637
ca8dc25
 
 
4c02c40
 
a5d0818
7efd637
e98c6cb
9dc7fb7
e98c6cb
ca8dc25
 
e98c6cb
 
 
4c02c40
 
 
 
ca8dc25
 
 
 
 
5f58d32
cad972e
d107cdf
cad972e
 
 
 
 
 
 
 
c190a55
d107cdf
cad972e
9dc7fb7
2c271aa
3807c9a
d107cdf
5f58d32
 
 
2c271aa
5f58d32
 
c190a55
5f58d32
 
 
 
 
2c271aa
5f58d32
ca8dc25
cad972e
629fec2
3807c9a
 
 
 
629fec2
3807c9a
 
a5d0818
d107cdf
 
9dc7fb7
d107cdf
82ee039
ca8dc25
82ee039
a5d0818
e98c6cb
5e6f5c8
 
cad972e
 
e98c6cb
 
7efd637
5e6f5c8
de4d170
5e6f5c8
d107cdf
de4d170
 
d107cdf
7efd637
5e6f5c8
b13161d
e98c6cb
d107cdf
5e6f5c8
7efd637
a5d0818
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import gradio as gr
from together import Together
import base64
from io import BytesIO
from PIL import Image
import numpy as np
import traceback

def generate_gradio_app(api_key, image):
    if not api_key:
        return "Error: API key not provided. Please enter your Together API key."

    try:
        # Initialize the Together client with the provided API key
        client = Together(api_key=api_key)

        # Convert numpy array to PIL Image
        if isinstance(image, np.ndarray):
            image = Image.fromarray(image.astype('uint8'), 'RGB')
        
        # Convert the image to base64
        buffered = BytesIO()
        image.save(buffered, format="PNG")
        img_str = base64.b64encode(buffered.getvalue()).decode()
        
        # Prepare the prompt
        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:

1. Briefly describe the main elements of the UI.
2. Generate a compact Gradio Python code that recreates the essential UI elements.
3. Use appropriate Gradio components for key elements in the UI.
4. Include necessary imports at the beginning of the code.
5. Implement minimal placeholder functions for interactive elements.
6. Use gr.Blocks() to create a basic layout that captures the essence of the screenshot.
7. Include the gr.Blocks().launch() call at the end of the code.
8. Provide a runnable Gradio application focusing on the most important aspects of the UI.
9. Keep the code concise, aiming for no more than 2000 tokens.

Please generate the Gradio code based on the provided image, focusing on the most crucial elements to fit within the token limit."""

        # Make the API call with the corrected message format
        response = client.chat.completions.create(
            model="meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo",
            messages=[
                {
                    "role": "user",
                    "content": prompt + f"\n\n<image>\ndata:image/png;base64,{img_str}\n</image>"
                }
            ],
            max_tokens=2000,
            temperature=0.7,
            top_p=0.7,
            top_k=50,
            repetition_penalty=1,
            stop=["<|eot_id|>", "<|eom_id|>"],
            stream=False
        )
        
        # Debug: Print relevant parts of the response
        print("API Response received. Content available:", bool(response.choices))

        # Extract the generated code from the response
        if response.choices and response.choices[0].message:
            generated_code = response.choices[0].message.content
            print("Generated code length:", len(generated_code))
        else:
            return "Error: Unexpected response structure from the API."
        
        if not generated_code:
            return "Error: No code generated from the model. Please try again."
        
        return generated_code

    except Exception as e:
        error_message = str(e)
        stack_trace = traceback.format_exc()
        return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease check your API key and try again."

with gr.Blocks() as demo:
    gr.Markdown("# Generate Concise Gradio App from Wireframe")
    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.")
    
    api_key_input = gr.Textbox(label="Enter your Together API Key", type="password")
    
    with gr.Row():
        with gr.Column(scale=1):
            image_input = gr.Image(label="Upload a screenshot", elem_id="image_upload")
            generate_button = gr.Button("Generate Gradio Code", variant="primary")
        
        with gr.Column(scale=2):
            code_output = gr.Code(language="python", label="Generated Gradio Code", lines=30)
    
    generate_button.click(
        fn=generate_gradio_app,
        inputs=[api_key_input, image_input],
        outputs=[code_output]
    )

if __name__ == "__main__":
    demo.launch(debug=True)