Spaces:
Sleeping
Sleeping
Agrega capacidad de carga de imágenes a la interfaz de chat en `app.py`. Se implementa la función `encode_image` para convertir imágenes PIL a cadenas base64. La función `respond` se actualiza para manejar mensajes de texto e imágenes, y se crea una nueva función `create_chat_interface` para estructurar la interfaz de usuario con opciones de carga de imágenes.
Browse files
app.py
CHANGED
|
@@ -1,11 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
| 7 |
history: list[dict[str, str]],
|
| 8 |
-
|
| 9 |
):
|
| 10 |
"""
|
| 11 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
@@ -16,13 +26,47 @@ def respond(
|
|
| 16 |
temperature = 0.7
|
| 17 |
top_p = 0.95
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
|
| 23 |
-
messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
response = ""
|
| 28 |
|
|
@@ -42,18 +86,109 @@ def respond(
|
|
| 42 |
yield response
|
| 43 |
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.LoginButton()
|
| 56 |
-
chatbot.render()
|
| 57 |
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import base64
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def encode_image(pil_image):
|
| 9 |
+
"""Convert PIL image to base64 string"""
|
| 10 |
+
buffered = io.BytesIO()
|
| 11 |
+
pil_image.save(buffered, format="PNG")
|
| 12 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 13 |
|
| 14 |
|
| 15 |
def respond(
|
| 16 |
message,
|
| 17 |
history: list[dict[str, str]],
|
| 18 |
+
images=None,
|
| 19 |
):
|
| 20 |
"""
|
| 21 |
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
|
|
|
| 26 |
temperature = 0.7
|
| 27 |
top_p = 0.95
|
| 28 |
|
| 29 |
+
# Try to get the token from environment variable first, then from current request
|
| 30 |
+
import os
|
| 31 |
+
token = os.environ.get('HF_TOKEN') or os.environ.get('HUGGINGFACE_API_TOKEN')
|
| 32 |
+
|
| 33 |
+
if not token:
|
| 34 |
+
# Try to get from current Gradio context
|
| 35 |
+
try:
|
| 36 |
+
import gradio as gr
|
| 37 |
+
token = gr.get_current_token()
|
| 38 |
+
except:
|
| 39 |
+
pass
|
| 40 |
+
|
| 41 |
+
if not token:
|
| 42 |
+
raise gr.Error("Please log in with HuggingFace to use this chatbot. Click the login button in the sidebar.")
|
| 43 |
+
|
| 44 |
+
client = InferenceClient(token=token, model="openbmb/MiniCPM-V-4_5")
|
| 45 |
|
| 46 |
messages = [{"role": "system", "content": system_message}]
|
| 47 |
|
| 48 |
+
# Convert history to messages format
|
| 49 |
+
for msg in history:
|
| 50 |
+
if isinstance(msg, dict):
|
| 51 |
+
messages.append(msg)
|
| 52 |
+
|
| 53 |
+
# Prepare user content - can include both text and images
|
| 54 |
+
user_content = []
|
| 55 |
|
| 56 |
+
# Add images if provided
|
| 57 |
+
if images:
|
| 58 |
+
for img in images:
|
| 59 |
+
if isinstance(img, dict) and 'path' in img:
|
| 60 |
+
# Handle file uploads from gallery
|
| 61 |
+
pil_img = Image.open(img['path']).convert("RGB")
|
| 62 |
+
base64_image = encode_image(pil_img)
|
| 63 |
+
user_content.append({"image": f"data:image/png;base64,{base64_image}"})
|
| 64 |
+
|
| 65 |
+
# Add text message
|
| 66 |
+
if message:
|
| 67 |
+
user_content.append({"text": message})
|
| 68 |
+
|
| 69 |
+
messages.append({"role": "user", "content": user_content})
|
| 70 |
|
| 71 |
response = ""
|
| 72 |
|
|
|
|
| 86 |
yield response
|
| 87 |
|
| 88 |
|
| 89 |
+
def create_chat_interface():
|
| 90 |
+
"""Create a custom chat interface with image upload capability"""
|
| 91 |
+
with gr.Blocks() as demo:
|
| 92 |
+
with gr.Sidebar():
|
| 93 |
+
gr.LoginButton()
|
| 94 |
+
# OAuth token will be passed automatically through the request context
|
| 95 |
+
|
| 96 |
+
gr.Markdown("# 🤖 Chat with Images")
|
| 97 |
+
gr.Markdown("Upload images and ask questions about them!")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
with gr.Column(scale=2):
|
| 101 |
+
# Image upload area
|
| 102 |
+
uploaded_images = gr.Gallery(
|
| 103 |
+
label="📸 Upload Images",
|
| 104 |
+
show_label=True,
|
| 105 |
+
columns=3,
|
| 106 |
+
rows=2,
|
| 107 |
+
height="300px",
|
| 108 |
+
allow_preview=True,
|
| 109 |
+
interactive=True
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Chat message input
|
| 113 |
+
message_input = gr.Textbox(
|
| 114 |
+
label="💬 Your Message",
|
| 115 |
+
placeholder="Ask me anything about the images or just chat...",
|
| 116 |
+
lines=3,
|
| 117 |
+
show_label=True
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
with gr.Column(scale=3):
|
| 121 |
+
# Chat display
|
| 122 |
+
chatbot = gr.Chatbot(
|
| 123 |
+
label="💬 Chat",
|
| 124 |
+
height="500px",
|
| 125 |
+
show_label=True
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
with gr.Row():
|
| 129 |
+
submit_btn = gr.Button("🚀 Send", variant="primary")
|
| 130 |
+
clear_btn = gr.Button("🗑️ Clear Chat")
|
| 131 |
+
|
| 132 |
+
# Store hf_token as state
|
| 133 |
+
hf_token_state = gr.State()
|
| 134 |
+
|
| 135 |
+
def update_token(token):
|
| 136 |
+
return token
|
| 137 |
+
|
| 138 |
+
# Handle message submission
|
| 139 |
+
def user_message(message, history, images):
|
| 140 |
+
if not message and not images:
|
| 141 |
+
return "", history, []
|
| 142 |
+
return "", history + [{"role": "user", "content": message}], images
|
| 143 |
+
|
| 144 |
+
def bot_response(message, history, images):
|
| 145 |
+
if not message and not images:
|
| 146 |
+
yield history, images
|
| 147 |
+
return
|
| 148 |
+
|
| 149 |
+
# Add user message to history
|
| 150 |
+
new_history = history + [{"role": "user", "content": message}]
|
| 151 |
+
|
| 152 |
+
# Get bot response
|
| 153 |
+
bot_message = ""
|
| 154 |
+
for partial_response in respond(message, history, images):
|
| 155 |
+
bot_message = partial_response
|
| 156 |
+
yield new_history + [{"role": "assistant", "content": bot_message}], images
|
| 157 |
+
|
| 158 |
+
# Event handlers
|
| 159 |
+
message_input.submit(
|
| 160 |
+
user_message,
|
| 161 |
+
[message_input, chatbot, uploaded_images],
|
| 162 |
+
[message_input, chatbot, uploaded_images],
|
| 163 |
+
queue=False
|
| 164 |
+
).then(
|
| 165 |
+
bot_response,
|
| 166 |
+
[message_input, chatbot, uploaded_images],
|
| 167 |
+
[chatbot, uploaded_images],
|
| 168 |
+
queue=True
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
submit_btn.click(
|
| 172 |
+
user_message,
|
| 173 |
+
[message_input, chatbot, uploaded_images],
|
| 174 |
+
[message_input, chatbot, uploaded_images],
|
| 175 |
+
queue=False
|
| 176 |
+
).then(
|
| 177 |
+
bot_response,
|
| 178 |
+
[message_input, chatbot, uploaded_images],
|
| 179 |
+
[chatbot, uploaded_images],
|
| 180 |
+
queue=True
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
clear_btn.click(
|
| 184 |
+
lambda: ([], []),
|
| 185 |
+
outputs=[chatbot, uploaded_images]
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
return demo
|
| 189 |
|
| 190 |
+
# Create the interface
|
| 191 |
+
demo = create_chat_interface()
|
|
|
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|