Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -13,14 +13,14 @@ def encode_image(img):
|
|
13 |
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
14 |
return encoded_string
|
15 |
|
16 |
-
def get_api_response(api_key,
|
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
|
24 |
return {"error": "Please provide a text message, an image, or both."}
|
25 |
|
26 |
try:
|
@@ -31,15 +31,15 @@ def get_api_response(api_key, user_message, user_image):
|
|
31 |
|
32 |
messages = []
|
33 |
|
34 |
-
if
|
35 |
messages.append({
|
36 |
"type": "text",
|
37 |
-
"text":
|
38 |
})
|
39 |
|
40 |
-
if
|
41 |
# Open the uploaded image (already a PIL Image)
|
42 |
-
base64_img = encode_image(
|
43 |
messages.append({
|
44 |
"type": "image_url",
|
45 |
"image_url": {"url": f"data:image/png;base64,{base64_img}"}
|
@@ -73,19 +73,19 @@ def get_api_response(api_key, user_message, user_image):
|
|
73 |
except Exception as e:
|
74 |
return {"error": str(e)}
|
75 |
|
76 |
-
def chatbot_response(api_key,
|
77 |
"""
|
78 |
Handles the chatbot interaction by updating the conversation history.
|
79 |
"""
|
|
|
|
|
|
|
80 |
# Append the user's message to the history
|
81 |
-
if
|
82 |
-
|
83 |
-
history.append(("User", user_message, user_image))
|
84 |
-
else:
|
85 |
-
history.append(("User", user_message, None))
|
86 |
|
87 |
# Get the API response
|
88 |
-
api_result = get_api_response(api_key,
|
89 |
|
90 |
if "error" in api_result:
|
91 |
ai_message = f"Error: {api_result['error']}"
|
@@ -118,19 +118,12 @@ with gr.Blocks() as demo:
|
|
118 |
chatbot = gr.Chatbot(label="π¬ Chatbot") # Removed `.style()` method
|
119 |
|
120 |
with gr.Row():
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
with gr.Column(scale=1):
|
128 |
-
user_image = gr.Image(
|
129 |
-
label="Upload Image",
|
130 |
-
type="pil", # Changed from 'file' to 'pil'
|
131 |
-
# Removed the 'tool' parameter
|
132 |
-
interactive=True
|
133 |
-
)
|
134 |
|
135 |
send_button = gr.Button("π€ Send")
|
136 |
|
@@ -139,7 +132,7 @@ with gr.Blocks() as demo:
|
|
139 |
|
140 |
send_button.click(
|
141 |
fn=chatbot_response,
|
142 |
-
inputs=[api_key_input,
|
143 |
outputs=[chatbot, state]
|
144 |
)
|
145 |
|
|
|
13 |
encoded_string = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
14 |
return encoded_string
|
15 |
|
16 |
+
def get_api_response(api_key, user_inputs):
|
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_inputs.get("text") and not user_inputs.get("image"):
|
24 |
return {"error": "Please provide a text message, an image, or both."}
|
25 |
|
26 |
try:
|
|
|
31 |
|
32 |
messages = []
|
33 |
|
34 |
+
if user_inputs.get("text"):
|
35 |
messages.append({
|
36 |
"type": "text",
|
37 |
+
"text": user_inputs["text"]
|
38 |
})
|
39 |
|
40 |
+
if user_inputs.get("image"):
|
41 |
# Open the uploaded image (already a PIL Image)
|
42 |
+
base64_img = encode_image(user_inputs["image"])
|
43 |
messages.append({
|
44 |
"type": "image_url",
|
45 |
"image_url": {"url": f"data:image/png;base64,{base64_img}"}
|
|
|
73 |
except Exception as e:
|
74 |
return {"error": str(e)}
|
75 |
|
76 |
+
def chatbot_response(api_key, user_inputs, history):
|
77 |
"""
|
78 |
Handles the chatbot interaction by updating the conversation history.
|
79 |
"""
|
80 |
+
user_text = user_inputs.get("text")
|
81 |
+
user_image = user_inputs.get("image")
|
82 |
+
|
83 |
# Append the user's message to the history
|
84 |
+
if user_text or user_image:
|
85 |
+
history.append(("User", user_text, user_image))
|
|
|
|
|
|
|
86 |
|
87 |
# Get the API response
|
88 |
+
api_result = get_api_response(api_key, user_inputs)
|
89 |
|
90 |
if "error" in api_result:
|
91 |
ai_message = f"Error: {api_result['error']}"
|
|
|
118 |
chatbot = gr.Chatbot(label="π¬ Chatbot") # Removed `.style()` method
|
119 |
|
120 |
with gr.Row():
|
121 |
+
chat_input = gr.MultimodalTextbox(
|
122 |
+
label="Your Input",
|
123 |
+
placeholder="Type your message and/or upload an image...",
|
124 |
+
file_count="multiple", # Allows multiple files if needed
|
125 |
+
interactive=True
|
126 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
send_button = gr.Button("π€ Send")
|
129 |
|
|
|
132 |
|
133 |
send_button.click(
|
134 |
fn=chatbot_response,
|
135 |
+
inputs=[api_key_input, chat_input, state],
|
136 |
outputs=[chatbot, state]
|
137 |
)
|
138 |
|