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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -52
app.py CHANGED
@@ -1,59 +1,56 @@
1
- import os
2
  import gradio as gr
3
- from together import Together
4
- import base64
5
 
6
- # Initialize the Together client
7
- client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))
 
 
 
 
 
8
 
9
- def process_image(image):
10
- # Convert the image to base64
11
- buffered = BytesIO()
12
- image.save(buffered, format="PNG")
13
- img_str = base64.b64encode(buffered.getvalue()).decode()
14
-
15
- # Prepare the messages for the API call
16
- messages = [
17
- {"role": "system", "content": "You are an AI assistant that can analyze images and generate code based on their content."},
18
- {"role": "user", "content": [
19
- {"type": "image_url", "image_url": f"data:image/png;base64,{img_str}"},
20
- {"type": "text", "text": "Analyze this image and generate Python code that could recreate or represent the main elements seen in the image."}
21
- ]}
22
- ]
23
-
24
- # Make the API call
25
- response = client.chat.completions.create(
26
- model="meta-llama/Llama-Vision-Free",
27
- messages=messages,
28
- max_tokens=512,
29
- temperature=0.7,
30
- top_p=0.7,
31
- top_k=50,
32
- repetition_penalty=1,
33
- stop=["<|eot_id|>", "<|eom_id|>"]
34
- )
35
 
36
- # Extract the generated code from the response
37
- generated_code = response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Generate HTML to display the code with syntax highlighting
40
- html_output = f"""
41
- <pre><code class="language-python">{generated_code}</code></pre>
42
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
43
- <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
44
- <script>hljs.highlightAll();</script>
45
- """
46
 
47
- return html_output
48
-
49
- # Create the Gradio interface
50
- iface = gr.Interface(
51
- fn=process_image,
52
- inputs=gr.Image(type="pil"),
53
- outputs=gr.HTML(),
54
- title="Llama Vision Free Code Generation",
55
- description="Upload an image, and this demo will use the Llama Vision Free model to analyze it and generate relevant Python code."
56
- )
57
 
58
- # Launch the interface
59
- iface.launch()
 
 
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()