akhaliq HF staff commited on
Commit
ca8dc25
1 Parent(s): 5e6f5c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -30
app.py CHANGED
@@ -1,56 +1,80 @@
1
  import gradio as gr
2
- import random
 
 
 
3
 
4
- def generate_app(image):
5
- # Placeholder function for app generation
6
- # In a real scenario, this would involve image analysis and code generation
7
- components = ["Header", "Sidebar", "MainContent", "Footer"]
8
- code = f"""
9
- import React from 'react';
10
- import {{ {', '.join(components)} }} from './components';
11
 
12
- function App() {{
13
- return (
14
- <div className="app">
15
- {' '.join(f'<{component} />' for component in components)}
16
- </div>
17
- );
18
- }}
19
 
20
- export default App;
21
- """
22
- return code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  with gr.Blocks() as demo:
25
  gr.Markdown("# Turn your wireframe into an app")
26
  gr.Markdown("Upload an image of your website design and we'll build it for you with React + Tailwind.")
27
 
28
  with gr.Row():
29
- with gr.Column(scale=2):
30
- with gr.Row():
31
- napkin_img = gr.Image(value="path/to/napkin_sketch.png", label="Napkin")
32
- react_img = gr.Image(value="path/to/react_app.png", label="React app")
33
-
34
- file_output = gr.File(label="Download React App")
35
-
36
  with gr.Column():
37
  image_input = gr.Image(label="Upload a screenshot", elem_id="image_upload")
38
- example_link = gr.Markdown("Need an example image? [Try ours](#).")
39
 
40
  model_dropdown = gr.Dropdown(
41
- choices=["Llama 3.2 90B Vision", "Other models..."],
42
- value="Llama 3.2 90B Vision",
43
  label="AI Model"
44
  )
45
 
46
  generate_button = gr.Button("Generate app", variant="primary")
47
 
48
- code_output = gr.Code(language="javascript", label="Generated React Code")
49
 
50
  generate_button.click(
51
  fn=generate_app,
52
  inputs=[image_input],
53
- outputs=[code_output, file_output]
54
  )
55
 
56
  demo.launch()
 
1
  import gradio as gr
2
+ import os
3
+ from together import Together
4
+ import base64
5
+ from io import BytesIO
6
 
7
+ # Initialize the Together client
8
+ api_key = os.environ.get('TOGETHER_API_KEY')
9
+ client = None
 
 
 
 
10
 
11
+ if api_key:
12
+ try:
13
+ client = Together(api_key=api_key)
14
+ except Exception as e:
15
+ print(f"Error initializing Together client: {e}")
 
 
16
 
17
+ def generate_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
+
21
+ try:
22
+ # Convert the image to base64
23
+ buffered = BytesIO()
24
+ image.save(buffered, format="PNG")
25
+ img_str = base64.b64encode(buffered.getvalue()).decode()
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 React 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 React code with Tailwind CSS classes that could recreate the main elements seen in the image."}
33
+ ]}
34
+ ]
35
+
36
+ # Make the API call
37
+ response = client.chat.completions.create(
38
+ model="meta-llama/Llama-2-70b-chat",
39
+ messages=messages,
40
+ max_tokens=1024,
41
+ temperature=0.7,
42
+ top_p=0.7,
43
+ top_k=50,
44
+ repetition_penalty=1,
45
+ stop=["<|im_end|>"]
46
+ )
47
+
48
+ # Extract the generated code from the response
49
+ generated_code = response.choices[0].message.content
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 an app")
57
  gr.Markdown("Upload an image of your website design and we'll build it for you with React + Tailwind.")
58
 
59
  with gr.Row():
 
 
 
 
 
 
 
60
  with gr.Column():
61
  image_input = gr.Image(label="Upload a screenshot", elem_id="image_upload")
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 2 70B Vision"],
66
+ value="Llama 2 70B Vision",
67
  label="AI Model"
68
  )
69
 
70
  generate_button = gr.Button("Generate app", variant="primary")
71
 
72
+ code_output = gr.Code(language="javascript", label="Generated React Code", lines=10)
73
 
74
  generate_button.click(
75
  fn=generate_app,
76
  inputs=[image_input],
77
+ outputs=[code_output]
78
  )
79
 
80
  demo.launch()