Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,43 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
-
import openai
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
# Set your API keys (replace with your actual keys or environment variables)
|
| 8 |
-
|
| 9 |
-
imagebb_api_key = os.getenv("ImageAPI") #
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
openai.api_base = "https://api.sambanova.ai/v1"
|
| 14 |
|
| 15 |
def upload_image_to_imgbb(image_path):
|
| 16 |
-
"""Uploads an image to
|
| 17 |
url = f"https://api.imgbb.com/1/upload?key={imagebb_api_key}"
|
| 18 |
with open(image_path, "rb") as image_file:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
raise ValueError(f"Image upload failed: {response.json()}")
|
| 25 |
|
| 26 |
def analyze_image(image, instruction):
|
| 27 |
-
"""Analyzes the image using
|
| 28 |
try:
|
| 29 |
-
# Save
|
| 30 |
image_path = "uploaded_image.png"
|
| 31 |
image.save(image_path)
|
| 32 |
|
| 33 |
-
# Upload
|
| 34 |
image_url = upload_image_to_imgbb(image_path)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
{
|
| 44 |
"role": "user",
|
| 45 |
"content": [
|
|
@@ -48,18 +46,23 @@ def analyze_image(image, instruction):
|
|
| 48 |
]
|
| 49 |
}
|
| 50 |
],
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
return f"Error:"
|
| 62 |
-
|
| 63 |
# Gradio interface
|
| 64 |
iface = gr.Interface(
|
| 65 |
fn=analyze_image,
|
|
@@ -70,9 +73,8 @@ iface = gr.Interface(
|
|
| 70 |
outputs="text",
|
| 71 |
title="Deep Image Analysis using LLM",
|
| 72 |
description=(
|
| 73 |
-
"Upload an image and provide instructions to analyze
|
| 74 |
-
"You can upload and analyze
|
| 75 |
-
"However, it's one at a time."
|
| 76 |
),
|
| 77 |
live=False,
|
| 78 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
+
# Set your API keys (replace with your actual keys or use environment variables)
|
| 7 |
+
nvidia_api_key = os.getenv("Vision") # NVIDIA API Key
|
| 8 |
+
imagebb_api_key = os.getenv("ImageAPI") # Imgbb API Key
|
| 9 |
|
| 10 |
+
# NVIDIA API Endpoint
|
| 11 |
+
invoke_url = "https://ai.api.nvidia.com/v1/gr/meta/llama-3.2-90b-vision-instruct/chat/completions"
|
|
|
|
| 12 |
|
| 13 |
def upload_image_to_imgbb(image_path):
|
| 14 |
+
"""Uploads an image to ImgBB and returns the URL."""
|
| 15 |
url = f"https://api.imgbb.com/1/upload?key={imagebb_api_key}"
|
| 16 |
with open(image_path, "rb") as image_file:
|
| 17 |
+
response = requests.post(url, files={"image": image_file})
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
return response.json()["data"]["url"]
|
| 20 |
+
else:
|
| 21 |
+
raise ValueError(f"Image upload failed: {response.json()}")
|
|
|
|
| 22 |
|
| 23 |
def analyze_image(image, instruction):
|
| 24 |
+
"""Analyzes the image using NVIDIA’s Llama 3.2 Vision Instruct model based on the provided instruction."""
|
| 25 |
try:
|
| 26 |
+
# Save image locally
|
| 27 |
image_path = "uploaded_image.png"
|
| 28 |
image.save(image_path)
|
| 29 |
|
| 30 |
+
# Upload image to ImgBB and get the URL
|
| 31 |
image_url = upload_image_to_imgbb(image_path)
|
| 32 |
|
| 33 |
+
# NVIDIA API Request
|
| 34 |
+
headers = {
|
| 35 |
+
"Authorization": f"Bearer {nvidia_api_key}",
|
| 36 |
+
"Accept": "application/json"
|
| 37 |
+
}
|
| 38 |
+
payload = {
|
| 39 |
+
"model": "meta/llama-3.2-90b-vision-instruct",
|
| 40 |
+
"messages": [
|
| 41 |
{
|
| 42 |
"role": "user",
|
| 43 |
"content": [
|
|
|
|
| 46 |
]
|
| 47 |
}
|
| 48 |
],
|
| 49 |
+
"max_tokens": 512,
|
| 50 |
+
"temperature": 0.1,
|
| 51 |
+
"top_p": 0.1
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
response = requests.post(invoke_url, headers=headers, json=payload)
|
| 55 |
+
response_data = response.json()
|
| 56 |
+
|
| 57 |
+
# Extract the response
|
| 58 |
+
if "choices" in response_data:
|
| 59 |
+
return response_data["choices"][0]["message"]["content"]
|
| 60 |
+
else:
|
| 61 |
+
return f"Error in response: {response_data}"
|
| 62 |
+
|
| 63 |
except Exception as e:
|
| 64 |
+
return f"Error: {str(e)}"
|
| 65 |
+
|
| 66 |
# Gradio interface
|
| 67 |
iface = gr.Interface(
|
| 68 |
fn=analyze_image,
|
|
|
|
| 73 |
outputs="text",
|
| 74 |
title="Deep Image Analysis using LLM",
|
| 75 |
description=(
|
| 76 |
+
"Upload an image and provide instructions to analyze it using Llama 3.2 90B Vision. "
|
| 77 |
+
"You can upload and analyze multiple pictures, but one at a time."
|
|
|
|
| 78 |
),
|
| 79 |
live=False,
|
| 80 |
)
|