File size: 13,667 Bytes
3af7433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae4a477
 
3af7433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65aec2
3af7433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65aec2
 
 
3af7433
d65aec2
 
 
 
 
3af7433
 
 
 
 
 
 
d65aec2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3af7433
 
 
 
 
d65aec2
3af7433
 
d65aec2
3af7433
 
d65aec2
3af7433
d65aec2
3af7433
 
d65aec2
 
 
 
 
 
 
3af7433
 
 
d65aec2
 
 
 
 
 
 
 
 
 
 
 
3af7433
 
 
 
 
 
 
 
d65aec2
 
 
3af7433
 
 
d65aec2
 
 
 
 
3af7433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65aec2
 
3af7433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65aec2
 
 
3af7433
 
 
d65aec2
3af7433
d65aec2
3af7433
d65aec2
3af7433
 
ae4a477
3af7433
 
 
 
 
 
d65aec2
3af7433
 
 
 
 
ae4a477
3af7433
 
 
 
 
d65aec2
 
 
3af7433
 
d65aec2
3af7433
 
 
 
 
 
 
ae4a477
d65aec2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Chat demo for local LLMs using Streamlit.


Run with:
```
streamlit run chat.py --server.address 0.0.0.0
```
"""

import logging
import os

import openai
import regex
import streamlit as st

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def convert_latex_brackets_to_dollars(text):
    """Convert LaTeX bracket notation to dollar notation for Streamlit."""

    def replace_display_latex(match):
        return f"\n<bdi> $$ {match.group(1).strip()} $$ </bdi>\n"

    text = regex.sub(r"(?r)\\\[\s*([^\[\]]+?)\s*\\\]", replace_display_latex, text)

    def replace_paren_latex(match):
        return f" <bdi> $ {match.group(1).strip()} $ </bdi> "

    text = regex.sub(r"(?r)\\\(\s*(.+?)\s*\\\)", replace_paren_latex, text)

    return text


# (CSS injection moved below and applied conditionally based on st.session_state.lang)


@st.cache_resource
def openai_configured():
    return {
        "model": os.getenv("MY_MODEL", "Intel/hebrew-math-tutor-v1"),
        "api_base": os.getenv("AWS_URL", "http://localhost:8111/v1"),
        "api_key": os.getenv("MY_KEY"),
    }


config = openai_configured()


@st.cache_resource
def get_client():
    return openai.OpenAI(api_key=config["api_key"], base_url=config["api_base"])


client = get_client()

# Language toggle state: 'he' (Hebrew) or 'en' (English)
if "lang" not in st.session_state:
    st.session_state.lang = "he"

# Localized UI strings
labels = {
    "he": {
        "title": "מתמטיבוט 🧮",
        "intro": """
ברוכים הבאים לדמו! 💡 כאן תוכלו להתרשם **ממודל השפה החדש** שלנו; מודל בגודל 4 מיליארד פרמטרים שאומן לענות על שאלות מתמטיות בעברית, על המחשב שלכם, ללא חיבור לרשת.

קישור למודל, פרטים נוספים, יצירת קשר ותנאי שימוש:

https://huggingface.co/Intel/hebrew-math-tutor-v1

-----
""",
        "select_label": "בחרו שאלה מוכנה או צרו שאלה חדשה:",
        "new_question": "שאלה חדשה...",
        "text_label": "שאלה:",
        "placeholder": "הזינו את השאלה כאן...",
        "send": "שלח",
        "reset": "שיחה חדשה",
        "toggle_to": "English 🇬🇧",
        "predefined": [
            "שאלה חדשה...",
            " מהו סכום הסדרה הבאה:  1 + 1/2 + 1/4 + 1/8 + ...",
            "פתח את הביטוי: (a-b)^4",
            "פתרו את המשוואה הבאה: sin(2x) = 0.5",
        ],
    },
    "en": {
        "title": "MathBot 🧮",
        "intro": """
Welcome to the demo! 💡 Here you can try our **new language model** — a 4-billion-parameter model trained to answer math questions in Hebrew while maintaining its English capabilities. It runs locally on your machine without requiring an internet connection.

For the model page and more details see:

https://huggingface.co/Intel/hebrew-math-tutor-v1

-----
""",
        "select_label": "Choose a prepared question or create a new one:",
        "new_question": "New question...",
        "text_label": "Question:",
        "placeholder": "Type your question here...",
        "send": "Send",
        "reset": "New Conversation",
        "toggle_to": "עברית 🇮🇱",
        "predefined": [
            "New question...",
            "What is the sum of the series: 1 + 1/2 + 1/4 + 1/8 + ...",
            "Expand the expression: (a-b)^4",
            "Solve the equation: sin(2x) = 0.5",
        ],
    },
}

L = labels[st.session_state.lang]

# Inject language-specific CSS so alignment follows the current UI language
if st.session_state.lang == "he":
    st.markdown(
        """
    <style>
    /* RTL: apply to Streamlit content */
    .stText, .stTextArea textarea, .stTextArea label, .stSelectbox select, .stSelectbox label, .stSelectbox div,
    select, option, [data-testid="stSelectbox"] select, [data-testid="stSelectbox"] option, .stSelectbox > div, .stSelectbox > div > div,
    .stSelectbox [role="listbox"], .stSelectbox [role="option"], div[role="listbox"], div[role="option"] {
        direction: rtl !important;
        text-align: right !important;
    }
    .stChatMessage { direction: rtl !important; text-align: right !important; }
    h1, .stTitle, [data-testid="stHeader"] h1 { direction: rtl !important; text-align: right !important; }
    .stMarkdown p:not(:has(.MathJax)):not(:has(mjx-container)):not(:has(.katex)) { direction: rtl !important; text-align: right !important; }
    .stMarkdown code, .stMarkdown pre { direction: ltr !important; text-align: left !important; }
    .stButton button { direction: rtl !important; }
    </style>
    """,
        unsafe_allow_html=True,
    )
else:
    # Ensure default LTR for English mode (override any residual RTL rules)
    st.markdown(
        """
    <style>
    .stText, .stTextArea textarea, .stTextArea label, .stSelectbox select, .stSelectbox label, .stSelectbox div,
    select, option, [data-testid="stSelectbox"] select, [data-testid="stSelectbox"] option, .stSelectbox > div, .stSelectbox > div > div,
    .stSelectbox [role="listbox"], .stSelectbox [role="option"], div[role="listbox"], div[role="option"] {
        direction: ltr !important;
        text-align: left !important;
    }
    .stChatMessage { direction: ltr !important; text-align: left !important; }
    h1, .stTitle, [data-testid="stHeader"] h1 { direction: ltr !important; text-align: left !important; }
    .stMarkdown code, .stMarkdown pre { direction: ltr !important; text-align: left !important; }
    .stButton button { direction: ltr !important; }
    </style>
    """,
        unsafe_allow_html=True,
    )

# Localized strings/templates for thinking/details and final answer
if st.session_state.lang == "he":
    _dir = "rtl"
    _align = "right"
    _summary_text = "לחץ כדי לראות את תהליך החשיבה"
    _thinking_prefix = "🤔 חושב"
    _thinking_done = "🤔 *תהליך החשיבה הושלם, מכין תשובה...*"
    _final_label = "📝 תשובה סופית:"
else:
    _dir = "ltr"
    _align = "left"
    _summary_text = "Click to view the thinking process"
    _thinking_prefix = "🤔 Thinking"
    _thinking_done = "🤔 *Thinking complete, preparing answer...*"
    _final_label = "📝 Final answer:"

# Helper HTML template for the collapsible thinking/details block
_details_template = (
    '<details dir="{dir}" style="text-align: {align};">'
    "<summary>🤔 <em>{summary}</em></summary>"
    '<div style="white-space: pre-wrap; margin: 10px 0; direction: {dir}; text-align: {align};">{content}</div>'
    "</details>"
)

st.title(L["title"])

st.markdown(L["intro"])

if "chat_history" not in st.session_state:
    st.session_state.chat_history = []

# Predefined options
predefined_options = L["predefined"]

# Dropdown for predefined options
selected_option = st.selectbox(L["select_label"], predefined_options)

# Text area for input
if selected_option == L["new_question"]:
    user_input = st.text_area(
        L["text_label"], height=100, key="user_input", placeholder=L["placeholder"]
    )
else:
    user_input = st.text_area(L["text_label"], height=100, key="user_input", value=selected_option)

# Buttons layout: Reset | Language Toggle | Send
col_left, col_mid, col_right = st.columns([4, 2, 4])

with col_left:
    if st.button(L["reset"], type="secondary", use_container_width=True):
        st.session_state.chat_history = []
        st.rerun()

with col_mid:
    # Button shows the language to switch TO (e.g. 'English' when current is Hebrew)
    if st.button(L["toggle_to"], use_container_width=True):
        st.session_state.lang = "en" if st.session_state.lang == "he" else "he"
        st.rerun()

with col_right:
    # Guard against None from text_area and ensure non-empty trimmed input
    send_clicked = st.button(L["send"], type="primary", use_container_width=True) and (
        user_input and user_input.strip()
    )

if send_clicked:
    st.session_state.chat_history.append(("user", user_input))

    # Create a placeholder for streaming output
    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""

        # System prompt - adapt to UI language; do not force Hebrew when UI is English
        if st.session_state.lang == "he":
            system_prompt = """\
You are a helpful AI assistant specialized in mathematics and problem-solving who can answer math questions with the correct answer.
Answer shortly, not more than 500 tokens, but outline the process step by step.
Answer ONLY in Hebrew!
"""
        else:
            system_prompt = """\
You are a helpful AI assistant specialized in mathematics and problem-solving who can answer math questions with the correct answer.
Answer shortly, not more than 500 tokens, but outline the process step by step.
"""

        # Create messages in proper chat format
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_input},
        ]

        # Build a single string prompt for OpenAI-compatible chat API
        # Keep the special thinking tokens (<think>...</think>) if the remote model supports them
        prompt_messages = messages

        # Stream from OpenAI-compatible API (vllm remote exposing openai-compatible endpoint)
        # Use the chat completions streaming interface
        in_thinking = True
        thinking_content = "<think>"
        final_answer = ""

        try:
            # openai.ChatCompletion.create with stream=True yields chunks with 'choices'
            stream = client.chat.completions.create(
                messages=prompt_messages,
                model=config["model"],
                temperature=0.6,
                max_tokens=2000,
                top_p=0.95,
                stream=True,
                extra_body={"top_k": 20},
            )

            for chunk in stream:
                # Each chunk is a dict; text delta at chunk['choices'][0]['delta'] for newer APIs
                delta = ""
                try:
                    # compatible with OpenAI response structure
                    delta = chunk.choices[0].delta.content
                except Exception:
                    # fallback for older/other shapes; use getattr to avoid dict-specific calls
                    delta = getattr(chunk, "text", None) or "HI "

                if not delta:
                    continue

                full_response += delta

                # Handle thinking markers
                if "<think>" in delta:
                    in_thinking = True

                if in_thinking:
                    thinking_content += delta
                    if "</think>" in delta:
                        in_thinking = False
                        thinking_text = (
                            thinking_content.replace("<think>", "").replace("</think>", "").strip()
                        )
                        display_content = _details_template.format(
                            dir=_dir, align=_align, summary=_summary_text, content=thinking_text
                        )
                        message_placeholder.markdown(display_content + "▌", unsafe_allow_html=True)
                    else:
                        dots = "." * ((len(thinking_content) // 10) % 6)
                        # thinking indicator
                        thinking_indicator = f"""
<div dir="{_dir}" style="padding: 10px; background-color: #f0f2f6; border-radius: 10px; border-right: 4px solid #1f77b4; text-align: {_align};">
    <p style="margin: 0; color: #1f77b4; font-style: italic;">
        {_thinking_prefix}{dots}
    </p>
</div>
"""
                        message_placeholder.markdown(thinking_indicator, unsafe_allow_html=True)
                else:
                    # Final answer streaming
                    final_answer += delta
                    converted_answer = convert_latex_brackets_to_dollars(final_answer)
                    message_placeholder.markdown(
                        f"{_thinking_done}\n\n**{_final_label}**\n\n" + converted_answer + "▌",
                        unsafe_allow_html=True,
                    )
        except Exception as e:
            # Show an error to the user
            message_placeholder.markdown(f"**Error contacting remote model:** {e}")

        # Final rendering: if there was thinking content include it
        if thinking_content and "</think>" in thinking_content:
            thinking_text = thinking_content.replace("<think>", "").replace("</think>", "").strip()
            message_placeholder.empty()
            with message_placeholder.container():
                thinking_html = _details_template.format(
                    dir=_dir, align=_align, summary=_summary_text, content=thinking_text
                )
                st.markdown(thinking_html, unsafe_allow_html=True)
                st.markdown(
                    f'<div dir="{_dir}" style="text-align: {_align}; margin: 10px 0;"><strong>{_final_label}</strong></div>',
                    unsafe_allow_html=True,
                )
                converted_answer = convert_latex_brackets_to_dollars(final_answer or full_response)
                st.markdown(converted_answer, unsafe_allow_html=True)
        else:
            converted_response = convert_latex_brackets_to_dollars(final_answer or full_response)
            message_placeholder.markdown(converted_response, unsafe_allow_html=True)

    st.session_state.chat_history.append(("assistant", final_answer or full_response))