Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,24 @@ 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 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
def analyze_image(image, instruction):
|
| 26 |
"""Analyzes the image using Groq's Llama 3.2 based on the instruction provided."""
|
|
@@ -35,13 +36,10 @@ def analyze_image(image, instruction):
|
|
| 35 |
# Call the Groq API to analyze the image
|
| 36 |
completion = client.chat.completions.create(
|
| 37 |
model="llama-3.2-90b-vision-preview",
|
| 38 |
-
messages=[
|
| 39 |
-
"role": "user",
|
| 40 |
-
"content":
|
| 41 |
-
|
| 42 |
-
{"type": "image_url", "image_url": {"url": image_url}}
|
| 43 |
-
]
|
| 44 |
-
}],
|
| 45 |
temperature=1,
|
| 46 |
max_tokens=1024,
|
| 47 |
top_p=1,
|
|
@@ -50,7 +48,7 @@ def analyze_image(image, instruction):
|
|
| 50 |
)
|
| 51 |
|
| 52 |
# Extract and return the response
|
| 53 |
-
analysis = completion.choices[0].message.content
|
| 54 |
return analysis
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error: {str(e)}"
|
|
@@ -64,9 +62,9 @@ iface = gr.Interface(
|
|
| 64 |
],
|
| 65 |
outputs="text",
|
| 66 |
title="Deep Image Analysis using LLM",
|
| 67 |
-
description="Upload an image and provide instructions to analyze the image using Llama 3.2 90B Vision. You can upload and
|
| 68 |
live=False,
|
| 69 |
)
|
| 70 |
|
| 71 |
# Launch the app
|
| 72 |
-
iface.launch()
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
|
| 6 |
# Initialize Groq client
|
| 7 |
+
visionkey = os.getenv("GroqVision") # Replace with your actual Groq API key if not set in the environment
|
| 8 |
client = Groq(api_key=visionkey)
|
| 9 |
|
| 10 |
# Your imgbb API key
|
| 11 |
+
imagekey = os.getenv("ImageAPI") # Replace with your actual imgbb API key if not set in the environment
|
| 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 |
+
try:
|
| 17 |
+
url = f"https://api.imgbb.com/1/upload?key={IMGBB_API_KEY}"
|
| 18 |
+
with open(image_path, "rb") as image_file:
|
| 19 |
+
files = {"image": image_file} # Corrected: Use 'files' instead of 'data'
|
| 20 |
+
response = requests.post(url, files=files, timeout=30) # Added timeout for better control
|
| 21 |
+
response.raise_for_status() # Raises an error for HTTP codes 4xx/5xx
|
| 22 |
+
return response.json()["data"]["url"]
|
| 23 |
+
except requests.exceptions.RequestException as e:
|
| 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."""
|
|
|
|
| 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,
|
| 45 |
top_p=1,
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
# Extract and return the response
|
| 51 |
+
analysis = completion.choices[0].message.content
|
| 52 |
return analysis
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error: {str(e)}"
|
|
|
|
| 62 |
],
|
| 63 |
outputs="text",
|
| 64 |
title="Deep Image Analysis using LLM",
|
| 65 |
+
description="Upload an image and provide instructions to analyze the image using Llama 3.2 90B Vision. You can upload and analyze as many pictures as possible with no restrictions. However, it's one at a time.",
|
| 66 |
live=False,
|
| 67 |
)
|
| 68 |
|
| 69 |
# Launch the app
|
| 70 |
+
iface.launch()
|