Spaces:
Sleeping
Sleeping
File size: 18,057 Bytes
eb3516b bb367a8 af65e0a f99bbd2 90f65d7 8d2aacb b74ae79 17a86bf 17fec4b bb367a8 8d2aacb 90f65d7 eb3516b 8d2aacb 90f65d7 bb367a8 eb3516b f2b1c55 bb367a8 140474f 8d2aacb 90f65d7 f2b1c55 17a86bf af781fa bb367a8 f2b1c55 eb3516b 17a86bf f2b1c55 46c0344 f2b1c55 82b205d 2b2be0b 22ab9c1 0786ab5 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 531fa18 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 8d2aacb a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 8d2aacb a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 a1355b3 f99bbd2 140474f f99bbd2 a1355b3 f99bbd2 140474f a1355b3 f99bbd2 140474f f99bbd2 9688ae6 a1355b3 f99bbd2 140474f a1355b3 7de3756 a1355b3 140474f a1355b3 f99bbd2 140474f f99bbd2 f2b1c55 140474f f99bbd2 f2b1c55 f99bbd2 140474f f2b1c55 a1355b3 9688ae6 a1355b3 8d2aacb a1355b3 8d2aacb 140474f f2b1c55 140474f a1355b3 140474f f99bbd2 140474f f2b1c55 f99bbd2 f2b1c55 fde08e9 f2b1c55 fde08e9 8d2aacb 90f65d7 8d2aacb 90f65d7 25e84a6 f2b1c55 140474f f99bbd2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
import gradio as gr
from transformers import pipeline
from langdetect import detect
from huggingface_hub import login
import os
import base64
from PIL import Image
from transformers import BlipProcessor, BlipForConditionalGeneration
import requests
from bs4 import BeautifulSoup
import cv2
from io import BytesIO
import torch
login(token=os.getenv("chatbot"))
generator = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
bg_to_en = pipeline("translation", model="Helsinki-NLP/opus-mt-bg-en")
en_to_bg = pipeline("translation", model="Helsinki-NLP/opus-mt-en-bg")
# Load BLIP for image captioning
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
blip_model.to("cuda" if torch.cuda.is_available() else "cpu")
def describe_image(image):
inputs = blip_processor(images=image, return_tensors="pt").to(blip_model.device)
out = blip_model.generate(**inputs)
caption = blip_processor.decode(out[0], skip_special_tokens=True)
return caption
def describe_video(video_path):
cap = cv2.VideoCapture(video_path)
success, frame = cap.read()
cap.release()
if success:
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
return describe_image(img)
else:
return "Could not read video frame."
def extract_text_from_url(url):
try:
response = requests.get(url, timeout=5)
soup = BeautifulSoup(response.text, 'html.parser')
paragraphs = soup.find_all('p')
text = ' '.join([p.text for p in paragraphs])
return text[:2000] # keep it short
except Exception as e:
return f"Failed to fetch URL: {str(e)}"
def generate_response(user_input, top_p, temperature, chat_counter, chatbot, history, image=None, video=None, url=None, request: gr.Request = None):
lang = detect(user_input)
print(f"Detected language: {lang}")
# Translate if needed
if lang == "bg":
user_input_translated = bg_to_en(user_input)[0]["translation_text"]
else:
user_input_translated = user_input
prompt = ""
# Multimodal additions
if image is not None:
try:
img_desc = describe_image(image)
prompt += f"[Image Description]: {img_desc}\n"
except Exception as e:
prompt += f"[Image Error]: {str(e)}\n"
if video is not None:
try:
vid_desc = describe_video(video)
prompt += f"[Video Description]: {vid_desc}\n"
except Exception as e:
prompt += f"[Video Error]: {str(e)}\n"
if url:
url_content = extract_text_from_url(url)
prompt += f"[URL Content]: {url_content}\n"
# Append user message to prompt
prompt += f"User said:\n{user_input_translated}\n\nGive simple, friendly, clear advice:\n"
# Generate response
response_text = generator(prompt, max_length=200, top_p=top_p, temperature=temperature, do_sample=True)[0]["generated_text"]
if lang == "bg":
response_text = en_to_bg(response_text)[0]["translation_text"]
# Format for type="messages"
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": response_text})
chat_counter += 1
return history, history, chat_counter, "β
Success", gr.update(value=''), gr.update(interactive=True)
def reset_textbox():
return gr.update(value='')
# ==== Enhanced Custom CSS with Masterbrand Styling ====
custom_css = f"""
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&display=swap');
* {{
font-family: 'Inter', sans-serif !important;
}}
.gradio-container {{
background: linear-gradient(135deg, #0a0a0a 0%, #1a0a0a 50%, #2a1a1a 100%) !important;
min-height: 100vh !important;
}}
.main-header {{
background: linear-gradient(135deg, #0a0a0a 0%, #1a0a0a 50%, #2a1a1a 100%) !important;
padding: 2rem 0 3rem 0 !important;
text-align: center !important;
border-bottom: 3px solid #ff0000 !important;
margin-bottom: 2rem !important;
position: relative !important;
overflow: hidden !important;
}}
.main-header::before {{
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: radial-gradient(circle at 50% 50%, rgba(255, 0, 0, 0.1) 0%, transparent 70%);
pointer-events: none;
}}
.logo-container {{
display: flex !important;
justify-content: center !important;
align-items: center !important;
margin-bottom: 1.5rem !important;
gap: 1rem !important;
}}
.logo-image {{
width: 50px !important;
height: 50px !important;
border-radius: 15px !important;
box-shadow: 0 0 30px rgba(255, 0, 0, 0.5) !important;
animation: pulse-glow 2s ease-in-out infinite alternate !important;
transition: transform 0.3s ease !important;
}}
.logo-image:hover {{
transform: scale(1.1) !important;
}}
.brand-text {{
background: linear-gradient(135deg, #00ff41 0%, #ffffff 50%, #ff0000 100%) !important;
-webkit-background-clip: text !important;
-webkit-text-fill-color: transparent !important;
background-clip: text !important;
font-size: 2.5rem !important;
font-weight: 900 !important;
letter-spacing: 2px !important;
text-shadow: 0 0 20px rgba(0, 255, 65, 0.3) !important;
}}
.main-header h1 {{
color: #ffffff !important;
font-size: 3.5rem !important;
font-weight: 900 !important;
text-shadow: 0 0 30px rgba(255, 0, 0, 0.5) !important;
background: linear-gradient(135deg, #ffffff 0%, #ff0000 100%) !important;
-webkit-background-clip: text !important;
-webkit-text-fill-color: transparent !important;
background-clip: text !important;
margin: 0 !important;
}}
.main-header p {{
color: #cccccc !important;
font-size: 1.3rem !important;
font-weight: 500 !important;
margin-top: 0.5rem !important;
}}
.chatbot-container {{
background: rgba(26, 26, 26, 0.95) !important;
border: 2px solid #ff0000 !important;
border-radius: 25px !important;
box-shadow: 0 15px 40px rgba(255, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.1) !important;
margin-bottom: 2rem !important;
backdrop-filter: blur(10px) !important;
}}
.input-section {{
background: rgba(26, 26, 26, 0.8) !important;
border: 2px solid #333333 !important;
border-radius: 20px !important;
padding: 2rem !important;
margin: 1rem 0 !important;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3) !important;
backdrop-filter: blur(10px) !important;
transition: all 0.3s ease !important;
}}
.input-section:hover {{
border-color: #ff0000 !important;
box-shadow: 0 15px 40px rgba(255, 0, 0, 0.2) !important;
}}
.input-group {{
margin-bottom: 1.5rem !important;
}}
.input-group:last-child {{
margin-bottom: 0 !important;
}}
.input-label {{
color: #ffffff !important;
font-size: 1.1rem !important;
font-weight: 600 !important;
margin-bottom: 0.8rem !important;
display: flex !important;
align-items: center !important;
gap: 0.5rem !important;
}}
.input-icon {{
font-size: 1.2rem !important;
color: #ff0000 !important;
}}
.enhanced-textbox textarea {{
background: linear-gradient(135deg, #1a1a1a 0%, #2a2a1a 100%) !important;
border: 2px solid #444444 !important;
border-radius: 15px !important;
color: #ffffff !important;
font-size: 1.1rem !important;
padding: 1.2rem !important;
resize: none !important;
transition: all 0.3s ease !important;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3) !important;
}}
.enhanced-textbox textarea:focus {{
border-color: #ff0000 !important;
box-shadow: 0 0 25px rgba(255, 0, 0, 0.4), 0 5px 15px rgba(0, 0, 0, 0.3) !important;
outline: none !important;
transform: translateY(-2px) !important;
}}
.enhanced-textbox textarea::placeholder {{
color: #888888 !important;
font-style: italic !important;
}}
.enhanced-image-input, .enhanced-video-input {{
background: rgba(42, 42, 42, 0.8) !important;
border: 2px dashed #444444 !important;
border-radius: 15px !important;
padding: 2rem !important;
text-align: center !important;
transition: all 0.3s ease !important;
min-height: 120px !important;
display: flex !important;
align-items: center !important;
justify-content: center !important;
}}
.enhanced-image-input:hover, .enhanced-video-input:hover {{
border-color: #ff0000 !important;
background: rgba(255, 0, 0, 0.05) !important;
}}
.enhanced-url-input input {{
background: linear-gradient(135deg, #1a1a1a 0%, #2a2a1a 100%) !important;
border: 2px solid #444444 !important;
border-radius: 15px !important;
color: #ffffff !important;
font-size: 1.1rem !important;
padding: 1.2rem !important;
transition: all 0.3s ease !important;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3) !important;
}}
.enhanced-url-input input:focus {{
border-color: #ff0000 !important;
box-shadow: 0 0 25px rgba(255, 0, 0, 0.4), 0 5px 15px rgba(0, 0, 0, 0.3) !important;
outline: none !important;
transform: translateY(-2px) !important;
}}
.enhanced-url-input input::placeholder {{
color: #888888 !important;
font-style: italic !important;
}}
.submit-button {{
background: linear-gradient(135deg, #ff0000 0%, #cc0000 100%) !important;
border: none !important;
border-radius: 15px !important;
color: #ffffff !important;
font-size: 1.2rem !important;
font-weight: 700 !important;
padding: 1.2rem 2.5rem !important;
transition: all 0.3s ease !important;
box-shadow: 0 8px 20px rgba(255, 0, 0, 0.3) !important;
text-transform: uppercase !important;
letter-spacing: 1px !important;
width: 100% !important;
margin-top: 1rem !important;
}}
.submit-button:hover {{
background: linear-gradient(135deg, #cc0000 0%, #aa0000 100%) !important;
transform: translateY(-3px) !important;
box-shadow: 0 12px 30px rgba(255, 0, 0, 0.4) !important;
}}
.submit-button:active {{
transform: translateY(-1px) !important;
}}
.accordion-container {{
background: rgba(26, 26, 26, 0.8) !important;
border: 1px solid #333333 !important;
border-radius: 15px !important;
margin-top: 2rem !important;
}}
.slider-container label {{
color: #ffffff !important;
font-weight: 600 !important;
font-size: 1rem !important;
}}
.slider-container input[type="range"] {{
accent-color: #ff0000 !important;
}}
.status-container textarea {{
background: transparent !important;
border: none !important;
color: #ff0000 !important;
font-weight: 600 !important;
text-align: center !important;
}}
.footer {{
display: none !important;
}}
/* Enhanced chat styling */
.message.user {{
background: linear-gradient(135deg, #2a2a2a 0%, #3a3a2a 100%) !important;
border-left: 4px solid #ff0000 !important;
margin-left: 2rem !important;
}}
.message.bot {{
background: linear-gradient(135deg, #1a2a1a 0%, #2a3a2a 100%) !important;
border-left: 4px solid #cc0000 !important;
margin-right: 2rem !important;
}}
/* Responsive design */
@media (max-width: 768px) {{
.main-header h1 {{
font-size: 2.5rem !important;
}}
.main-header p {{
font-size: 1.1rem !important;
}}
.logo-image {{
width: 60px !important;
height: 60px !important;
}}
.brand-text {{
font-size: 2rem !important;
}}
}}
/* Animations */
@keyframes pulse-glow {{
0% {{
box-shadow: 0 0 20px rgba(255, 0, 0, 0.3);
transform: scale(1);
}}
50% {{
box-shadow: 0 0 40px rgba(255, 0, 0, 0.6);
transform: scale(1.02);
}}
100% {{
box-shadow: 0 0 20px rgba(255, 0, 0, 0.3);
transform: scale(1);
}}
}}
@keyframes float {{
0%, 100% {{ transform: translateY(0px); }}
50% {{ transform: translateY(-10px); }}
}}
.floating {{
animation: float 3s ease-in-out infinite;
}}
"""
# ==== Enhanced Theme ====
masterbrand_theme = gr.themes.Base(primary_hue="red").set(
body_background_fill="#0a0a0a",
body_text_color="#ffffff",
border_color_accent="#ff0000",
button_primary_background_fill="#ff0000",
button_primary_background_fill_hover="#cc0000",
button_primary_text_color="#ffffff",
block_background_fill="#1a1a1a",
block_border_color="#333333"
)
# ==== Enhanced UI ====
with gr.Blocks(theme=masterbrand_theme, css=custom_css, title="MasterBrand Assistant") as demo:
# Enhanced header with logo
logo_html = f"""
<div class="main-header">
<div class="logo-container">
<img src="https://huggingface.co/spaces/Yordann/MasterBrand_ChatBot/resolve/main/logo.png" alt="MasterBrand Logo" class="logo-image floating style="height:100px;">
<div class="brand-text">MASTERBRAND</div>
</div>
<h1>AI ASSISTANT</h1>
<p>Your Personal E-commerce Business Expert - Available in English & Bulgarian</p>
<p style="font-size: 1rem; color: #ff0000; font-weight: 600; margin-top: 1rem;">
π Powered by Advanced AI β’ π Multilingual Support β’ πΌ Expert Business Guidance
</p>
</div>
"""
gr.HTML(logo_html)
with gr.Row():
with gr.Column(scale=1):
chatbot = gr.Chatbot(
label="π¬ Chat with MasterBrand AI",
height=450,
elem_classes=["chatbot-container"],
type="messages",
show_label=True,
container=True,
show_copy_button=True
)
# Enhanced Input Section
with gr.Column(elem_classes=["input-section"]):
gr.HTML('<div class="input-label"><span class="input-icon">π¬</span>Ask Your Question</div>')
inputs = gr.Textbox(
placeholder="Ask me anything about your e-commerce business please... π‘",
label="",
lines=3,
elem_classes=["enhanced-textbox"],
show_label=False
)
with gr.Row():
with gr.Column():
gr.HTML('<div class="input-label"><span class="input-icon">πΌοΈ</span>Upload Image (Optional)</div>')
image_input = gr.Image(
label="",
type="pil",
elem_classes=["enhanced-image-input"],
show_label=False
)
with gr.Column():
gr.HTML('<div class="input-label"><span class="input-icon">π₯</span>Upload Video (Optional)</div>')
video_input = gr.Video(
label="",
elem_classes=["enhanced-video-input"],
show_label=False
)
gr.HTML('<div class="input-label"><span class="input-icon">π</span>Paste URL (Optional)</div>')
url_input = gr.Textbox(
label="",
placeholder="https://example.com - Paste any URL for analysis",
elem_classes=["enhanced-url-input"],
show_label=False
)
submit_btn = gr.Button(
"π Get Expert Advice",
variant="primary",
size="lg",
elem_classes=["submit-button"]
)
with gr.Row():
status_box = gr.Textbox(
label="Status",
interactive=False,
elem_classes=["status-container"],
show_label=False
)
with gr.Accordion("βοΈ Advanced Settings", open=False, elem_classes=["accordion-container"]):
with gr.Row():
top_p = gr.Slider(
0.1, 1.0,
value=0.9,
step=0.05,
label="π― Creativity (Top-p)",
elem_classes=["slider-container"],
info="Higher values make responses more creative"
)
temperature = gr.Slider(
0.1, 2.0,
value=1.0,
step=0.1,
label="π‘οΈ Temperature",
elem_classes=["slider-container"],
info="Controls randomness in responses"
)
# Enhanced footer
gr.HTML("""
<div style="text-align: center; padding: 2rem; color: #666666; border-top: 1px solid #333333; margin-top: 3rem;">
<p style="margin: 0; font-size: 0.9rem;">
Β© 2025 MasterBrand AI Assistant β’ Built with β€οΈ for E-commerce Success
</p>
<p style="margin: 0.5rem 0 0 0; font-size: 0.8rem; color: #888888;">
Empowering entrepreneurs worldwide with AI-driven business insights
</p>
</div>
"""
)
# State
state = gr.State([])
chat_counter = gr.Number(value=0, visible=False)
# Event Handlers
inputs.submit(reset_textbox, [], [inputs, submit_btn], queue=False)
inputs.submit(
generate_response,
[inputs, top_p, temperature, chat_counter, chatbot, state, image_input, video_input, url_input],
[chatbot, state, chat_counter, status_box, inputs, submit_btn]
)
submit_btn.click(reset_textbox, [], [inputs, submit_btn], queue=False)
submit_btn.click(
generate_response,
[inputs, top_p, temperature, chat_counter, chatbot, state, image_input, video_input, url_input],
[chatbot, state, chat_counter, status_box, inputs, submit_btn]
)
# ==== Launch ====
if __name__ == "__main__":
demo.queue(max_size=10).launch(server_name="0.0.0.0", server_port=7860)
|