File size: 19,087 Bytes
74a7a39
 
 
 
 
 
 
 
 
 
 
 
d124dac
74a7a39
d124dac
74a7a39
 
 
 
 
 
 
d124dac
 
 
74a7a39
 
 
 
 
 
 
d124dac
 
 
 
 
74a7a39
 
 
 
 
 
d124dac
 
 
 
74a7a39
 
 
 
d124dac
74a7a39
 
 
d124dac
74a7a39
 
d124dac
74a7a39
 
 
d124dac
 
 
 
74a7a39
 
d124dac
 
 
 
74a7a39
 
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a7a39
 
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a7a39
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a7a39
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a7a39
 
d124dac
 
 
 
 
 
74a7a39
 
d124dac
 
 
 
 
 
 
 
 
74a7a39
d124dac
74a7a39
 
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a7a39
 
 
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d246278
 
 
d124dac
d246278
d124dac
8f9e9ab
d124dac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74a7a39
d124dac
74a7a39
d124dac
 
 
 
 
 
 
 
74a7a39
d124dac
 
74a7a39
d124dac
 
 
 
 
d246278
d124dac
74a7a39
d124dac
 
74a7a39
 
d124dac
 
 
 
 
74a7a39
d124dac
 
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
import os
import re
import json
import pickle
from urllib.parse import quote

import numpy as np
import gradio as gr
from rank_bm25 import BM25Okapi
from sentence_transformers import SentenceTransformer
from openai import OpenAI

# =====================================================
# PATHS
# =====================================================
BUILD_DIR = "brainchat_build"
CHUNKS_PATH = os.path.join(BUILD_DIR, "chunks.pkl")
TOKENS_PATH = os.path.join(BUILD_DIR, "tokenized_chunks.pkl")
EMBED_PATH = os.path.join(BUILD_DIR, "embeddings.npy")
CONFIG_PATH = os.path.join(BUILD_DIR, "config.json")
LOGO_FILE = "Brain chat-09.png"

# =====================================================
# GLOBALS
# =====================================================
EMBED_MODEL = None
BM25 = None
CHUNKS = None
EMBEDDINGS = None
CLIENT = None


# =====================================================
# LOADERS
# =====================================================
def tokenize(text: str):
    return re.findall(r"\w+", text.lower(), flags=re.UNICODE)


def ensure_loaded():
    global EMBED_MODEL, BM25, CHUNKS, EMBEDDINGS, CLIENT

    if CHUNKS is None:
        for path in [CHUNKS_PATH, TOKENS_PATH, EMBED_PATH, CONFIG_PATH]:
            if not os.path.exists(path):
                raise FileNotFoundError(f"Missing file: {path}")

        with open(CHUNKS_PATH, "rb") as f:
            CHUNKS = pickle.load(f)

        with open(TOKENS_PATH, "rb") as f:
            tokenized_chunks = pickle.load(f)

        EMBEDDINGS = np.load(EMBED_PATH)

        with open(CONFIG_PATH, "r", encoding="utf-8") as f:
            cfg = json.load(f)

        BM25 = BM25Okapi(tokenized_chunks)
        EMBED_MODEL = SentenceTransformer(cfg["embedding_model"])

    if CLIENT is None:
        api_key = os.getenv("OPENAI_API_KEY")
        if not api_key:
            raise ValueError("OPENAI_API_KEY is missing in Hugging Face Space Secrets.")
        CLIENT = OpenAI(api_key=api_key)


# =====================================================
# RETRIEVAL
# =====================================================
def search_hybrid(query: str, shortlist_k: int = 20, final_k: int = 3):
    ensure_loaded()

    query_tokens = tokenize(query)
    bm25_scores = BM25.get_scores(query_tokens)

    shortlist_idx = np.argsort(bm25_scores)[::-1][:shortlist_k]
    shortlist_embeddings = EMBEDDINGS[shortlist_idx]

    qvec = EMBED_MODEL.encode([query], normalize_embeddings=True).astype("float32")[0]
    dense_scores = shortlist_embeddings @ qvec

    rerank_order = np.argsort(dense_scores)[::-1][:final_k]
    final_idx = shortlist_idx[rerank_order]

    return [CHUNKS[int(i)] for i in final_idx]


def build_context(records):
    blocks = []
    for i, r in enumerate(records, start=1):
        blocks.append(
            f"""[Source {i}]
Book: {r['book']}
Section: {r['section_title']}
Pages: {r['page_start']}-{r['page_end']}
Text:
{r['text']}"""
        )
    return "\n\n".join(blocks)


def make_sources(records):
    seen = set()
    lines = []
    for r in records:
        key = (r["book"], r["section_title"], r["page_start"], r["page_end"])
        if key in seen:
            continue
        seen.add(key)
        lines.append(
            f"• {r['book']} | {r['section_title']} | pp. {r['page_start']}-{r['page_end']}"
        )
    return "\n".join(lines)


# =====================================================
# PROMPTS
# =====================================================
def language_instruction(language_mode: str) -> str:
    if language_mode == "English":
        return "Answer only in English."
    if language_mode == "Spanish":
        return "Answer only in Spanish."
    if language_mode == "Bilingual":
        return "Answer first in English, then provide a Spanish version under the heading 'Español:'."
    return (
        "If the user's message is in Spanish, answer in Spanish. "
        "If the user's message is in English, answer in English."
    )


def choose_quiz_count(user_text: str, selector: str) -> int:
    if selector in {"3", "5", "7"}:
        return int(selector)

    t = user_text.lower()
    if any(k in t for k in ["mock test", "final exam", "exam practice", "full test"]):
        return 7
    if any(k in t for k in ["detailed", "revision", "comprehensive", "study"]):
        return 5
    return 3


def build_tutor_prompt(mode: str, language_mode: str, question: str, context: str) -> str:
    mode_map = {
        "Explain": (
            "Explain clearly like a friendly tutor using simple language. "
            "Use short headings if useful."
        ),
        "Detailed": (
            "Give a fuller and more detailed explanation. Include concept, key points, and clinical relevance when supported by context."
        ),
        "Short Notes": (
            "Answer in concise revision-note format using short bullet points."
        ),
        "Flashcards": (
            "Create 6 flashcards in Q/A format using only the provided context."
        ),
        "Case-Based": (
            "Create a short clinical scenario and explain it clearly using the provided context."
        )
    }

    return f"""
You are BrainChat, an interactive neurology and neuroanatomy tutor.

Rules:
- Use only the provided context from the books.
- If the answer is not supported by the context, say exactly:
  Not found in the course material.
- Be accurate and student-friendly.
- Do not invent facts outside the context.
- {language_instruction(language_mode)}

Teaching style:
{mode_map[mode]}

Context:
{context}

Question:
{question}
""".strip()


def build_quiz_generation_prompt(language_mode: str, topic: str, context: str, n_questions: int) -> str:
    return f"""
You are BrainChat, an interactive tutor.

Rules:
- Use only the provided context.
- Create exactly {n_questions} quiz questions.
- Questions should be short and clear.
- Also create a short answer key.
- Return valid JSON only.
- {language_instruction(language_mode)}

Required JSON format:
{{
  "title": "short quiz title",
  "questions": [
    {{"q": "question 1", "answer_key": "expected short answer"}},
    {{"q": "question 2", "answer_key": "expected short answer"}}
  ]
}}

Context:
{context}

Topic:
{topic}
""".strip()


def build_quiz_evaluation_prompt(language_mode: str, quiz_data: dict, user_answers: str) -> str:
    quiz_json = json.dumps(quiz_data, ensure_ascii=False)
    return f"""
You are BrainChat, an interactive tutor.

Evaluate the student's answers fairly using the quiz answer key.
Give:
- total score
- per-question feedback
- one short improvement suggestion

Rules:
- Accept semantically correct answers even if wording differs.
- Return valid JSON only.
- {language_instruction(language_mode)}

Required JSON format:
{{
  "score_obtained": 0,
  "score_total": 0,
  "summary": "short overall feedback",
  "results": [
    {{
      "question": "question text",
      "student_answer": "student answer",
      "result": "Correct / Partially Correct / Incorrect",
      "feedback": "short explanation"
    }}
  ]
}}

Quiz data:
{quiz_json}

Student answers:
{user_answers}
""".strip()


# =====================================================
# OPENAI HELPERS
# =====================================================
def chat_text(prompt: str) -> str:
    resp = CLIENT.chat.completions.create(
        model="gpt-4o-mini",
        temperature=0.2,
        messages=[
            {"role": "system", "content": "You are a helpful educational assistant."},
            {"role": "user", "content": prompt},
        ],
    )
    return resp.choices[0].message.content.strip()


def chat_json(prompt: str) -> dict:
    resp = CLIENT.chat.completions.create(
        model="gpt-4o-mini",
        temperature=0.2,
        response_format={"type": "json_object"},
        messages=[
            {"role": "system", "content": "Return only valid JSON."},
            {"role": "user", "content": prompt},
        ],
    )
    return json.loads(resp.choices[0].message.content)


# =====================================================
# HTML RENDERING
# =====================================================
def md_to_html(text: str) -> str:
    safe = (
        text.replace("&", "&")
            .replace("<", "&lt;")
            .replace(">", "&gt;")
    )
    safe = re.sub(r"\*\*(.+?)\*\*", r"<strong>\1</strong>", safe)
    safe = safe.replace("\n", "<br>")
    return safe


def render_chat(history):
    if not history:
        return """
        <div class="empty-chat">
            <div class="empty-chat-text">
                Ask a question, choose a tutor mode, or start a quiz.
            </div>
        </div>
        """

    rows = []
    for msg in history:
        role = msg["role"]
        content = md_to_html(msg["content"])

        if role == "user":
            rows.append(
                f'<div class="msg-row user-row"><div class="msg-bubble user-bubble">{content}</div></div>'
            )
        else:
            rows.append(
                f'<div class="msg-row bot-row"><div class="msg-bubble bot-bubble">{content}</div></div>'
            )

    return f'<div class="chat-wrap">{"".join(rows)}</div>'


def detect_logo_url():
    if os.path.exists(LOGO_FILE):
        return f"/gradio_api/file={quote(LOGO_FILE)}"
    return None


def render_header():
    logo_url = detect_logo_url()
    if logo_url:
        logo_html = f"""
        <img src="{logo_url}" alt="BrainChat Logo"
             style="width:120px;height:120px;object-fit:contain;display:block;margin:0 auto;">
        """
    else:
        logo_html = """
        <div style="
            width:120px;height:120px;border-radius:50%;
            background:#efe85a;display:flex;align-items:center;justify-content:center;
            font-weight:700;text-align:center;margin:0 auto;">
            BRAIN<br>CHAT
        </div>
        """

    return f"""
    <div class="hero-card">
        <div class="hero-inner">
            <div class="hero-logo">{logo_html}</div>
            <div class="hero-title">BrainChat</div>
            <div class="hero-subtitle">
                Interactive neurology and neuroanatomy tutor based on your uploaded books
            </div>
        </div>
    </div>
    """


# =====================================================
# MAIN LOGIC
# =====================================================
def answer_question(message, history, mode, language_mode, quiz_count_mode, show_sources, quiz_state):
    if history is None:
        history = []
    if quiz_state is None:
        quiz_state = {
            "active": False,
            "topic": None,
            "quiz_data": None,
            "language_mode": "Auto"
        }

    if not message or not message.strip():
        return history, render_chat(history), quiz_state, ""

    try:
        ensure_loaded()
        user_text = message.strip()
        history = history + [{"role": "user", "content": user_text}]

        # -------------------------------
        # QUIZ EVALUATION
        # -------------------------------
        if quiz_state.get("active", False):
            evaluation_prompt = build_quiz_evaluation_prompt(
                quiz_state["language_mode"],
                quiz_state["quiz_data"],
                user_text
            )
            evaluation = chat_json(evaluation_prompt)

            lines = []
            lines.append(f"**Score:** {evaluation['score_obtained']}/{evaluation['score_total']}")
            lines.append("")
            lines.append(f"**Overall feedback:** {evaluation['summary']}")
            lines.append("")
            lines.append("**Question-wise evaluation:**")

            for item in evaluation["results"]:
                lines.append("")
                lines.append(f"**Q:** {item['question']}")
                lines.append(f"**Your answer:** {item['student_answer']}")
                lines.append(f"**Result:** {item['result']}")
                lines.append(f"**Feedback:** {item['feedback']}")

            final_answer = "\n".join(lines)
            history = history + [{"role": "assistant", "content": final_answer}]

            quiz_state = {
                "active": False,
                "topic": None,
                "quiz_data": None,
                "language_mode": language_mode
            }

            return history, render_chat(history), quiz_state, ""

        # -------------------------------
        # NORMAL RETRIEVAL
        # -------------------------------
        records = search_hybrid(user_text, shortlist_k=20, final_k=3)
        context = build_context(records)

        # -------------------------------
        # QUIZ GENERATION
        # -------------------------------
        if mode == "Quiz Me":
            n_questions = choose_quiz_count(user_text, quiz_count_mode)
            prompt = build_quiz_generation_prompt(language_mode, user_text, context, n_questions)
            quiz_data = chat_json(prompt)

            lines = []
            lines.append(f"**{quiz_data.get('title', 'Quiz')}**")
            lines.append("")
            lines.append("Please answer the following questions in one message.")
            lines.append("You can reply in numbered format, for example:")
            lines.append("1. ...")
            lines.append("2. ...")
            lines.append("")
            lines.append(f"**Total questions: {len(quiz_data['questions'])}**")
            lines.append("")

            for i, q in enumerate(quiz_data["questions"], start=1):
                lines.append(f"**Q{i}.** {q['q']}")

            if show_sources:
                lines.append("\n---\n**Topic sources used to create the quiz:**")
                lines.append(make_sources(records))

            assistant_text = "\n".join(lines)
            history = history + [{"role": "assistant", "content": assistant_text}]

            quiz_state = {
                "active": True,
                "topic": user_text,
                "quiz_data": quiz_data,
                "language_mode": language_mode
            }

            return history, render_chat(history), quiz_state, ""

        # -------------------------------
        # OTHER MODES
        # -------------------------------
        prompt = build_tutor_prompt(mode, language_mode, user_text, context)
        answer = chat_text(prompt)

        if show_sources:
            answer += "\n\n---\n**Sources used:**\n" + make_sources(records)

        history = history + [{"role": "assistant", "content": answer}]
        return history, render_chat(history), quiz_state, ""

    except Exception as e:
        history = history + [{"role": "assistant", "content": f"Error: {str(e)}"}]
        quiz_state["active"] = False
        return history, render_chat(history), quiz_state, ""


def clear_all():
    empty_history = []
    empty_quiz = {
        "active": False,
        "topic": None,
        "quiz_data": None,
        "language_mode": "Auto"
    }
    return empty_history, render_chat(empty_history), empty_quiz, ""


# =====================================================
# CSS
# =====================================================
CSS = """
body, .gradio-container {
    background: #dcdcdc !important;
    font-family: Arial, Helvetica, sans-serif !important;
}
footer { display: none !important; }

.hero-card {
    max-width: 900px;
    margin: 18px auto 14px auto;
    border-radius: 28px;
    background: linear-gradient(180deg, #e8c7d4 0%, #a55ca2 48%, #2b0c46 100%);
    padding: 22px 22px 18px 22px;
}
.hero-inner { text-align: center; }
.hero-title {
    color: white;
    font-size: 34px;
    font-weight: 800;
    margin-top: 6px;
}
.hero-subtitle {
    color: white;
    opacity: 0.92;
    font-size: 16px;
    margin-top: 6px;
}

.chat-panel {
    max-width: 900px;
    margin: 0 auto;
    background: white;
    border-radius: 22px;
    padding: 16px;
    min-height: 420px;
    box-shadow: 0 6px 18px rgba(0,0,0,0.08);
}

.chat-wrap {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.msg-row {
    display: flex;
    width: 100%;
}

.user-row {
    justify-content: flex-end;
}

.bot-row {
    justify-content: flex-start;
}

.msg-bubble {
    max-width: 80%;
    padding: 14px 16px;
    border-radius: 18px;
    line-height: 1.5;
    font-size: 15px;
    word-wrap: break-word;
}

.user-bubble {
    background: #e9d8ff;
    color: #111;
    border-bottom-right-radius: 6px;
}

.bot-bubble {
    background: #f7f3a1;
    color: #111;
    border-bottom-left-radius: 6px;
}

.empty-chat {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 360px;
}

.empty-chat-text {
    color: #777;
    font-size: 16px;
    text-align: center;
}

.controls-wrap {
    max-width: 900px;
    margin: 0 auto;
}
"""


# =====================================================
# UI
# =====================================================
with gr.Blocks() as demo:
    history_state = gr.State([])
    quiz_state = gr.State({
        "active": False,
        "topic": None,
        "quiz_data": None,
        "language_mode": "Auto"
    })

    gr.HTML(render_header())

    with gr.Row(elem_classes="controls-wrap"):
        mode = gr.Dropdown(
            choices=["Explain", "Detailed", "Short Notes", "Flashcards", "Case-Based", "Quiz Me"],
            value="Explain",
            label="Tutor Mode"
        )
        language_mode = gr.Dropdown(
            choices=["Auto", "English", "Spanish", "Bilingual"],
            value="Auto",
            label="Answer Language"
        )

    with gr.Row(elem_classes="controls-wrap"):
        quiz_count_mode = gr.Dropdown(
            choices=["Auto", "3", "5", "7"],
            value="Auto",
            label="Quiz Questions"
        )
        show_sources = gr.Checkbox(value=True, label="Show Sources")

    gr.Markdown("""
**How to use**
- Choose a **Tutor Mode**
- Then type a topic or question
- For **Quiz Me**, type a topic such as: `cranial nerves`
- The system will ask questions, and your **next message will be evaluated automatically**
""")

    chat_html = gr.HTML(render_chat([]), elem_classes="chat-panel")

    with gr.Row(elem_classes="controls-wrap"):
        msg = gr.Textbox(
            placeholder="Ask a question or type a topic...",
            lines=1,
            show_label=False,
            scale=8
        )
        send_btn = gr.Button("Send", scale=1)

    with gr.Row(elem_classes="controls-wrap"):
        clear_btn = gr.Button("Clear Chat")

    msg.submit(
        answer_question,
        inputs=[msg, history_state, mode, language_mode, quiz_count_mode, show_sources, quiz_state],
        outputs=[history_state, chat_html, quiz_state, msg]
    )

    send_btn.click(
        answer_question,
        inputs=[msg, history_state, mode, language_mode, quiz_count_mode, show_sources, quiz_state],
        outputs=[history_state, chat_html, quiz_state, msg]
    )

    clear_btn.click(
        clear_all,
        inputs=[],
        outputs=[history_state, chat_html, quiz_state, msg]
    )

if __name__ == "__main__":
    demo.launch(css=CSS)