Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,7 @@ if api_key:
|
|
14 |
except Exception as e:
|
15 |
print(f"Error initializing Together client: {e}")
|
16 |
|
17 |
-
def
|
18 |
if not api_key or not client:
|
19 |
return "Error: TOGETHER_API_KEY not set or client initialization failed. Please check your API key."
|
20 |
|
@@ -26,35 +26,39 @@ def generate_app(image):
|
|
26 |
|
27 |
# Prepare the messages for the API call
|
28 |
messages = [
|
29 |
-
{"role": "system", "content": "You are an AI assistant that can analyze wireframe images and generate
|
30 |
{"role": "user", "content": [
|
31 |
{"type": "image_url", "image_url": f"data:image/png;base64,{img_str}"},
|
32 |
-
{"type": "text", "text": "Analyze this wireframe image and generate
|
33 |
]}
|
34 |
]
|
35 |
|
36 |
# Make the API call
|
37 |
response = client.chat.completions.create(
|
38 |
-
model="meta-llama/Llama-
|
39 |
messages=messages,
|
40 |
-
max_tokens=
|
41 |
temperature=0.7,
|
42 |
top_p=0.7,
|
43 |
top_k=50,
|
44 |
repetition_penalty=1,
|
45 |
-
stop=["<|
|
|
|
46 |
)
|
47 |
|
48 |
-
#
|
49 |
-
generated_code =
|
|
|
|
|
|
|
50 |
|
51 |
return generated_code
|
52 |
except Exception as e:
|
53 |
return f"An error occurred: {str(e)}"
|
54 |
|
55 |
with gr.Blocks() as demo:
|
56 |
-
gr.Markdown("# Turn your wireframe into
|
57 |
-
gr.Markdown("Upload an image of your
|
58 |
|
59 |
with gr.Row():
|
60 |
with gr.Column():
|
@@ -62,17 +66,17 @@ with gr.Blocks() as demo:
|
|
62 |
example_link = gr.Markdown("Need an example image? [Try ours](https://example.com/wireframe.png).")
|
63 |
|
64 |
model_dropdown = gr.Dropdown(
|
65 |
-
choices=["Llama
|
66 |
-
value="Llama
|
67 |
label="AI Model"
|
68 |
)
|
69 |
|
70 |
-
generate_button = gr.Button("Generate app", variant="primary")
|
71 |
|
72 |
-
code_output = gr.Code(language="
|
73 |
|
74 |
generate_button.click(
|
75 |
-
fn=
|
76 |
inputs=[image_input],
|
77 |
outputs=[code_output]
|
78 |
)
|
|
|
14 |
except Exception as e:
|
15 |
print(f"Error initializing Together client: {e}")
|
16 |
|
17 |
+
def generate_gradio_app(image):
|
18 |
if not api_key or not client:
|
19 |
return "Error: TOGETHER_API_KEY not set or client initialization failed. Please check your API key."
|
20 |
|
|
|
26 |
|
27 |
# Prepare the messages for the API call
|
28 |
messages = [
|
29 |
+
{"role": "system", "content": "You are an AI assistant that can analyze wireframe images and generate Gradio code based on their content."},
|
30 |
{"role": "user", "content": [
|
31 |
{"type": "image_url", "image_url": f"data:image/png;base64,{img_str}"},
|
32 |
+
{"type": "text", "text": "Analyze this wireframe image and generate Python code using Gradio that could recreate the main elements seen in the image. Use Gradio components that best represent the UI elements in the wireframe."}
|
33 |
]}
|
34 |
]
|
35 |
|
36 |
# Make the API call
|
37 |
response = client.chat.completions.create(
|
38 |
+
model="meta-llama/Llama-Vision-Free",
|
39 |
messages=messages,
|
40 |
+
max_tokens=512,
|
41 |
temperature=0.7,
|
42 |
top_p=0.7,
|
43 |
top_k=50,
|
44 |
repetition_penalty=1,
|
45 |
+
stop=["<|eot_id|>", "<|eom_id|>"],
|
46 |
+
stream=True
|
47 |
)
|
48 |
|
49 |
+
# Collect the streamed response
|
50 |
+
generated_code = ""
|
51 |
+
for chunk in response:
|
52 |
+
if chunk.choices[0].delta.content is not None:
|
53 |
+
generated_code += chunk.choices[0].delta.content
|
54 |
|
55 |
return generated_code
|
56 |
except Exception as e:
|
57 |
return f"An error occurred: {str(e)}"
|
58 |
|
59 |
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("# Turn your wireframe into a Gradio app")
|
61 |
+
gr.Markdown("Upload an image of your UI design and we'll build a Gradio app for you.")
|
62 |
|
63 |
with gr.Row():
|
64 |
with gr.Column():
|
|
|
66 |
example_link = gr.Markdown("Need an example image? [Try ours](https://example.com/wireframe.png).")
|
67 |
|
68 |
model_dropdown = gr.Dropdown(
|
69 |
+
choices=["Llama-Vision-Free"],
|
70 |
+
value="Llama-Vision-Free",
|
71 |
label="AI Model"
|
72 |
)
|
73 |
|
74 |
+
generate_button = gr.Button("Generate Gradio app", variant="primary")
|
75 |
|
76 |
+
code_output = gr.Code(language="python", label="Generated Gradio Code", lines=20)
|
77 |
|
78 |
generate_button.click(
|
79 |
+
fn=generate_gradio_app,
|
80 |
inputs=[image_input],
|
81 |
outputs=[code_output]
|
82 |
)
|