Afeezee's picture
Update app.py
f8ffbef verified
raw
history blame
2.69 kB
import os
import gradio as gr
import requests
import openai
from PIL import Image
# Set your API keys (replace with your actual keys or environment variables)
sambanova_api_key = os.getenv("Vision") # Sambanova API Key
imagebb_api_key = os.getenv("ImageAPI") # Your imgbb API key from environment variables
# Initialize Sambanova OpenAI client
openai.api_key = sambanova_api_key
openai.api_base = "https://api.sambanova.ai/v1"
def upload_image_to_imgbb(image_path):
"""Uploads an image to imgbb and returns the URL."""
url = f"https://api.imgbb.com/1/upload?key={imagebb_api_key}"
with open(image_path, "rb") as image_file:
files = {"image": image_file.read()}
response = requests.post(url, files=files)
if response.status_code == 200:
return response.json()["data"]["url"]
else:
raise ValueError(f"Image upload failed: {response.json()}")
def analyze_image(image, instruction):
"""Analyzes the image using Sambanova's Llama 3.2 Vision Instruct based on the provided instruction."""
try:
# Save the uploaded image locally
image_path = "uploaded_image.png"
image.save(image_path)
# Upload the image to imgbb
image_url = upload_image_to_imgbb(image_path)
# Debug: Log the uploaded image URL
print(f"Uploaded Image URL: {image_url}")
# Call the Sambanova API to analyze the image
completion = openai.ChatCompletion.create(
model="Llama-3.2-90B-Vision-Instruct",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": instruction},
{"type": "image_url", "image_url": {"url": image_url}}
]
}
],
temperature=0.1,
top_p=0.1,
)
# Extract and return the response
analysis = completion.choices[0].message.content
return analysis
except Exception as e:
return f"Error: {str(e)}"
# Gradio interface
iface = gr.Interface(
fn=analyze_image,
inputs=[
gr.Image(type="pil", label="Upload Image"),
gr.Textbox(label="Instruction", placeholder="Enter your analysis instruction here.", lines=2)
],
outputs="text",
title="Deep Image Analysis using LLM",
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."
),
live=False,
)
# Launch the app
iface.launch()