akhaliq HF staff commited on
Commit
4d26ed5
1 Parent(s): c190a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -87
app.py CHANGED
@@ -1,100 +1,85 @@
 
1
  import gradio as gr
2
  from together import Together
3
- import base64
4
- from io import BytesIO
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:
11
- return "Error: API key not provided. Please enter your Together API key."
12
 
13
- try:
14
- # Initialize the Together client with the provided API key
15
- client = Together(api_key=api_key)
16
 
17
- # Convert numpy array to PIL Image
18
- if isinstance(image, np.ndarray):
19
- image = Image.fromarray(image.astype('uint8'), 'RGB')
20
-
21
- # Convert the image to base64
22
- buffered = BytesIO()
23
- image.save(buffered, format="PNG")
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 2000 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(
43
- model="meta-llama/Llama-3.2-90B-Vision-Instruct-Turbo",
44
- messages=[
45
- {
46
- "role": "user",
47
- "content": prompt + f"\n\n<image>\ndata:image/png;base64,{img_str}\n</image>"
48
- }
49
- ],
50
- max_tokens=2000,
51
- temperature=0.7,
52
- top_p=0.7,
53
- top_k=50,
54
- repetition_penalty=1,
55
- stop=["<|eot_id|>", "<|eom_id|>"],
56
- stream=False
57
- )
58
-
59
- # Debug: Print relevant parts of the response
60
- print("API Response received. Content available:", bool(response.choices))
61
 
62
- # Extract the generated code from the response
63
- if response.choices and response.choices[0].message:
64
- generated_code = response.choices[0].message.content
65
- print("Generated code length:", len(generated_code))
66
- else:
67
- return "Error: Unexpected response structure from the API."
68
-
69
- if not generated_code:
70
- return "Error: No code generated from the model. Please try again."
71
-
72
- return generated_code
73
 
74
- except Exception as e:
75
- error_message = str(e)
76
- stack_trace = traceback.format_exc()
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
-
85
- with gr.Row():
86
- with gr.Column(scale=1):
87
- image_input = gr.Image(label="Upload a screenshot", elem_id="image_upload")
88
- generate_button = gr.Button("Generate Gradio Code", variant="primary")
89
-
90
- with gr.Column(scale=2):
91
- code_output = gr.Code(language="python", label="Generated Gradio Code", lines=30)
92
-
93
- generate_button.click(
94
- fn=generate_gradio_app,
95
- inputs=[api_key_input, image_input],
96
- outputs=[code_output]
97
  )
 
98
 
99
- if __name__ == "__main__":
100
- demo.launch(debug=True)
 
1
+ import os
2
  import gradio as gr
3
  from together import Together
 
 
4
  from PIL import Image
5
+ import io
6
+ import base64
 
 
 
 
7
 
8
+ # Initialize the Together AI client
9
+ client = Together(api_key=os.environ.get('TOGETHER_API_KEY'))
 
10
 
11
+ def encode_image(image):
12
+ buffered = io.BytesIO()
13
+ image.save(buffered, format="PNG")
14
+ return base64.b64encode(buffered.getvalue()).decode('utf-8')
 
 
 
 
 
 
 
15
 
16
+ def chat_with_image(message, image, history):
17
+ # Encode the image
18
+ if image is not None:
19
+ encoded_image = encode_image(Image.open(image))
20
+ image_message = {
21
+ "role": "user",
22
+ "content": [
23
+ {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
24
+ {"type": "text", "text": message}
25
+ ]
26
+ }
27
+ else:
28
+ image_message = {"role": "user", "content": message}
29
+
30
+ # Prepare the messages
31
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
32
+ for human, assistant in history:
33
+ messages.append({"role": "user", "content": human})
34
+ messages.append({"role": "assistant", "content": assistant})
35
+ messages.append(image_message)
36
+
37
+ # Call the Together AI API
38
+ response = client.chat.completions.create(
39
+ model="meta-llama/Llama-Vision-Free",
40
+ messages=messages,
41
+ max_tokens=512,
42
+ temperature=0.7,
43
+ top_p=0.7,
44
+ top_k=50,
45
+ repetition_penalty=1,
46
+ stop=["<|eot_id|>", "<|eom_id|>"],
47
+ stream=True
48
+ )
49
+
50
+ # Accumulate the response
51
+ full_response = ""
52
+ for chunk in response:
53
+ if chunk.choices[0].delta.content is not None:
54
+ full_response += chunk.choices[0].delta.content
55
+ yield full_response
56
 
57
+ return full_response
58
 
59
+ # Create the Gradio interface
60
+ with gr.Blocks() as demo:
61
+ chatbot = gr.Chatbot()
62
+ msg = gr.Textbox()
63
+ image = gr.Image(type="filepath")
64
+ clear = gr.Button("Clear")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ def user(user_message, image, history):
67
+ return "", image, history + [[user_message, None]]
 
 
 
 
 
 
 
 
 
68
 
69
+ def bot(history):
70
+ user_message, image = history[-1][0], None
71
+ if len(history) > 1 and isinstance(history[-2][0], dict):
72
+ image = history[-2][0]['image']
73
+ bot_message = chat_with_image(user_message, image, history[:-1])
74
+ history[-1][1] = ""
75
+ for character in bot_message:
76
+ history[-1][1] += character
77
+ yield history
78
 
79
+ msg.submit(user, [msg, image, chatbot], [msg, image, chatbot], queue=False).then(
80
+ bot, chatbot, chatbot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  )
82
+ clear.click(lambda: None, None, chatbot, queue=False)
83
 
84
+ demo.queue()
85
+ demo.launch()