File size: 931 Bytes
ef31fc1
fb15347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# /app/components/ChatHistory.py
from __future__ import annotations
from typing import List, Tuple
import gradio as gr

History = List[Tuple[str, str]]  # [("user","hi"), ("bot","hello")]

def to_chatbot_pairs(history: History) -> list[tuple[str, str]]:
    """
    Convert [('user','..'),('bot','..')] into gr.Chatbot expected pairs.
    Pairs are [(user_text, bot_text), ...].
    """
    pairs: list[tuple[str, str]] = []
    buf_user: str | None = None
    for who, text in history:
        if who == "user":
            buf_user = text
        elif who == "bot":
            pairs.append((buf_user or "", text))
            buf_user = None
    return pairs

def build_chat_history(label: str = "Conversation") -> gr.Chatbot:
    """
    Create a Chatbot component (the large chat pane).
    Use .update(value=to_chatbot_pairs(history)) to refresh.
    """
    return gr.Chatbot(label=label, height=360, show_copy_button=True)