Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,38 @@ from chatbot import chatbot
|
|
| 8 |
|
| 9 |
css = Path("styles.css").read_text()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# -------------------------
|
| 12 |
# Gradio UI
|
| 13 |
# -------------------------
|
|
@@ -42,6 +74,9 @@ theme = (
|
|
| 42 |
|
| 43 |
with gr.Blocks(
|
| 44 |
title="Saint James Backpackers Assistant",
|
|
|
|
|
|
|
|
|
|
| 45 |
) as demo:
|
| 46 |
|
| 47 |
gr.HTML(
|
|
@@ -107,71 +142,22 @@ I can help you with:
|
|
| 107 |
|
| 108 |
answer.render()
|
| 109 |
|
| 110 |
-
|
| 111 |
-
# Ask button
|
| 112 |
-
# -------------------------
|
| 113 |
-
|
| 114 |
-
ask_event = ask_button.click(
|
| 115 |
fn=chatbot,
|
| 116 |
inputs=question,
|
| 117 |
outputs=answer,
|
| 118 |
)
|
| 119 |
|
| 120 |
-
|
| 121 |
-
fn=None,
|
| 122 |
-
inputs=None,
|
| 123 |
-
outputs=None,
|
| 124 |
-
js="""
|
| 125 |
-
() => {
|
| 126 |
-
document.getElementById("answer")
|
| 127 |
-
?.scrollIntoView({
|
| 128 |
-
behavior: "smooth",
|
| 129 |
-
block: "start"
|
| 130 |
-
});
|
| 131 |
-
}
|
| 132 |
-
"""
|
| 133 |
-
)
|
| 134 |
-
|
| 135 |
-
# -------------------------
|
| 136 |
-
# Press Enter
|
| 137 |
-
# -------------------------
|
| 138 |
-
|
| 139 |
-
submit_event = question.submit(
|
| 140 |
fn=chatbot,
|
| 141 |
inputs=question,
|
| 142 |
outputs=answer,
|
| 143 |
)
|
| 144 |
|
| 145 |
-
submit_event.then(
|
| 146 |
-
fn=None,
|
| 147 |
-
inputs=None,
|
| 148 |
-
outputs=None,
|
| 149 |
-
js="""
|
| 150 |
-
() => {
|
| 151 |
-
document.getElementById("answer")
|
| 152 |
-
?.scrollIntoView({
|
| 153 |
-
behavior: "smooth",
|
| 154 |
-
block: "start"
|
| 155 |
-
});
|
| 156 |
-
}
|
| 157 |
-
"""
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
# -------------------------
|
| 161 |
-
# Clear
|
| 162 |
-
# -------------------------
|
| 163 |
-
|
| 164 |
clear_button.click(
|
| 165 |
lambda: ("", ""),
|
| 166 |
outputs=[question, answer],
|
| 167 |
)
|
| 168 |
|
| 169 |
-
# -------------------------
|
| 170 |
-
# Entry point
|
| 171 |
-
# -------------------------
|
| 172 |
-
|
| 173 |
if __name__ == "__main__":
|
| 174 |
-
demo.launch(
|
| 175 |
-
theme=theme,
|
| 176 |
-
css=css,
|
| 177 |
-
)
|
|
|
|
| 8 |
|
| 9 |
css = Path("styles.css").read_text()
|
| 10 |
|
| 11 |
+
# -------------------------
|
| 12 |
+
# JavaScript
|
| 13 |
+
# -------------------------
|
| 14 |
+
|
| 15 |
+
js = """
|
| 16 |
+
function () {
|
| 17 |
+
const answer = document.getElementById("answer");
|
| 18 |
+
if (!answer) return;
|
| 19 |
+
|
| 20 |
+
let previous = "";
|
| 21 |
+
|
| 22 |
+
const observer = new MutationObserver(() => {
|
| 23 |
+
const current = answer.innerText;
|
| 24 |
+
|
| 25 |
+
if (current && current !== previous) {
|
| 26 |
+
previous = current;
|
| 27 |
+
|
| 28 |
+
answer.scrollIntoView({
|
| 29 |
+
behavior: "smooth",
|
| 30 |
+
block: "start"
|
| 31 |
+
});
|
| 32 |
+
}
|
| 33 |
+
});
|
| 34 |
+
|
| 35 |
+
observer.observe(answer, {
|
| 36 |
+
childList: true,
|
| 37 |
+
subtree: true,
|
| 38 |
+
characterData: true
|
| 39 |
+
});
|
| 40 |
+
}
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
# -------------------------
|
| 44 |
# Gradio UI
|
| 45 |
# -------------------------
|
|
|
|
| 74 |
|
| 75 |
with gr.Blocks(
|
| 76 |
title="Saint James Backpackers Assistant",
|
| 77 |
+
css=css,
|
| 78 |
+
js=js,
|
| 79 |
+
theme=theme,
|
| 80 |
) as demo:
|
| 81 |
|
| 82 |
gr.HTML(
|
|
|
|
| 142 |
|
| 143 |
answer.render()
|
| 144 |
|
| 145 |
+
ask_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
fn=chatbot,
|
| 147 |
inputs=question,
|
| 148 |
outputs=answer,
|
| 149 |
)
|
| 150 |
|
| 151 |
+
question.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
fn=chatbot,
|
| 153 |
inputs=question,
|
| 154 |
outputs=answer,
|
| 155 |
)
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
clear_button.click(
|
| 158 |
lambda: ("", ""),
|
| 159 |
outputs=[question, answer],
|
| 160 |
)
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
if __name__ == "__main__":
|
| 163 |
+
demo.launch()
|
|
|
|
|
|
|
|
|