Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import os
|
2 |
from pathlib import Path
|
|
|
3 |
|
4 |
# Disable Chroma telemetry (optional)
|
5 |
os.environ["CHROMA_TELEMETRY_ENABLED"] = "false"
|
@@ -42,11 +43,30 @@ def chat_fn(message, history):
|
|
42 |
sources = None
|
43 |
if sources:
|
44 |
answer = f"{answer}\n\nπ Sources: {', '.join(sources)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
history.append((message, answer))
|
46 |
return history, ""
|
47 |
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
# chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
|
51 |
chatbot = gr.Chatbot(elem_id="chatbot", type="tuples")
|
52 |
txt = gr.Textbox(show_label=False, placeholder="Ask about the course...")
|
|
|
1 |
import os
|
2 |
from pathlib import Path
|
3 |
+
import re
|
4 |
|
5 |
# Disable Chroma telemetry (optional)
|
6 |
os.environ["CHROMA_TELEMETRY_ENABLED"] = "false"
|
|
|
43 |
sources = None
|
44 |
if sources:
|
45 |
answer = f"{answer}\n\nπ Sources: {', '.join(sources)}"
|
46 |
+
|
47 |
+
def format_answer(answer):
|
48 |
+
# Wrap LaTeX formulas in a span so MathJax can render them
|
49 |
+
answer = re.sub(r"\$\$(.+?)\$\$", r'<span class="math">$$\1$$</span>', answer)
|
50 |
+
return f"<div>{answer}</div>"
|
51 |
+
|
52 |
+
answer = format_answer(answer)
|
53 |
history.append((message, answer))
|
54 |
return history, ""
|
55 |
|
56 |
+
CSS = """
|
57 |
+
* { direction: rtl; text-align: right; font-family: 'Vazir', sans-serif; }
|
58 |
+
.gr-chat-message { white-space: pre-wrap; }
|
59 |
+
.math { font-size: 1.2em; }
|
60 |
+
"""
|
61 |
+
|
62 |
+
with gr.Blocks(css=CSS) as demo:
|
63 |
+
demo.load(lambda: None, [], [], _js="""
|
64 |
+
const script = document.createElement('script');
|
65 |
+
script.src = "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js";
|
66 |
+
document.head.appendChild(script);
|
67 |
+
""")
|
68 |
+
|
69 |
+
gr.Markdown("## π SCR Course Assistant β Chat with course files")
|
70 |
# chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
|
71 |
chatbot = gr.Chatbot(elem_id="chatbot", type="tuples")
|
72 |
txt = gr.Textbox(show_label=False, placeholder="Ask about the course...")
|