File size: 3,230 Bytes
52eea3a
7e68852
 
e0c473c
52eea3a
7e68852
 
e0c473c
52eea3a
390f796
c2db707
390f796
 
 
 
 
 
 
 
7f7c489
 
 
 
 
 
 
 
 
 
 
 
390f796
bad3b4b
 
390f796
bad3b4b
52eea3a
390f796
 
 
 
 
 
 
 
cad5bb4
390f796
 
cad5bb4
 
390f796
 
 
 
 
 
 
 
 
 
 
 
bad3b4b
390f796
 
 
 
 
 
bad3b4b
cad5bb4
bad3b4b
 
 
52eea3a
bad3b4b
 
 
 
 
390f796
bad3b4b
 
390f796
 
bad3b4b
390f796
 
bad3b4b
390f796
bad3b4b
390f796
bad3b4b
390f796
 
 
bad3b4b
390f796
bad3b4b
390f796
 
bad3b4b
 
 
 
c2db707
e0c473c
d207a2b
 
 
 
 
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
import gradio as gr
import os
import sys

# Add backend to path for imports
backend_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'backend')
sys.path.insert(0, backend_path)

from backend.models.character_manager import CharacterManager
import asyncio

# Global character manager
character_manager = None

def initialize_models():
    """Initialize the character manager"""
    global character_manager
    if character_manager is None:
        print("πŸ”„ Initializing character manager...")
        try:
            character_manager = CharacterManager()
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            loop.run_until_complete(character_manager.initialize())
            loop.close()
            print("βœ… Models loaded successfully!")
        except Exception as e:
            print(f"❌ Error initializing models: {e}")
            import traceback
            traceback.print_exc()
            raise

def chat_function(message, character_id, history):
    """Handle chat with character - returns updated history and empty message"""
    if not message or not message.strip():
        return history, ""
    
    # Initialize if needed
    if character_manager is None:
        initialize_models()
    
    try:
        # Convert history for context
        conversation_history = []
        if history:
            for user_msg, bot_msg in history[-3:]:
                if user_msg:
                    conversation_history.append({"role": "user", "content": user_msg})
                if bot_msg:
                    conversation_history.append({"role": "assistant", "content": bot_msg})
        
        # Generate response
        response = character_manager.generate_response(
            character_id=character_id,
            user_message=message,
            conversation_history=conversation_history
        )
        
        # Update history
        if history is None:
            history = []
        history.append((message, response))
        return history, ""
        
    except Exception as e:
        error_msg = f"❌ Error: {str(e)}"
        if history is None:
            history = []
        history.append((message, error_msg))
        return history, ""

# Ultra-simple interface to avoid schema issues
with gr.Blocks() as demo:
    gr.Markdown("# 🎭 Roleplay Chat Box")
    
    character_dropdown = gr.Dropdown(
        choices=["moses", "samsung_employee", "jinx"],
        value="moses",
        label="Choose Character"
    )
    
    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Message", placeholder="Type here...")
    
    with gr.Row():
        submit = gr.Button("Send")
        clear = gr.Button("Clear")
    
    # Event handlers - simple format
    submit.click(
        chat_function,
        inputs=[msg, character_dropdown, chatbot],
        outputs=[chatbot, msg]
    )
    
    msg.submit(
        chat_function,
        inputs=[msg, character_dropdown, chatbot],
        outputs=[chatbot, msg]
    )
    
    clear.click(
        lambda: [],
        outputs=[chatbot]
    )

if __name__ == "__main__":
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860
    )