akhaliq HF staff commited on
Commit
8e13475
1 Parent(s): 362fab5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import requests
3
+ from io import BytesIO
4
+ from PIL import Image
5
+ import gradio as gr
6
+
7
+ def encode_image(img):
8
+ """
9
+ Encodes a PIL Image to a base64 string.
10
+ """
11
+ buffered = BytesIO()
12
+ img.save(buffered, format="PNG")
13
+ encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
14
+ return encoded_string
15
+
16
+ def get_image_description(api_key, uploaded_image):
17
+ """
18
+ Sends the uploaded image and API key to the Hyperbolic API and retrieves the response.
19
+ """
20
+ if not api_key:
21
+ return {"error": "API key is required."}
22
+
23
+ if uploaded_image is None:
24
+ return {"error": "No image uploaded."}
25
+
26
+ try:
27
+ # Open the uploaded image
28
+ img = Image.open(uploaded_image)
29
+ base64_img = encode_image(img)
30
+
31
+ api_endpoint = "https://api.hyperbolic.xyz/v1/chat/completions"
32
+
33
+ headers = {
34
+ "Content-Type": "application/json",
35
+ "Authorization": f"Bearer {api_key}",
36
+ }
37
+
38
+ payload = {
39
+ "messages": [
40
+ {
41
+ "role": "user",
42
+ "content": [
43
+ {"type": "text", "text": "What is this image?"},
44
+ {
45
+ "type": "image_url",
46
+ "image_url": {"url": f"data:image/png;base64,{base64_img}"},
47
+ },
48
+ ],
49
+ }
50
+ ],
51
+ "model": "Qwen/Qwen2-VL-72B-Instruct",
52
+ "max_tokens": 2048,
53
+ "temperature": 0.7,
54
+ "top_p": 0.9,
55
+ }
56
+
57
+ response = requests.post(api_endpoint, headers=headers, json=payload)
58
+
59
+ # Check if the request was successful
60
+ if response.status_code == 200:
61
+ return response.json()
62
+ else:
63
+ return {"error": f"API Error: {response.status_code} - {response.text}"}
64
+
65
+ except Exception as e:
66
+ return {"error": str(e)}
67
+
68
+ # Define the Gradio interface
69
+ with gr.Blocks() as demo:
70
+ gr.Markdown(
71
+ """
72
+ # Image Description with Hyperbolic API
73
+
74
+ Upload an image and enter your Hyperbolic API key to get a description of the image.
75
+ """
76
+ )
77
+
78
+ with gr.Row():
79
+ api_key_input = gr.Textbox(
80
+ label="Hyperbolic API Key",
81
+ type="password",
82
+ placeholder="Enter your API key here",
83
+ interactive=True
84
+ )
85
+
86
+ with gr.Row():
87
+ image_input = gr.Image(
88
+ label="Upload Image",
89
+ type="filepath",
90
+ tool="editor"
91
+ )
92
+
93
+ with gr.Row():
94
+ submit_button = gr.Button("Get Description")
95
+
96
+ output = gr.JSON(label="API Response")
97
+
98
+ # Define the button click event
99
+ submit_button.click(
100
+ fn=get_image_description,
101
+ inputs=[api_key_input, image_input],
102
+ outputs=output
103
+ )
104
+
105
+ gr.Markdown(
106
+ """
107
+ ---
108
+ **Note:** Your API key is used only for this session and is not stored.
109
+ """
110
+ )
111
+
112
+ # Launch the Gradio app
113
+ if __name__ == "__main__":
114
+ demo.launch()