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 os
|
| 4 |
+
|
| 5 |
+
# Initialize Groq client
|
| 6 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
+
|
| 8 |
+
def process_image_and_get_response(image, user_description):
|
| 9 |
+
"""
|
| 10 |
+
Process the uploaded image and user description, then get a response from Groq.
|
| 11 |
+
Since Groq's API doesn't support images, we use the description as input.
|
| 12 |
+
"""
|
| 13 |
+
if not user_description:
|
| 14 |
+
user_description = "Describe this image." # Placeholder for image-to-text
|
| 15 |
+
try:
|
| 16 |
+
completion = client.chat.completions.create(
|
| 17 |
+
model="meta-llama/llama-4-maverick-17b-128e-instruct",
|
| 18 |
+
messages=[
|
| 19 |
+
{
|
| 20 |
+
"role": "user",
|
| 21 |
+
"content": user_description
|
| 22 |
+
}
|
| 23 |
+
],
|
| 24 |
+
temperature=1,
|
| 25 |
+
max_completion_tokens=1024,
|
| 26 |
+
top_p=1,
|
| 27 |
+
stream=True,
|
| 28 |
+
stop=None
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Collect streamed response
|
| 32 |
+
response = ""
|
| 33 |
+
for chunk in completion:
|
| 34 |
+
response += chunk.choices[0].delta.content or ""
|
| 35 |
+
return response
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
| 38 |
+
|
| 39 |
+
# Define Gradio interface
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=process_image_and_get_response,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Image(type="pil", label="Upload an Image"),
|
| 44 |
+
gr.Textbox(label="Describe the image (optional)", placeholder="Enter a description or leave blank")
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.Textbox(label="Groq AI Response"),
|
| 47 |
+
title="Image-based Groq AI Chat",
|
| 48 |
+
description="Upload an image and optionally provide a description. The Groq AI will respond based on the description."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the interface
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
iface.launch()
|