Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -38,7 +38,7 @@ def generate_response(chat_history, user_input):
|
|
| 38 |
Generates a response from the model based on the chat history and user input.
|
| 39 |
"""
|
| 40 |
# Append user input to chat history
|
| 41 |
-
chat_history.append(("User", user_input))
|
| 42 |
|
| 43 |
# Prepare messages for the model
|
| 44 |
messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] + [
|
|
@@ -84,13 +84,60 @@ def create_chat_interface():
|
|
| 84 |
Creates the Gradio interface for the chat application.
|
| 85 |
"""
|
| 86 |
with gr.Blocks() as chat_app:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
gr.Markdown("## Math Hint Chat")
|
| 88 |
gr.Markdown(
|
| 89 |
"This chatbot provides hints and step-by-step guidance for solving math problems. "
|
| 90 |
-
"It will not reveal the final answer."
|
| 91 |
)
|
| 92 |
|
| 93 |
-
chatbot = gr.Chatbot(label="Math Tutor Chat")
|
| 94 |
user_input = gr.Textbox(
|
| 95 |
placeholder="Ask your math question here (e.g., Solve for x: 4x + 5 = 6x + 7)",
|
| 96 |
label="Your Query"
|
|
|
|
| 38 |
Generates a response from the model based on the chat history and user input.
|
| 39 |
"""
|
| 40 |
# Append user input to chat history
|
| 41 |
+
chat_history.append(("User", user_input + "\n\n strinctly prohibited to reveal answer only provide hints and guidelines to solve this"))
|
| 42 |
|
| 43 |
# Prepare messages for the model
|
| 44 |
messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] + [
|
|
|
|
| 84 |
Creates the Gradio interface for the chat application.
|
| 85 |
"""
|
| 86 |
with gr.Blocks() as chat_app:
|
| 87 |
+
gr.HTML("""
|
| 88 |
+
<!-- Include KaTeX CSS and JS -->
|
| 89 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
|
| 90 |
+
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.js"></script>
|
| 91 |
+
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"></script>
|
| 92 |
+
<script>
|
| 93 |
+
// Preprocess LaTeX content
|
| 94 |
+
function preprocessLatex(text) {
|
| 95 |
+
// Convert block math `[ ... ]` to `\\[ ... \\]`
|
| 96 |
+
text = text.replace(/\[([^\[\]]+)\]/g, '\\[$1\\]');
|
| 97 |
+
// Convert inline math `( ... )` to `\\( ... \\)`
|
| 98 |
+
text = text.replace(/\(([^\(\)]+)\)/g, '\\($1\\)');
|
| 99 |
+
return text;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
// Render LaTeX only for elements requiring math
|
| 103 |
+
function renderChatLatex(mutationsList) {
|
| 104 |
+
for (const mutation of mutationsList) {
|
| 105 |
+
if (mutation.type === "childList") {
|
| 106 |
+
mutation.addedNodes.forEach((node) => {
|
| 107 |
+
if (node.nodeType === 1) { // Ensure it's an element node
|
| 108 |
+
// Check if the content needs LaTeX rendering
|
| 109 |
+
if (node.innerHTML.match(/\\\(|\\\[|\$|\[|\(/)) {
|
| 110 |
+
node.innerHTML = preprocessLatex(node.innerHTML);
|
| 111 |
+
renderMathInElement(node, {
|
| 112 |
+
delimiters: [
|
| 113 |
+
{ left: "\\(", right: "\\)", display: false },
|
| 114 |
+
{ left: "\\[", right: "\\]", display: true },
|
| 115 |
+
{ left: "$$", right: "$$", display: true },
|
| 116 |
+
{ left: "$", right: "$", display: false }
|
| 117 |
+
]
|
| 118 |
+
});
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
});
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
// Setup MutationObserver
|
| 127 |
+
document.addEventListener("DOMContentLoaded", () => {
|
| 128 |
+
const chatContainer = document.querySelector("#chat-container");
|
| 129 |
+
const observer = new MutationObserver(renderChatLatex);
|
| 130 |
+
observer.observe(chatContainer, { childList: true, subtree: true });
|
| 131 |
+
});
|
| 132 |
+
</script>
|
| 133 |
+
""")
|
| 134 |
+
|
| 135 |
gr.Markdown("## Math Hint Chat")
|
| 136 |
gr.Markdown(
|
| 137 |
"This chatbot provides hints and step-by-step guidance for solving math problems. "
|
|
|
|
| 138 |
)
|
| 139 |
|
| 140 |
+
chatbot = gr.Chatbot(label="Math Tutor Chat",, elem_id="chat-container")
|
| 141 |
user_input = gr.Textbox(
|
| 142 |
placeholder="Ask your math question here (e.g., Solve for x: 4x + 5 = 6x + 7)",
|
| 143 |
label="Your Query"
|