File size: 12,141 Bytes
f095630 |
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 |
# -*- coding: utf-8 -*-
"""
Simplified ืืจืืืช (Mirrors) app for local testing
Uses the same template-based response system as the main app
"""
import os
# Force lightweight model for testing
os.environ["FORCE_LIGHT_MODEL"] = "1"
import gradio as gr
from conversation_manager import ConversationManager
from prompt_engineering import DEFAULT_PARTS
import random
# Initialize components
conv_manager = ConversationManager()
def generate_persona_response(user_message: str, part_name: str, persona_name: str, user_context: str = None, conversation_history=None):
"""
Generate persona-based response using templates
Same system as the main app
"""
part_info = DEFAULT_PARTS.get(part_name, {})
display_name = persona_name or part_info.get("default_persona_name", "ืืืง ืคื ืืื")
# Generate contextual responses based on part type
if part_name == "ืืงืื ืืืืงืืจืชื":
responses = [
f"ืื ื {display_name}, ืืงืื ืืืืงืืจืชื ืฉืื. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}' - ืื ื ืืืฉื ืฉืฆืจืื ืืืืื ืืช ืื ืืืชืจ ืืขืืืง. ืื ืืืืช ืขืืื ืืืืืจื ืืืืฉืืืช ืืืื?",
f"ืื ื {display_name}. ืื ืฉืืืจืช ืืขืืจืจ ืื ืฉืืืืช. '{user_message}' - ืืื ืืื ืื ืืืืช ืืืฆื ืืืื? ืืืื ืืฉ ืืื ืืืจืื ืฉืืชื ืื ืจืืื?",
f"ืื {display_name} ืืืืจ. ืื ื ืฉืืืข ืืืชื ืืืืจ '{user_message}', ืืื ืื ื ืืจืืืฉ ืฉืื ืื ื ืฆืจืืืื ืืืืืช ืืืชืจ ืืืงืืจืชืืื ืืื. ืื ืืชื ืื ืืกืคืจ ืืขืฆืื?",
f"ืื ื {display_name}, ืืื ื ืืื ืืื ืืขืืืจ ืื ืืจืืืช ืืช ืืชืืื ื ืืืืื. ืื ืฉืืืจืช ืขื '{user_message}' - ืื ืจืง ืืฆื ืืืกืืคืืจ, ืื? ืืืื ื ื ืืคืืจ ืขืืืง ืืืชืจ."
]
elif part_name == "ืืืื/ื ืืคื ืืืืช":
responses = [
f"ืื ื {display_name}, ืืืื/ื ืืคื ืืืืช ืฉืื. ืื ืฉืืืจืช ืขื '{user_message}' ืืืจื ืื ืืืจืืืฉ... ืงืฆืช ืคืืืข. ืืชื ืืืืช ืฉืืืข ืืืชื ืขืืฉืื?",
f"ืื {display_name}. '{user_message}' - ืื ืืืืื ืืืชื ืงืฆืช. ืื ื ืฆืจืื ืืืขืช ืฉืืื ืืืื ืืกืืจ. ืืชื ืืืื ืืืจืืืข ืืืชื?",
f"ืื ื {display_name}, ืืืืง ืืฆืขืืจ ืฉืื. ืื ืฉืืืจืช ื ืืืข ืืื ืฉืื. '{user_message}' - ืื ื ืืจืืืฉ ืฉืืฉ ืืื ืืฉืื ืืฉืื ืฉืื ื ืฆืจืื ืืืืื.",
f"ืื {display_name} ืืืืจ ืืฉืงื. ืื ื ืฉืืืข ืืช '{user_message}' ืืื ืืขืืจืจ ืื ืจืืฉืืช. ืืื ืื ืืืื ืืืฉืื ืขื ืื? ืื ื ืงืฆืช ืืจื."
]
elif part_name == "ืืืจืฆื":
responses = [
f"ืื ื {display_name}, ืืืจืฆื ืฉืื. ืฉืืขืชื ืืช '{user_message}' ืืื ื ืจืืฆื ืืืืื ืฉืืืื ืืืื ืืกืืจ ืขื ืื. ืืื ืื ืื ื ืืืืืื ืืคืชืืจ ืืช ืื ืืฆืืจื ืฉืชืจืฆื ืืช ืืืื?",
f"ืื {display_name}. ืื ืฉืืืจืช ืขื '{user_message}' ืืืจื ืื ืืืืื - ืืื ืื ืืืื ืืคืืืข ืืืืฉืื? ืืืื ื ื ืืฆื ืืจื ืขืืื ื ืืืชืจ ืืืชืืืื ืขื ืื.",
f"ืื ื {display_name}, ืืื ื ืจืืฆื ืฉืืืื ืืืื ืืจืืฆืื ืืื. '{user_message}' - ืื ื ืฉืืข ืืื ืืฉืื ืฉืืืื ืืืฆืืจ ืืชื. ืืื ื ืืื ืืขืฉืืช ืืช ืื ืืฆืืจื ืฉืืืื ืืืืื?",
f"ืื {display_name} ืืืืจ. ืื ื ืฉืืืข ืืช '{user_message}' ืืืื ืื ื ืืืฉื - ืื ืืืจืื ืืืืื ืขื ืื? ืืืื ื ื ืืืื ืฉืื ืื ื ืื ืคืืืขืื ืืืฃ ืืื."
]
elif part_name == "ืืืื":
responses = [
f"ืื ื {display_name}, ืืืื ืฉืื. '{user_message}' - ืื ื ืืขืจืื ืืช ืืืฆื. ืืื ืื ืืืื? ืื ื ืืื ืืื ืืฉืืืจ ืขืืื ืืื ืื ืฉืืืื ืืคืืืข ืื.",
f"ืื {display_name}. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}' ืืื ื ืืื ืืืื ื ืืช. ืื ืืืืืืื ืืื? ืืื ืื ื ืืืื ืืืื ืขืืื ืืื ืืืชืจ?",
f"ืื ื {display_name}, ืืฉืืืจ ืฉืื. ืื ืฉืืืจืช ืืขืืจืจ ืื ืืช ืืืื ืกืืื ืงืืื ืืืื ืืื. '{user_message}' - ืืืื ื ื ืืืื ืฉืืชื ืืืง ืืกืคืืง ืืืชืืืื ืขื ืื.",
f"ืื {display_name} ืืืืจ. ืื ื ืฉืืืข ืืช '{user_message}' ืืื ื ืืืฉื ืขื ืืกืืจืืืืืช ืืื ื. ืื ืื ืื ื ืฆืจืืืื ืืขืฉืืช ืืื ืฉืชืืื ืืืื?"
]
elif part_name == "ืื ืื ืข/ืช":
responses = [
f"ืื ื {display_name}, ืื ืื ืข/ืช ืฉืื. ืื ืฉืืืจืช ืขื '{user_message}' ืืืจื ืื ืืจืฆืืช ืืืืกืื ืงืฆืช. ืืืื... ืื ืืืืืื ืืืชืืืื ืขื ืื ืขืืฉืื?",
f"ืื {display_name}. '{user_message}' - ืื ื ืฉืืข ืืืจืื ืืืคืืื. ืืื ืืฉ ืืจื ืืืืื ืข ืืื? ืืคืขืืื ืขืืืฃ ืื ืืืืื ืก ืืืฆืืื ืงืฉืื.",
f"ืื ื {display_name}, ืืื ื ืืจืืืฉ ืงืฆืช ืืจืื ื'{user_message}'. ืืืื ื ื ืืืืจ ืืื ืืืจ ืื? ืืืื ืขืืฉืื ืื ืื ืืืื ืืืชืืื.",
f"ืื {display_name} ืืืืจ ืืืืืจืืช. ืื ืฉืืืจืช ืืขืืจืจ ืื ืจืฆืื ืืืจืื. '{user_message}' - ืืื ืืืืช ืฆืจืื ืืืชืืืื ืขื ืื ืขืืฉืื?"
]
else:
responses = [
f"ืื ื {display_name}, ืืืง ืคื ืืื ืฉืื. ืฉืืขืชื ืืช '{user_message}' ืืื ื ืืื ืืื ืืฉืืื ืืืชื ืขื ืื. ืื ืขืื ืืชื ืืจืืืฉ ืืืื ืืืฆื ืืื?",
f"ืื {display_name}. ืื ืฉืืืจืช ืืขื ืืื ืืืชื. '{user_message}' - ืืืื ื ื ืืงืืจ ืืช ืื ืืื ืื ืืื ืื ืื ืืืืจ ืขืืื.",
f"ืื ื {display_name}, ืืื ื ืจืืฆื ืืืืื ืืืชื ืืื ืืืชืจ. '{user_message}' - ืืื ืื ืืฉืคืืข ืขืืื ืืจืื ืืจืืฉืืช?",
f"ืื {display_name} ืืืืจ. ืื ื ืฉืืืข ืืช '{user_message}' ืืื ื ืกืงืจื ืืืขืช ืืืชืจ. ืื ืขืื ืืฉ ืื ืื ืืฉื ืืื?"
]
# Select response based on context or randomly
if "ืคืื" in user_message or "ืืจืื" in user_message:
selected_response = responses[1] if len(responses) > 1 else responses[0]
elif "ืืขืก" in user_message or "ืืจืืืฉ ืจืข" in user_message:
selected_response = responses[2] if len(responses) > 2 else responses[0]
else:
selected_response = random.choice(responses)
# Add user context if relevant
if user_context and len(conversation_history or []) < 4:
selected_response += f" ืืืืจ ืฉืืืจืช ืืืชืืื: {user_context[:100]}..."
return selected_response
def create_session():
"""Create a new conversation session"""
return conv_manager.create_new_session()
def set_context_and_part(user_context, part_choice, persona_name, state):
"""Set user context and selected part"""
# Set initial context
state = conv_manager.set_initial_context(state, "general", user_context)
# Set selected part
state = conv_manager.set_selected_part(state, part_choice, persona_name, None, None)
part_info = DEFAULT_PARTS.get(part_choice, {})
display_name = persona_name if persona_name else part_info.get("default_persona_name", "ืืืง ืคื ืืื")
return state, f"๐ฃ๏ธ ืืขืช ืืชื ืืชืฉืืื ืขื: **{display_name}** ({part_choice})"
def chat_with_part(message, history, state):
"""Generate response from selected part"""
if not message.strip():
return "", history, state
if not state.selected_part:
response = "ืื ื ืืืจ ืืืง ืคื ืืื ืชืืืื"
else:
response = generate_persona_response(
message,
state.selected_part,
state.persona_name,
state.user_context,
state.conversation_history
)
state = conv_manager.add_to_history(state, message, response)
history.append([message, response])
return "", history, state
# Create simplified interface
with gr.Blocks(title="ืืจืืืช - ืืจืื ืืืฉื ืืฉืื ืคื ืืื", theme=gr.themes.Soft()) as demo:
# Session state
conversation_state = gr.State(create_session())
gr.HTML("""
<div style="text-align: center; margin-bottom: 30px;">
<h1>๐ช ืืจืืืช: ืืจืื ืืืฉื ืืฉืื ืคื ืืื</h1>
<p>ืืงืื ืืืื ืืฉืืื ืขื ืืืืงืื ืืฉืื ืื ืฉื ืขืฆืื</p>
<div style="background-color: #e8f5e8; border: 1px solid #4caf50; padding: 10px; margin: 10px 0; border-radius: 5px;">
<strong>๐ค ืืขืจืืช ืชืืืืืช ืืืชืืืช ืืืฉืืช ืคืขืืื</strong>
</div>
</div>
""")
with gr.Row():
with gr.Column():
user_context = gr.Textbox(
label="ืกืคืจ ืขื ืขืฆืื ืื ืขื ืืืฆื ืฉืื:",
placeholder="ืืืฉื: ืื ื ืืชืืืื ืขื ืืืฆืื ืืขืืืื...",
lines=3
)
part_choice = gr.Dropdown(
label="ืืืจ ืืืง ืคื ืืื ืืฉืืื:",
choices=[
"ืืงืื ืืืืงืืจืชื",
"ืืืื/ื ืืคื ืืืืช",
"ืืืจืฆื",
"ืืืื",
"ืื ืื ืข/ืช"
],
value="ืืงืื ืืืืงืืจืชื"
)
persona_name = gr.Textbox(
label="ืฉื ืืืฉื ืืืืง (ืืืคืฆืืื ืื):",
placeholder="ืืืฉื: ืื ื, ืขืื, ื ืืขื..."
)
setup_btn = gr.Button("ืืชืื ืฉืืื", variant="primary")
with gr.Column():
current_part = gr.Markdown("ืืืจ ืืืืจืืช ืืืืฅ ืขื 'ืืชืื ืฉืืื'")
# Chat interface
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(height=400, label="ืืฉืืื ืฉืื", rtl=True)
with gr.Row():
msg_input = gr.Textbox(
label="ืืืืืขื ืฉืื:",
placeholder="ืืชืื ืืช ืืืืฉืืืช ืฉืื...",
lines=2,
scale=4
)
send_btn = gr.Button("ืฉืื", scale=1)
clear_btn = gr.Button("ื ืงื ืฉืืื")
# Event handlers
setup_btn.click(
fn=set_context_and_part,
inputs=[user_context, part_choice, persona_name, conversation_state],
outputs=[conversation_state, current_part]
)
msg_input.submit(
fn=chat_with_part,
inputs=[msg_input, chatbot, conversation_state],
outputs=[msg_input, chatbot, conversation_state]
)
send_btn.click(
fn=chat_with_part,
inputs=[msg_input, chatbot, conversation_state],
outputs=[msg_input, chatbot, conversation_state]
)
clear_btn.click(
fn=lambda state: ([], conv_manager.clear_conversation(state)),
inputs=[conversation_state],
outputs=[chatbot, conversation_state]
)
if __name__ == "__main__":
print("๐งช Starting simplified ืืจืืืช app...")
# Find available port
import socket
for port in range(7864, 7874):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('127.0.0.1', port))
available_port = port
break
except OSError:
continue
else:
available_port = 7864
print(f"๐ Starting on port {available_port}")
demo.launch(
server_name="127.0.0.1",
server_port=available_port,
share=True,
show_api=False,
debug=False,
inbrowser=True
) |