File size: 2,688 Bytes
82d3f8a
 
 
f8ffbef
 
82d3f8a
f8ffbef
 
 
82d3f8a
f8ffbef
 
 
82d3f8a
 
 
f8ffbef
e597996
 
 
 
 
 
 
82d3f8a
 
f8ffbef
82d3f8a
 
 
 
 
 
 
 
e597996
 
 
f8ffbef
 
 
 
 
 
 
 
 
 
 
 
 
 
82d3f8a
 
 
333e4fb
82d3f8a
 
 
 
 
 
 
 
 
 
 
 
 
e597996
 
 
 
 
82d3f8a
 
 
 
f8ffbef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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()