akhaliq HF staff commited on
Commit
3691ca2
1 Parent(s): 487ff70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -36
app.py CHANGED
@@ -6,46 +6,51 @@ 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",
@@ -54,58 +59,92 @@ def get_image_description(api_key, uploaded_image):
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
- # Removed the 'tool' parameter to ensure compatibility
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
 
 
6
 
7
  def encode_image(img):
8
  """
9
+ Encodes a PIL Image to a base64 string in PNG format.
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_api_response(api_key, user_message, user_image):
17
  """
18
+ Sends the user message and image to the Hyperbolic API and retrieves the response.
19
  """
20
  if not api_key:
21
  return {"error": "API key is required."}
22
 
23
+ if not user_message and not user_image:
24
+ return {"error": "Please provide a text message, an image, or both."}
25
 
26
  try:
 
 
 
 
 
 
27
  headers = {
28
  "Content-Type": "application/json",
29
  "Authorization": f"Bearer {api_key}",
30
  }
31
 
32
+ messages = []
33
+
34
+ if user_message:
35
+ messages.append({
36
+ "type": "text",
37
+ "text": user_message
38
+ })
39
+
40
+ if user_image:
41
+ # Open the uploaded image
42
+ img = Image.open(user_image)
43
+ base64_img = encode_image(img)
44
+ messages.append({
45
+ "type": "image_url",
46
+ "image_url": {"url": f"data:image/png;base64,{base64_img}"}
47
+ })
48
+
49
  payload = {
50
  "messages": [
51
  {
52
  "role": "user",
53
+ "content": messages,
 
 
 
 
 
 
54
  }
55
  ],
56
  "model": "Qwen/Qwen2-VL-72B-Instruct",
 
59
  "top_p": 0.9,
60
  }
61
 
62
+ api_endpoint = "https://api.hyperbolic.xyz/v1/chat/completions"
63
  response = requests.post(api_endpoint, headers=headers, json=payload)
64
 
65
  # Check if the request was successful
66
  if response.status_code == 200:
67
+ api_response = response.json()
68
+ # Extract the AI's reply (assuming the response structure)
69
+ ai_reply = api_response.get("choices", [{}])[0].get("message", {}).get("content", "No response content.")
70
+ return {"response": ai_reply}
71
  else:
72
  return {"error": f"API Error: {response.status_code} - {response.text}"}
73
 
74
  except Exception as e:
75
  return {"error": str(e)}
76
 
77
+ def chatbot_response(api_key, user_message, user_image, history):
78
+ """
79
+ Handles the chatbot interaction by updating the conversation history.
80
+ """
81
+ # Append the user's message to the history
82
+ if user_message or user_image:
83
+ history.append((user_message, user_image))
84
+
85
+ # Get the API response
86
+ api_result = get_api_response(api_key, user_message, user_image)
87
+
88
+ if "error" in api_result:
89
+ ai_message = f"Error: {api_result['error']}"
90
+ else:
91
+ ai_message = api_result["response"]
92
+
93
+ # Append the AI's response to the history
94
+ history.append((ai_message, None))
95
+
96
+ return history, history
97
+
98
  # Define the Gradio interface
99
  with gr.Blocks() as demo:
100
  gr.Markdown(
101
  """
102
+ # 🖼️ Image Description Chatbot with Hyperbolic API
103
 
104
+ Engage in a conversation with the AI by sending text messages and/or uploading images. Enter your Hyperbolic API key to get started.
105
  """
106
  )
107
 
108
  with gr.Row():
109
  api_key_input = gr.Textbox(
110
+ label="🔑 Hyperbolic API Key",
111
  type="password",
112
  placeholder="Enter your API key here",
113
  interactive=True
114
  )
115
 
116
+ chatbot = gr.Chatbot(label="💬 Chatbot").style(height=500)
 
 
 
 
 
117
 
118
  with gr.Row():
119
+ with gr.Column(scale=4):
120
+ user_text = gr.Textbox(
121
+ label="Your Message",
122
+ placeholder="Type your message here...",
123
+ lines=1
124
+ )
125
+ with gr.Column(scale=1):
126
+ user_image = gr.Image(
127
+ label="Upload Image",
128
+ type="file",
129
+ tool="editor",
130
+ interactive=True
131
+ )
132
+
133
+ send_button = gr.Button("📤 Send")
134
 
135
+ # Hidden state to keep track of the conversation history
136
+ state = gr.State([])
137
 
138
+ send_button.click(
139
+ fn=chatbot_response,
140
+ inputs=[api_key_input, user_text, user_image, state],
141
+ outputs=[chatbot, state]
 
142
  )
143
 
144
  gr.Markdown(
145
  """
146
  ---
147
+ **Note:** Your API key is used only for this session and is not stored. Ensure you trust the environment in which you're running this application.
148
  """
149
  )
150