Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from groq import Groq
|
3 |
+
import base64
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Encode image to base64
|
8 |
+
def encode_image_to_base64(image):
|
9 |
+
buffered = io.BytesIO()
|
10 |
+
image.save(buffered, format="JPEG")
|
11 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
12 |
+
|
13 |
+
# Prediction function with API key input
|
14 |
+
def predict(api_key, image, user_prompt):
|
15 |
+
if not api_key:
|
16 |
+
return "❌ Please provide your Groq API key."
|
17 |
+
|
18 |
+
try:
|
19 |
+
client = Groq(api_key=api_key)
|
20 |
+
base64_image = encode_image_to_base64(image)
|
21 |
+
|
22 |
+
response = client.chat.completions.create(
|
23 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
24 |
+
messages=[
|
25 |
+
{
|
26 |
+
"role": "user",
|
27 |
+
"content": [
|
28 |
+
{"type": "text", "text": user_prompt},
|
29 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
30 |
+
]
|
31 |
+
}
|
32 |
+
],
|
33 |
+
max_tokens=1024,
|
34 |
+
)
|
35 |
+
return response.choices[0].message.content
|
36 |
+
|
37 |
+
except Exception as e:
|
38 |
+
return f"⚠️ Error: {str(e)}"
|
39 |
+
|
40 |
+
# Gradio UI
|
41 |
+
demo = gr.Interface(
|
42 |
+
fn=predict,
|
43 |
+
inputs=[
|
44 |
+
gr.Textbox(label="Groq API Key", placeholder="Enter your API key...", type="password"),
|
45 |
+
gr.Image(type="pil", label="Upload Image"),
|
46 |
+
gr.Textbox(label="Enter Prompt", lines=10, placeholder="Paste your prompt here...")
|
47 |
+
],
|
48 |
+
outputs=gr.Markdown(label="Output"),
|
49 |
+
title="Custom Visual Prompt with Groq",
|
50 |
+
description="Upload an image, enter your Groq API key, and provide a prompt to analyze the image using LLaMA 4 Scout.",
|
51 |
+
)
|
52 |
+
|
53 |
+
demo.launch()
|