Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from deep_translator import GoogleTranslator
|
| 4 |
from indic_transliteration import sanscript
|
| 5 |
from indic_transliteration.detect import detect as detect_script
|
| 6 |
from indic_transliteration.sanscript import transliterate
|
| 7 |
import langdetect
|
| 8 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Initialize clients
|
| 11 |
-
text_client = InferenceClient(
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def detect_language_script(text: str) -> tuple[str, str]:
|
| 15 |
"""Detect language and script of the input text.
|
|
@@ -111,23 +127,16 @@ def is_image_request(message: str) -> bool:
|
|
| 111 |
message_lower = message.lower()
|
| 112 |
return any(trigger in message_lower for trigger in image_triggers)
|
| 113 |
|
| 114 |
-
def generate_image(prompt
|
| 115 |
-
"""Generate
|
| 116 |
try:
|
| 117 |
-
response =
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
"negative_prompt": "blurry, bad quality, nsfw",
|
| 121 |
-
"num_inference_steps": 30,
|
| 122 |
-
"guidance_scale": 7.5
|
| 123 |
-
}
|
| 124 |
-
)
|
| 125 |
-
# Save the image and return the path or base64 string
|
| 126 |
-
# Note: Implementation depends on how you want to handle the image output
|
| 127 |
-
return response
|
| 128 |
except Exception as e:
|
| 129 |
print(f"Image generation error: {e}")
|
| 130 |
return None
|
|
|
|
| 131 |
def romanized_to_bengali(text: str) -> str:
|
| 132 |
"""Convert romanized Bengali text to Bengali script."""
|
| 133 |
bengali_mappings = {
|
|
@@ -156,49 +165,167 @@ def romanized_to_bengali(text: str) -> str:
|
|
| 156 |
|
| 157 |
return text_lower
|
| 158 |
|
| 159 |
-
import gradio as gr
|
| 160 |
-
import time
|
| 161 |
-
from huggingface_hub import InferenceClient
|
| 162 |
-
from deep_translator import GoogleTranslator
|
| 163 |
-
from indic_transliteration import sanscript
|
| 164 |
-
from indic_transliteration.detect import detect as detect_script
|
| 165 |
-
from indic_transliteration.sanscript import transliterate
|
| 166 |
-
import langdetect
|
| 167 |
-
import re
|
| 168 |
|
| 169 |
-
# Initialize clients
|
| 170 |
-
text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 171 |
-
image_client = InferenceClient("SG161222/RealVisXL_V3.0")
|
| 172 |
|
| 173 |
def create_chat_interface():
|
| 174 |
-
# Custom CSS for better styling
|
| 175 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
.container {
|
| 177 |
max-width: 850px !important;
|
| 178 |
margin: auto;
|
| 179 |
}
|
|
|
|
| 180 |
.chat-window {
|
| 181 |
height: 600px !important;
|
| 182 |
overflow-y: auto;
|
| 183 |
border-radius: 15px !important;
|
| 184 |
-
box-shadow: 0
|
|
|
|
| 185 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
.chat-message {
|
| 187 |
padding: 1rem !important;
|
| 188 |
margin: 0.5rem !important;
|
| 189 |
-
border-radius:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
}
|
|
|
|
| 191 |
.user-message {
|
| 192 |
-
background
|
|
|
|
|
|
|
| 193 |
}
|
|
|
|
| 194 |
.bot-message {
|
| 195 |
-
background
|
|
|
|
| 196 |
}
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
padding: 1rem !important;
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
margin-top: 1rem !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
}
|
| 203 |
"""
|
| 204 |
|
|
@@ -207,9 +334,13 @@ def create_chat_interface():
|
|
| 207 |
# Header
|
| 208 |
with gr.Row():
|
| 209 |
gr.HTML("""
|
| 210 |
-
<div style="text-align: center; margin-bottom:
|
| 211 |
-
<h1 style="font-size:
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
</div>
|
| 214 |
""")
|
| 215 |
|
|
@@ -221,21 +352,43 @@ def create_chat_interface():
|
|
| 221 |
show_label=False,
|
| 222 |
container=True,
|
| 223 |
elem_classes=["chat-window"],
|
| 224 |
-
type='messages'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
)
|
| 226 |
|
| 227 |
-
# Input area with buttons
|
| 228 |
with gr.Row():
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
# Settings panel
|
| 238 |
-
with gr.Accordion(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
with gr.Row():
|
| 240 |
with gr.Column():
|
| 241 |
system_msg = gr.Textbox(
|
|
@@ -266,62 +419,11 @@ def create_chat_interface():
|
|
| 266 |
label="Top-p (nucleus sampling)"
|
| 267 |
)
|
| 268 |
|
| 269 |
-
#
|
| 270 |
-
|
| 271 |
-
if message:
|
| 272 |
-
return "", history + [{"role": "user", "content": message}]
|
| 273 |
-
return "", history
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
return history
|
| 278 |
-
|
| 279 |
-
# Get the last user message
|
| 280 |
-
message = history[-1]["content"]
|
| 281 |
-
|
| 282 |
-
# Check for custom responses first
|
| 283 |
-
custom_response = check_custom_responses(message)
|
| 284 |
-
if custom_response:
|
| 285 |
-
history.append({"role": "assistant", "content": custom_response})
|
| 286 |
-
return history
|
| 287 |
-
|
| 288 |
-
# Check for image generation request
|
| 289 |
-
if is_image_request(message):
|
| 290 |
-
try:
|
| 291 |
-
image = generate_image(message)
|
| 292 |
-
if image:
|
| 293 |
-
history.append({"role": "assistant", "content": "Here's your generated image!"})
|
| 294 |
-
return history
|
| 295 |
-
except Exception as e:
|
| 296 |
-
history.append({"role": "assistant", "content": f"Sorry, I couldn't generate the image: {str(e)}"})
|
| 297 |
-
return history
|
| 298 |
-
|
| 299 |
-
# Handle regular text responses
|
| 300 |
-
try:
|
| 301 |
-
translated_msg, original_lang, was_transliterated = translate_text(message)
|
| 302 |
-
messages = [{"role": "system", "content": system_msg}]
|
| 303 |
-
messages.extend(history[:-1])
|
| 304 |
-
messages.append({"role": "user", "content": translated_msg})
|
| 305 |
-
|
| 306 |
-
response = ""
|
| 307 |
-
for token in text_client.chat_completion(
|
| 308 |
-
messages,
|
| 309 |
-
max_tokens=max_tokens,
|
| 310 |
-
temperature=temperature,
|
| 311 |
-
top_p=top_p,
|
| 312 |
-
stream=True,
|
| 313 |
-
):
|
| 314 |
-
response += token.choices[0].delta.content or ""
|
| 315 |
-
history.append({"role": "assistant", "content": response})
|
| 316 |
-
yield history
|
| 317 |
-
time.sleep(0.02)
|
| 318 |
-
|
| 319 |
-
except Exception as e:
|
| 320 |
-
history.append({"role": "assistant", "content": f"An error occurred: {str(e)}"})
|
| 321 |
-
yield history
|
| 322 |
-
|
| 323 |
-
# Event handlers
|
| 324 |
-
txt_msg = txt.submit(
|
| 325 |
user_message,
|
| 326 |
[txt, chatbot],
|
| 327 |
[txt, chatbot],
|
|
@@ -329,7 +431,7 @@ def create_chat_interface():
|
|
| 329 |
).then(
|
| 330 |
bot_response,
|
| 331 |
[chatbot, system_msg, max_tokens, temperature, top_p],
|
| 332 |
-
chatbot
|
| 333 |
)
|
| 334 |
|
| 335 |
send_btn.click(
|
|
@@ -340,10 +442,22 @@ def create_chat_interface():
|
|
| 340 |
).then(
|
| 341 |
bot_response,
|
| 342 |
[chatbot, system_msg, max_tokens, temperature, top_p],
|
| 343 |
-
chatbot
|
| 344 |
)
|
| 345 |
|
| 346 |
-
clear_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
return demo
|
| 349 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
+
import requests
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from huggingface_hub import InferenceClient, HfApi
|
| 7 |
from deep_translator import GoogleTranslator
|
| 8 |
from indic_transliteration import sanscript
|
| 9 |
from indic_transliteration.detect import detect as detect_script
|
| 10 |
from indic_transliteration.sanscript import transliterate
|
| 11 |
import langdetect
|
| 12 |
import re
|
| 13 |
+
import os
|
| 14 |
+
|
| 15 |
+
# Get secrets from Hugging Face Space
|
| 16 |
+
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 17 |
+
if not HF_TOKEN:
|
| 18 |
+
raise ValueError("Please set the HF_TOKEN secret in your HuggingFace Space")
|
| 19 |
|
| 20 |
# Initialize clients
|
| 21 |
+
text_client = InferenceClient(
|
| 22 |
+
"HuggingFaceH4/zephyr-7b-beta",
|
| 23 |
+
token=HF_TOKEN
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Image generation setup
|
| 27 |
+
API_URL = "https://api-inference.huggingface.co/models/SG161222/RealVisXL_V4.0"
|
| 28 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 29 |
|
| 30 |
def detect_language_script(text: str) -> tuple[str, str]:
|
| 31 |
"""Detect language and script of the input text.
|
|
|
|
| 127 |
message_lower = message.lower()
|
| 128 |
return any(trigger in message_lower for trigger in image_triggers)
|
| 129 |
|
| 130 |
+
def generate_image(prompt):
|
| 131 |
+
"""Generate image using HuggingFace inference API"""
|
| 132 |
try:
|
| 133 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
|
| 134 |
+
image = Image.open(io.BytesIO(response.content))
|
| 135 |
+
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
print(f"Image generation error: {e}")
|
| 138 |
return None
|
| 139 |
+
|
| 140 |
def romanized_to_bengali(text: str) -> str:
|
| 141 |
"""Convert romanized Bengali text to Bengali script."""
|
| 142 |
bengali_mappings = {
|
|
|
|
| 165 |
|
| 166 |
return text_lower
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
def create_chat_interface():
|
| 171 |
+
# Custom CSS for better styling with Inter font and animations
|
| 172 |
custom_css = """
|
| 173 |
+
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
| 174 |
+
|
| 175 |
+
* {
|
| 176 |
+
font-family: 'Inter', sans-serif !important;
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
.container {
|
| 180 |
max-width: 850px !important;
|
| 181 |
margin: auto;
|
| 182 |
}
|
| 183 |
+
|
| 184 |
.chat-window {
|
| 185 |
height: 600px !important;
|
| 186 |
overflow-y: auto;
|
| 187 |
border-radius: 15px !important;
|
| 188 |
+
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1) !important;
|
| 189 |
+
transition: all 0.3s ease !important;
|
| 190 |
}
|
| 191 |
+
|
| 192 |
+
.chat-window:hover {
|
| 193 |
+
box-shadow: 0 12px 20px rgba(0, 0, 0, 0.15) !important;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
.chat-message {
|
| 197 |
padding: 1rem !important;
|
| 198 |
margin: 0.5rem !important;
|
| 199 |
+
border-radius: 12px !important;
|
| 200 |
+
transition: all 0.2s ease !important;
|
| 201 |
+
opacity: 0;
|
| 202 |
+
animation: messageSlide 0.3s ease forwards;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
@keyframes messageSlide {
|
| 206 |
+
from {
|
| 207 |
+
opacity: 0;
|
| 208 |
+
transform: translateY(10px);
|
| 209 |
+
}
|
| 210 |
+
to {
|
| 211 |
+
opacity: 1;
|
| 212 |
+
transform: translateY(0);
|
| 213 |
+
}
|
| 214 |
}
|
| 215 |
+
|
| 216 |
.user-message {
|
| 217 |
+
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
|
| 218 |
+
color: white !important;
|
| 219 |
+
margin-left: 2rem !important;
|
| 220 |
}
|
| 221 |
+
|
| 222 |
.bot-message {
|
| 223 |
+
background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%) !important;
|
| 224 |
+
margin-right: 2rem !important;
|
| 225 |
}
|
| 226 |
+
|
| 227 |
+
/* Button Styles */
|
| 228 |
+
button.primary {
|
| 229 |
+
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%) !important;
|
| 230 |
+
border: none !important;
|
| 231 |
+
color: white !important;
|
| 232 |
+
padding: 0.75rem 1.5rem !important;
|
| 233 |
+
border-radius: 12px !important;
|
| 234 |
+
font-weight: 600 !important;
|
| 235 |
+
transition: all 0.3s ease !important;
|
| 236 |
+
transform: translateY(0);
|
| 237 |
+
box-shadow: 0 4px 6px rgba(99, 102, 241, 0.2) !important;
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
button.primary:hover {
|
| 241 |
+
transform: translateY(-2px);
|
| 242 |
+
box-shadow: 0 8px 12px rgba(99, 102, 241, 0.3) !important;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
button.primary:active {
|
| 246 |
+
transform: translateY(0);
|
| 247 |
+
}
|
| 248 |
+
|
| 249 |
+
button.secondary {
|
| 250 |
+
background: #f3f4f6 !important;
|
| 251 |
+
border: 2px solid #e5e7eb !important;
|
| 252 |
+
color: #4b5563 !important;
|
| 253 |
+
padding: 0.75rem 1.5rem !important;
|
| 254 |
+
border-radius: 12px !important;
|
| 255 |
+
font-weight: 600 !important;
|
| 256 |
+
transition: all 0.3s ease !important;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
button.secondary:hover {
|
| 260 |
+
background: #e5e7eb !important;
|
| 261 |
+
border-color: #d1d5db !important;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
/* Input Styles */
|
| 265 |
+
.input-container {
|
| 266 |
+
position: relative;
|
| 267 |
+
margin-bottom: 1rem;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
textarea {
|
| 271 |
+
border: 2px solid #e5e7eb !important;
|
| 272 |
+
border-radius: 12px !important;
|
| 273 |
padding: 1rem !important;
|
| 274 |
+
transition: all 0.3s ease !important;
|
| 275 |
+
font-size: 1rem !important;
|
| 276 |
+
line-height: 1.5 !important;
|
| 277 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05) !important;
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
textarea:focus {
|
| 281 |
+
border-color: #6366f1 !important;
|
| 282 |
+
box-shadow: 0 4px 6px rgba(99, 102, 241, 0.1) !important;
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
/* Settings Panel */
|
| 286 |
+
.settings-block {
|
| 287 |
+
background: white !important;
|
| 288 |
+
border-radius: 15px !important;
|
| 289 |
+
padding: 1.5rem !important;
|
| 290 |
margin-top: 1rem !important;
|
| 291 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05) !important;
|
| 292 |
+
transition: all 0.3s ease !important;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
.settings-block:hover {
|
| 296 |
+
box-shadow: 0 6px 8px rgba(0, 0, 0, 0.08) !important;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
/* Slider Styles */
|
| 300 |
+
.gr-slider {
|
| 301 |
+
height: 4px !important;
|
| 302 |
+
background: #e5e7eb !important;
|
| 303 |
+
border-radius: 2px !important;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
.gr-slider .handle {
|
| 307 |
+
width: 16px !important;
|
| 308 |
+
height: 16px !important;
|
| 309 |
+
border: 2px solid #6366f1 !important;
|
| 310 |
+
background: white !important;
|
| 311 |
+
border-radius: 50% !important;
|
| 312 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1) !important;
|
| 313 |
+
transition: all 0.2s ease !important;
|
| 314 |
+
}
|
| 315 |
+
|
| 316 |
+
.gr-slider .handle:hover {
|
| 317 |
+
transform: scale(1.1);
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
/* Loading Animation */
|
| 321 |
+
@keyframes pulse {
|
| 322 |
+
0% { opacity: 1; }
|
| 323 |
+
50% { opacity: 0.5; }
|
| 324 |
+
100% { opacity: 1; }
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
.loading {
|
| 328 |
+
animation: pulse 1.5s ease-in-out infinite;
|
| 329 |
}
|
| 330 |
"""
|
| 331 |
|
|
|
|
| 334 |
# Header
|
| 335 |
with gr.Row():
|
| 336 |
gr.HTML("""
|
| 337 |
+
<div style="text-align: center; margin-bottom: 2rem; padding: 2rem;">
|
| 338 |
+
<h1 style="font-size: 3rem; font-weight: 700; color: #4f46e5; margin-bottom: 0.5rem;">
|
| 339 |
+
✨ Xylaria Chat
|
| 340 |
+
</h1>
|
| 341 |
+
<p style="color: #6b7280; font-size: 1.2rem; font-weight: 500;">
|
| 342 |
+
Your Intelligent Multilingual Assistant
|
| 343 |
+
</p>
|
| 344 |
</div>
|
| 345 |
""")
|
| 346 |
|
|
|
|
| 352 |
show_label=False,
|
| 353 |
container=True,
|
| 354 |
elem_classes=["chat-window"],
|
| 355 |
+
type='messages'
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
image_output = gr.Image(
|
| 359 |
+
type="pil",
|
| 360 |
+
label="Generated Image",
|
| 361 |
+
visible=False,
|
| 362 |
+
elem_classes=["generated-image"]
|
| 363 |
)
|
| 364 |
|
|
|
|
| 365 |
with gr.Row():
|
| 366 |
+
with gr.Column(scale=8):
|
| 367 |
+
txt = gr.Textbox(
|
| 368 |
+
show_label=False,
|
| 369 |
+
placeholder="Type your message here...",
|
| 370 |
+
container=False,
|
| 371 |
+
elem_classes=["input-textbox"]
|
| 372 |
+
)
|
| 373 |
+
with gr.Column(scale=1):
|
| 374 |
+
send_btn = gr.Button(
|
| 375 |
+
"Send",
|
| 376 |
+
variant="primary",
|
| 377 |
+
elem_classes=["primary"]
|
| 378 |
+
)
|
| 379 |
+
with gr.Column(scale=1):
|
| 380 |
+
clear_btn = gr.Button(
|
| 381 |
+
"Clear",
|
| 382 |
+
variant="secondary",
|
| 383 |
+
elem_classes=["secondary"]
|
| 384 |
+
)
|
| 385 |
|
| 386 |
+
# Settings panel
|
| 387 |
+
with gr.Accordion(
|
| 388 |
+
"⚙️ Advanced Settings",
|
| 389 |
+
open=False,
|
| 390 |
+
elem_classes=["settings-accordion"]
|
| 391 |
+
):
|
| 392 |
with gr.Row():
|
| 393 |
with gr.Column():
|
| 394 |
system_msg = gr.Textbox(
|
|
|
|
| 419 |
label="Top-p (nucleus sampling)"
|
| 420 |
)
|
| 421 |
|
| 422 |
+
# Rest of your existing functions (user_message, bot_response, etc.)
|
| 423 |
+
# ... (keep the same function implementations)
|
|
|
|
|
|
|
|
|
|
| 424 |
|
| 425 |
+
# Update the event handlers to use the new classes
|
| 426 |
+
send_event = txt.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
user_message,
|
| 428 |
[txt, chatbot],
|
| 429 |
[txt, chatbot],
|
|
|
|
| 431 |
).then(
|
| 432 |
bot_response,
|
| 433 |
[chatbot, system_msg, max_tokens, temperature, top_p],
|
| 434 |
+
[chatbot, image_output]
|
| 435 |
)
|
| 436 |
|
| 437 |
send_btn.click(
|
|
|
|
| 442 |
).then(
|
| 443 |
bot_response,
|
| 444 |
[chatbot, system_msg, max_tokens, temperature, top_p],
|
| 445 |
+
[chatbot, image_output]
|
| 446 |
)
|
| 447 |
|
| 448 |
+
clear_btn.click(
|
| 449 |
+
lambda: (None, None),
|
| 450 |
+
None,
|
| 451 |
+
[chatbot, image_output],
|
| 452 |
+
queue=False
|
| 453 |
+
)
|
| 454 |
+
|
| 455 |
+
# Update image visibility
|
| 456 |
+
send_event.then(
|
| 457 |
+
lambda img: gr.update(visible=img is not None),
|
| 458 |
+
image_output,
|
| 459 |
+
image_output
|
| 460 |
+
)
|
| 461 |
|
| 462 |
return demo
|
| 463 |
|