Download simple_app.py from shim5/mirrors: direct link, hf CLI and curl.
- Browser
- Download file 12.1 kB
-
https://huggingface.co/spaces/shim5/mirrors/resolve/main/simple_app.py
- Command line
-
hf download hf://spaces/shim5/mirrors/simple_app.py
-
curl -L -o simple_app.py https://huggingface.co/spaces/shim5/mirrors/resolve/main/simple_app.py
12.1 kB
| # -*- 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 | |
| ) |