Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,20 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import os
|
4 |
|
5 |
hf_key = os.getenv("HF_KEY").strip()
|
6 |
# Initialize the Hugging Face client
|
7 |
client = InferenceClient(api_key=hf_key)
|
8 |
|
9 |
# Define the chatbot function
|
10 |
-
def chat_with_model(text,
|
|
|
11 |
messages = [{"role": "user", "content": {"type": "text", "text": text}}]
|
12 |
-
|
13 |
-
# If an image is uploaded, encode it and send it as part of the payload
|
14 |
-
if image:
|
15 |
-
import base64
|
16 |
-
from PIL import Image
|
17 |
-
from io import BytesIO
|
18 |
-
|
19 |
-
# Convert image to base64
|
20 |
-
buffered = BytesIO()
|
21 |
-
image.save(buffered, format="PNG")
|
22 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
23 |
-
|
24 |
-
# Add the image content to the messages
|
25 |
messages.append({
|
26 |
"role": "user",
|
27 |
"content": {
|
28 |
-
"type": "
|
29 |
-
"
|
30 |
}
|
31 |
})
|
32 |
|
@@ -44,11 +32,10 @@ ui = gr.Interface(
|
|
44 |
fn=chat_with_model,
|
45 |
inputs=[
|
46 |
gr.Textbox(label="Enter your message"),
|
47 |
-
gr.
|
48 |
],
|
49 |
outputs=gr.Textbox(label="Response from the chatbot"),
|
50 |
-
title="AI Chatbot with
|
51 |
-
theme="TejAndrewsACC/zetaofficalthemeacc"
|
52 |
)
|
53 |
|
54 |
# Launch the UI
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
hf_key = os.getenv("HF_KEY").strip()
|
5 |
# Initialize the Hugging Face client
|
6 |
client = InferenceClient(api_key=hf_key)
|
7 |
|
8 |
# Define the chatbot function
|
9 |
+
def chat_with_model(text, image_url=None):
|
10 |
+
# Prepare messages
|
11 |
messages = [{"role": "user", "content": {"type": "text", "text": text}}]
|
12 |
+
if image_url:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
messages.append({
|
14 |
"role": "user",
|
15 |
"content": {
|
16 |
+
"type": "image_url",
|
17 |
+
"image_url": {"url": image_url}
|
18 |
}
|
19 |
})
|
20 |
|
|
|
32 |
fn=chat_with_model,
|
33 |
inputs=[
|
34 |
gr.Textbox(label="Enter your message"),
|
35 |
+
gr.Textbox(label="Optional Image URL (Leave empty if not needed)")
|
36 |
],
|
37 |
outputs=gr.Textbox(label="Response from the chatbot"),
|
38 |
+
title="AI Chatbot with Image Processing"
|
|
|
39 |
)
|
40 |
|
41 |
# Launch the UI
|