File size: 2,836 Bytes
1856369
1f597c7
 
1856369
 
37ececc
1856369
1567745
1f597c7
0bb902d
e2e7692
1f597c7
 
 
0bb902d
1f597c7
 
e2e7692
 
0bb902d
e2e7692
 
 
 
 
 
abcf75a
e2e7692
 
 
 
 
 
abcf75a
 
 
55ee68d
1856369
 
 
 
 
e2e7692
1856369
e2e7692
 
 
 
abcf75a
 
 
 
 
e2e7692
 
abcf75a
 
 
 
 
 
e2e7692
 
 
 
abcf75a
 
 
 
 
e2e7692
abcf75a
 
 
 
 
e2e7692
 
 
 
1856369
e2e7692
 
1856369
e2e7692
 
1856369
e2e7692
 
55ee68d
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
import gradio as gr

from config import DEFAULT_PERSONA
from prompts import PERSONAS
from utils import generate_response


def handle_message(persona, user_input, history):
    """Process a chat message and return updated history.

    Args:
        persona: Selected chat persona
        user_input: User's message
        history: Previous chat messages

    Returns:
        Updated chat history with new messages
    """
    # Validate inputs

    if not persona or persona not in PERSONAS:
        return history + [
            {"role": "assistant", "content": "Please select a valid persona"}
        ]

    if not user_input.strip():
        return history + [{"role": "assistant", "content": "Please enter a message"}]

    try:
        # Get response from model
        return generate_response(PERSONAS[persona], user_input, history)
    except Exception as e:
        # Return error message in proper format
        return history + [{"role": "assistant", "content": f"Error: {str(e)}"}]


with gr.Blocks() as demo:
    gr.Markdown("## 🤖 LLMates: Persona-based Chat Assistant")

    persona = gr.Dropdown(
        choices=list(PERSONAS.keys()), value=DEFAULT_PERSONA, label="Choose Persona"
    )
    chatbox = gr.Chatbot(label="LLMates", type="messages")
    msg = gr.Textbox(label="Type your message...")
    chat_history = gr.State([])

    def user_submit(user_input, history, persona):
        if not user_input.strip():
            return (
                [],
                history,
                "",
            )  # Return empty chatbox update but keep history and clear input

        if not persona or persona not in PERSONAS:
            return (
                [],
                history
                + [{"role": "assistant", "content": "Please select a valid persona"}],
                "",
            )

        try:
            # Get response from model
            new_history = generate_response(PERSONAS[persona], user_input, history)
            return (
                new_history,
                new_history,
                "",
            )  # Clear input after successful submission
        except Exception as e:
            return (
                [],
                history + [{"role": "assistant", "content": f"Error: {str(e)}"}],
                "",
            )

    def clear_history():
        """Clear the chat history and reset message input when persona changes"""
        return ([], [], "")  # Clear chatbox, history state, and message input

    # Clear chat history and reset message input when persona changes
    persona.change(clear_history, outputs=[chatbox, chat_history, msg])

    # Handle message submission
    msg.submit(user_submit, [msg, chat_history, persona], [chatbox, chat_history, msg])

# main function
if __name__ == "__main__":
    demo.launch()