Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,36 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_htmlinjector import HTMLInjector
|
| 3 |
|
| 4 |
-
#
|
| 5 |
def handle_choice(choice: str, chat_history: list[tuple[str, str]]):
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 7 |
if c in {"oui", "o", "yes", "y"}:
|
| 8 |
-
|
|
|
|
| 9 |
elif c in {"non", "n", "no"}:
|
| 10 |
-
|
|
|
|
| 11 |
else:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def on_submit(user_msg: str, chat_history: list[tuple[str, str]]):
|
| 18 |
if not user_msg:
|
| 19 |
-
return chat_history, "" # rien
|
|
|
|
| 20 |
assistant_reply = "Ceci est une réponse de démonstration.\n\nConfirmez-vous l’action ?"
|
| 21 |
chat_history = chat_history + [(user_msg, assistant_reply)]
|
| 22 |
-
return chat_history, "" #
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def inject_assets():
|
| 26 |
js = r"""
|
| 27 |
// Pousser une valeur dans un composant Gradio (shadow DOM v4)
|
|
@@ -59,7 +68,7 @@ def inject_assets():
|
|
| 59 |
background: var(--button-primary-background-fill, #2563eb);
|
| 60 |
color: white; border-color: transparent;
|
| 61 |
}
|
| 62 |
-
/*
|
| 63 |
#bridge-choice { display: none; }
|
| 64 |
"""
|
| 65 |
html = r"""
|
|
@@ -82,25 +91,23 @@ def inject_assets():
|
|
| 82 |
"""
|
| 83 |
return {"css": css, "js": js, "body_html": html}
|
| 84 |
|
| 85 |
-
#
|
| 86 |
with gr.Blocks(title="Démo Chat + Popup", fill_height=True) as demo:
|
| 87 |
html_injector = HTMLInjector()
|
| 88 |
|
| 89 |
chat = gr.Chatbot(label="Chatbot", height=420)
|
| 90 |
user_in = gr.Textbox(placeholder="Tapez un message…", lines=2)
|
| 91 |
|
| 92 |
-
# Pont: textbox cachée qui recevra "oui"/"non"
|
| 93 |
bridge_choice = gr.Textbox(visible=True, elem_id="bridge-choice")
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
# + ouverture popup (client) via paramètre JS de l'événement
|
| 97 |
user_in.submit(
|
| 98 |
fn=on_submit,
|
| 99 |
inputs=[user_in, chat],
|
| 100 |
outputs=[chat, user_in],
|
| 101 |
js="""
|
| 102 |
() => {
|
| 103 |
-
// Ouvre la popup, même si l'asset vient juste d'être injecté (retry 3s)
|
| 104 |
const start = Date.now();
|
| 105 |
(function tick(){
|
| 106 |
const modal = document.getElementById('confirm-modal');
|
|
@@ -115,14 +122,14 @@ with gr.Blocks(title="Démo Chat + Popup", fill_height=True) as demo:
|
|
| 115 |
""",
|
| 116 |
)
|
| 117 |
|
| 118 |
-
#
|
| 119 |
bridge_choice.change(
|
| 120 |
fn=handle_choice,
|
| 121 |
inputs=[bridge_choice, chat],
|
| 122 |
outputs=[chat, bridge_choice],
|
| 123 |
)
|
| 124 |
|
| 125 |
-
# Injection initiale
|
| 126 |
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector])
|
| 127 |
|
| 128 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from gradio_htmlinjector import HTMLInjector
|
| 3 |
|
| 4 |
+
# --- Handler du choix depuis la popup ---
|
| 5 |
def handle_choice(choice: str, chat_history: list[tuple[str, str]]):
|
| 6 |
+
if not choice: # ignore le 2e "change" éventuel (quand on vide le pont)
|
| 7 |
+
return chat_history, ""
|
| 8 |
+
|
| 9 |
+
c = choice.strip().lower()
|
| 10 |
if c in {"oui", "o", "yes", "y"}:
|
| 11 |
+
user_msg = "Oui"
|
| 12 |
+
bot_reply = "✅ Vous avez choisi **OUI**. Action validée."
|
| 13 |
elif c in {"non", "n", "no"}:
|
| 14 |
+
user_msg = "Non"
|
| 15 |
+
bot_reply = "❌ Vous avez choisi **NON**. Action annulée."
|
| 16 |
else:
|
| 17 |
+
# Dans cette démo, la popup ne devrait envoyer que oui/non.
|
| 18 |
+
# On ignore toute autre valeur pour éviter des messages parasites.
|
| 19 |
+
return chat_history, ""
|
| 20 |
+
|
| 21 |
+
chat_history = chat_history + [(user_msg, bot_reply)]
|
| 22 |
+
return chat_history, "" # on vide le pont (déclenche un 2e change, mais ignoré ci-dessus)
|
| 23 |
|
| 24 |
+
# --- Soumission utilisateur : réponse fixe + demande d'ouverture de la popup ---
|
| 25 |
def on_submit(user_msg: str, chat_history: list[tuple[str, str]]):
|
| 26 |
if not user_msg:
|
| 27 |
+
return chat_history, "" # rien
|
| 28 |
+
|
| 29 |
assistant_reply = "Ceci est une réponse de démonstration.\n\nConfirmez-vous l’action ?"
|
| 30 |
chat_history = chat_history + [(user_msg, assistant_reply)]
|
| 31 |
+
return chat_history, "" # l'ouverture de la popup se fait côté JS (param js)
|
| 32 |
|
| 33 |
+
# --- Injection (HTML/CSS/JS) de la popup ---
|
| 34 |
def inject_assets():
|
| 35 |
js = r"""
|
| 36 |
// Pousser une valeur dans un composant Gradio (shadow DOM v4)
|
|
|
|
| 68 |
background: var(--button-primary-background-fill, #2563eb);
|
| 69 |
color: white; border-color: transparent;
|
| 70 |
}
|
| 71 |
+
/* Pont caché mais monté dans le DOM */
|
| 72 |
#bridge-choice { display: none; }
|
| 73 |
"""
|
| 74 |
html = r"""
|
|
|
|
| 91 |
"""
|
| 92 |
return {"css": css, "js": js, "body_html": html}
|
| 93 |
|
| 94 |
+
# --- UI ---
|
| 95 |
with gr.Blocks(title="Démo Chat + Popup", fill_height=True) as demo:
|
| 96 |
html_injector = HTMLInjector()
|
| 97 |
|
| 98 |
chat = gr.Chatbot(label="Chatbot", height=420)
|
| 99 |
user_in = gr.Textbox(placeholder="Tapez un message…", lines=2)
|
| 100 |
|
| 101 |
+
# Pont: textbox cachée qui recevra "oui"/"non"
|
| 102 |
bridge_choice = gr.Textbox(visible=True, elem_id="bridge-choice")
|
| 103 |
|
| 104 |
+
# A) Soumission -> réponse + ouverture popup (client JS, après rendu du message)
|
|
|
|
| 105 |
user_in.submit(
|
| 106 |
fn=on_submit,
|
| 107 |
inputs=[user_in, chat],
|
| 108 |
outputs=[chat, user_in],
|
| 109 |
js="""
|
| 110 |
() => {
|
|
|
|
| 111 |
const start = Date.now();
|
| 112 |
(function tick(){
|
| 113 |
const modal = document.getElementById('confirm-modal');
|
|
|
|
| 122 |
""",
|
| 123 |
)
|
| 124 |
|
| 125 |
+
# B) Choix -> ajoute un nouveau tour (user: Oui/Non, bot: confirmation)
|
| 126 |
bridge_choice.change(
|
| 127 |
fn=handle_choice,
|
| 128 |
inputs=[bridge_choice, chat],
|
| 129 |
outputs=[chat, bridge_choice],
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# Injection initiale de la popup
|
| 133 |
demo.load(fn=inject_assets, inputs=None, outputs=[html_injector])
|
| 134 |
|
| 135 |
demo.launch()
|