Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,24 +4,23 @@ import requests
|
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Initialize Groq client
|
| 7 |
-
visionkey = os.getenv("GroqVision") #
|
| 8 |
client = Groq(api_key=visionkey)
|
| 9 |
|
| 10 |
# Your imgbb API key
|
| 11 |
-
imagekey = os.getenv("ImageAPI") #
|
| 12 |
IMGBB_API_KEY = imagekey
|
| 13 |
|
| 14 |
def upload_image_to_imgbb(image_path):
|
| 15 |
"""Uploads an image to imgbb and returns the URL."""
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
response.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
raise ValueError(f"Image upload failed: {str(e)}")
|
| 25 |
|
| 26 |
def analyze_image(image, instruction):
|
| 27 |
"""Analyzes the image using Groq's Llama 3.2 based on the instruction provided."""
|
|
@@ -33,12 +32,15 @@ def analyze_image(image, instruction):
|
|
| 33 |
# Upload the image to imgbb
|
| 34 |
image_url = upload_image_to_imgbb(image_path)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
# Call the Groq API to analyze the image
|
| 37 |
completion = client.chat.completions.create(
|
| 38 |
model="llama-3.2-90b-vision-preview",
|
| 39 |
messages=[
|
| 40 |
-
{"role": "user", "content": instruction},
|
| 41 |
-
{"role": "user", "content": {"type": "image_url", "image_url": {"url": image_url}}},
|
| 42 |
],
|
| 43 |
temperature=1,
|
| 44 |
max_tokens=1024,
|
|
@@ -62,7 +64,11 @@ iface = gr.Interface(
|
|
| 62 |
],
|
| 63 |
outputs="text",
|
| 64 |
title="Deep Image Analysis using LLM",
|
| 65 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
live=False,
|
| 67 |
)
|
| 68 |
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Initialize Groq client
|
| 7 |
+
visionkey = os.getenv("GroqVision") # Your Groq Vision API key from environment variables
|
| 8 |
client = Groq(api_key=visionkey)
|
| 9 |
|
| 10 |
# Your imgbb API key
|
| 11 |
+
imagekey = os.getenv("ImageAPI") # Your imgbb API key from environment variables
|
| 12 |
IMGBB_API_KEY = imagekey
|
| 13 |
|
| 14 |
def upload_image_to_imgbb(image_path):
|
| 15 |
"""Uploads an image to imgbb and returns the URL."""
|
| 16 |
+
url = f"https://api.imgbb.com/1/upload?key={IMGBB_API_KEY}"
|
| 17 |
+
with open(image_path, "rb") as image_file:
|
| 18 |
+
files = {"image": image_file.read()}
|
| 19 |
+
response = requests.post(url, files=files)
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
return response.json()["data"]["url"]
|
| 22 |
+
else:
|
| 23 |
+
raise ValueError(f"Image upload failed: {response.json()}")
|
|
|
|
| 24 |
|
| 25 |
def analyze_image(image, instruction):
|
| 26 |
"""Analyzes the image using Groq's Llama 3.2 based on the instruction provided."""
|
|
|
|
| 32 |
# Upload the image to imgbb
|
| 33 |
image_url = upload_image_to_imgbb(image_path)
|
| 34 |
|
| 35 |
+
# Debug: Log the uploaded image URL
|
| 36 |
+
print(f"Uploaded Image URL: {image_url}")
|
| 37 |
+
|
| 38 |
# Call the Groq API to analyze the image
|
| 39 |
completion = client.chat.completions.create(
|
| 40 |
model="llama-3.2-90b-vision-preview",
|
| 41 |
messages=[
|
| 42 |
+
{"role": "user", "content": instruction}, # Text instruction
|
| 43 |
+
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": image_url}}]}, # Image
|
| 44 |
],
|
| 45 |
temperature=1,
|
| 46 |
max_tokens=1024,
|
|
|
|
| 64 |
],
|
| 65 |
outputs="text",
|
| 66 |
title="Deep Image Analysis using LLM",
|
| 67 |
+
description=(
|
| 68 |
+
"Upload an image and provide instructions to analyze the image using Llama 3.2 90B Vision. "
|
| 69 |
+
"You can upload and analyze as many pictures as possible with no restrictions. "
|
| 70 |
+
"However, it's one at a time."
|
| 71 |
+
),
|
| 72 |
live=False,
|
| 73 |
)
|
| 74 |
|