OpceanAI commited on
Commit
26f0ff4
·
verified ·
1 Parent(s): 1c634c9

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +35 -34
app.py CHANGED
@@ -14,21 +14,21 @@ def get_token(request: gr.Request):
14
  def chat_stream(message, history, request: gr.Request):
15
  token = get_token(request)
16
  if not token:
17
- yield history + [{"role": "assistant", "content": "⚠️ Por favor, inicia sesión con tu cuenta de Hugging Face para usar el chat."}]
18
  return
19
 
20
  messages = []
21
- for msg in history:
22
- if msg["role"] == "user":
23
- messages.append({"role": "user", "content": msg["content"]})
24
- elif msg["role"] == "assistant":
25
- messages.append({"role": "assistant", "content": msg["content"]})
26
  messages.append({"role": "user", "content": message})
27
 
28
  headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
29
  payload = {"model": MODEL, "messages": messages, "stream": True, "max_tokens": 2048}
30
 
31
- chat_history = history + [{"role": "user", "content": message}]
32
  yield chat_history
33
 
34
  full_response = ""
@@ -36,7 +36,7 @@ def chat_stream(message, history, request: gr.Request):
36
  with httpx.Client(timeout=60.0) as client:
37
  with client.stream("POST", API_URL, json=payload, headers=headers) as response:
38
  if response.status_code != 200:
39
- yield chat_history + [{"role": "assistant", "content": f"❌ Error {response.status_code}"}]
40
  return
41
  for line in response.iter_lines():
42
  if not line.startswith("data: "):
@@ -50,11 +50,11 @@ def chat_stream(message, history, request: gr.Request):
50
  content = delta.get("content", "")
51
  if content:
52
  full_response += content
53
- yield chat_history + [{"role": "assistant", "content": full_response}]
54
  except (json.JSONDecodeError, IndexError, KeyError):
55
  continue
56
  except Exception as e:
57
- yield chat_history + [{"role": "assistant", "content": f"❌ Error: {str(e)}"}]
58
 
59
  custom_css = """
60
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&family=JetBrains+Mono:wght@400;500;600&display=swap');
@@ -479,29 +479,7 @@ document.addEventListener('DOMContentLoaded', () => { initTheme(); setupNavbar()
479
  # ─── App ──────────────────────────────────────────────────────────────────────
480
  with gr.Blocks(
481
  fill_height=False,
482
- css=custom_css,
483
- js=custom_js,
484
  title="Yuuki-RxG",
485
- theme=gr.themes.Base(
486
- primary_hue="zinc", neutral_hue="zinc",
487
- font=gr.themes.GoogleFont("Inter"), font_mono=gr.themes.GoogleFont("JetBrains Mono"),
488
- radius_size=gr.themes.sizes.radius_sm, spacing_size=gr.themes.sizes.spacing_sm,
489
- ).set(
490
- body_background_fill="#09090b", body_text_color="#fafafa",
491
- background_fill_primary="#09090b", background_fill_secondary="#111113",
492
- border_color_primary="rgba(255,255,255,0.08)", color_accent="#22c55e",
493
- color_accent_soft="rgba(34,197,94,0.12)",
494
- button_primary_background_fill="#fafafa", button_primary_text_color="#09090b",
495
- button_primary_background_fill_hover="#e4e4e7",
496
- button_secondary_background_fill="transparent",
497
- button_secondary_border_color="rgba(255,255,255,0.08)",
498
- button_secondary_text_color="#a1a1aa",
499
- block_border_color="rgba(255,255,255,0.08)", block_background_fill="transparent",
500
- block_shadow="none", input_background_fill="rgba(255,255,255,0.04)",
501
- input_border_color="rgba(255,255,255,0.08)",
502
- input_border_color_focus="rgba(255,255,255,0.25)", input_shadow_focus="none",
503
- shadow_drop="none", shadow_drop_lg="none",
504
- )
505
  ) as demo:
506
  # ─── Hero ─────────────────────────────────────────────────────────────
507
  gr.HTML("""
@@ -577,7 +555,7 @@ with gr.Blocks(
577
 
578
  login_btn = gr.LoginButton("Iniciar sesión con Hugging Face", elem_classes="auth-login-btn")
579
 
580
- chatbot = gr.Chatbot(type="messages", height=480)
581
  msg = gr.Textbox(placeholder="Escribe tu mensaje...", lines=1)
582
  clear = gr.Button("Limpiar chat", variant="secondary")
583
 
@@ -597,7 +575,30 @@ with gr.Blocks(
597
 
598
  if __name__ == "__main__":
599
  try:
600
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
601
  finally:
602
  import asyncio
603
  try:
 
14
  def chat_stream(message, history, request: gr.Request):
15
  token = get_token(request)
16
  if not token:
17
+ yield history + [(message, "⚠️ Por favor, inicia sesión con tu cuenta de Hugging Face para usar el chat.")]
18
  return
19
 
20
  messages = []
21
+ for user_msg, bot_msg in history:
22
+ if user_msg:
23
+ messages.append({"role": "user", "content": user_msg})
24
+ if bot_msg:
25
+ messages.append({"role": "assistant", "content": bot_msg})
26
  messages.append({"role": "user", "content": message})
27
 
28
  headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
29
  payload = {"model": MODEL, "messages": messages, "stream": True, "max_tokens": 2048}
30
 
31
+ chat_history = history + [(message, None)]
32
  yield chat_history
33
 
34
  full_response = ""
 
36
  with httpx.Client(timeout=60.0) as client:
37
  with client.stream("POST", API_URL, json=payload, headers=headers) as response:
38
  if response.status_code != 200:
39
+ yield chat_history[:-1] + [(message, f"❌ Error {response.status_code}")]
40
  return
41
  for line in response.iter_lines():
42
  if not line.startswith("data: "):
 
50
  content = delta.get("content", "")
51
  if content:
52
  full_response += content
53
+ yield chat_history[:-1] + [(message, full_response)]
54
  except (json.JSONDecodeError, IndexError, KeyError):
55
  continue
56
  except Exception as e:
57
+ yield chat_history[:-1] + [(message, f"❌ Error: {str(e)}")]
58
 
59
  custom_css = """
60
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800;900&family=JetBrains+Mono:wght@400;500;600&display=swap');
 
479
  # ─── App ──────────────────────────────────────────────────────────────────────
480
  with gr.Blocks(
481
  fill_height=False,
 
 
482
  title="Yuuki-RxG",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  ) as demo:
484
  # ─── Hero ─────────────────────────────────────────────────────────────
485
  gr.HTML("""
 
555
 
556
  login_btn = gr.LoginButton("Iniciar sesión con Hugging Face", elem_classes="auth-login-btn")
557
 
558
+ chatbot = gr.Chatbot(height=480)
559
  msg = gr.Textbox(placeholder="Escribe tu mensaje...", lines=1)
560
  clear = gr.Button("Limpiar chat", variant="secondary")
561
 
 
575
 
576
  if __name__ == "__main__":
577
  try:
578
+ demo.launch(
579
+ css=custom_css,
580
+ js=custom_js,
581
+ theme=gr.themes.Base(
582
+ primary_hue="zinc", neutral_hue="zinc",
583
+ font=gr.themes.GoogleFont("Inter"), font_mono=gr.themes.GoogleFont("JetBrains Mono"),
584
+ radius_size=gr.themes.sizes.radius_sm, spacing_size=gr.themes.sizes.spacing_sm,
585
+ ).set(
586
+ body_background_fill="#09090b", body_text_color="#fafafa",
587
+ background_fill_primary="#09090b", background_fill_secondary="#111113",
588
+ border_color_primary="rgba(255,255,255,0.08)", color_accent="#22c55e",
589
+ color_accent_soft="rgba(34,197,94,0.12)",
590
+ button_primary_background_fill="#fafafa", button_primary_text_color="#09090b",
591
+ button_primary_background_fill_hover="#e4e4e7",
592
+ button_secondary_background_fill="transparent",
593
+ button_secondary_border_color="rgba(255,255,255,0.08)",
594
+ button_secondary_text_color="#a1a1aa",
595
+ block_border_color="rgba(255,255,255,0.08)", block_background_fill="transparent",
596
+ block_shadow="none", input_background_fill="rgba(255,255,255,0.04)",
597
+ input_border_color="rgba(255,255,255,0.08)",
598
+ input_border_color_focus="rgba(255,255,255,0.25)", input_shadow_focus="none",
599
+ shadow_drop="none", shadow_drop_lg="none",
600
+ )
601
+ )
602
  finally:
603
  import asyncio
604
  try: