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