File size: 3,167 Bytes
8e13475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
487ff70
 
8e13475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import base64
import requests
from io import BytesIO
from PIL import Image
import gradio as gr

def encode_image(img):
    """
    Encodes a PIL Image to a base64 string.
    """
    buffered = BytesIO()
    img.save(buffered, format="PNG")
    encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
    return encoded_string

def get_image_description(api_key, uploaded_image):
    """
    Sends the uploaded image and API key to the Hyperbolic API and retrieves the response.
    """
    if not api_key:
        return {"error": "API key is required."}
    
    if uploaded_image is None:
        return {"error": "No image uploaded."}
    
    try:
        # Open the uploaded image
        img = Image.open(uploaded_image)
        base64_img = encode_image(img)
        
        api_endpoint = "https://api.hyperbolic.xyz/v1/chat/completions"
        
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {api_key}",
        }
        
        payload = {
            "messages": [
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": "What is this image?"},
                        {
                            "type": "image_url",
                            "image_url": {"url": f"data:image/png;base64,{base64_img}"},
                        },
                    ],
                }
            ],
            "model": "Qwen/Qwen2-VL-72B-Instruct",
            "max_tokens": 2048,
            "temperature": 0.7,
            "top_p": 0.9,
        }
        
        response = requests.post(api_endpoint, headers=headers, json=payload)
        
        # Check if the request was successful
        if response.status_code == 200:
            return response.json()
        else:
            return {"error": f"API Error: {response.status_code} - {response.text}"}
    
    except Exception as e:
        return {"error": str(e)}

# Define the Gradio interface
with gr.Blocks() as demo:
    gr.Markdown(
        """
        # Image Description with Hyperbolic API

        Upload an image and enter your Hyperbolic API key to get a description of the image.
        """
    )
    
    with gr.Row():
        api_key_input = gr.Textbox(
            label="Hyperbolic API Key",
            type="password",
            placeholder="Enter your API key here",
            interactive=True
        )
    
    with gr.Row():
        image_input = gr.Image(
            label="Upload Image",
            type="filepath"
            # Removed the 'tool' parameter to ensure compatibility
        )
    
    with gr.Row():
        submit_button = gr.Button("Get Description")
    
    output = gr.JSON(label="API Response")
    
    # Define the button click event
    submit_button.click(
        fn=get_image_description,
        inputs=[api_key_input, image_input],
        outputs=output
    )
    
    gr.Markdown(
        """
        ---
        **Note:** Your API key is used only for this session and is not stored.
        """
    )

# Launch the Gradio app
if __name__ == "__main__":
    demo.launch()