Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 7 |
-
import
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
client = Together(api_key=api_key)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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 |
-
|
| 63 |
-
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 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 |
-
|
| 100 |
-
|
|
|
|
| 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()
|