|
|
|
|
|
|
|
|
""" |
|
|
Local startup script for ืืจืืืช (Mirrors) application |
|
|
Handles environment setup and provides fallback options |
|
|
""" |
|
|
|
|
|
import os |
|
|
import sys |
|
|
import socket |
|
|
import subprocess |
|
|
import logging |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
def find_available_port(start_port=7861, max_tries=10): |
|
|
"""Find an available port starting from start_port""" |
|
|
for port in range(start_port, start_port + max_tries): |
|
|
try: |
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
|
|
s.bind(('127.0.0.1', port)) |
|
|
return port |
|
|
except OSError: |
|
|
continue |
|
|
return start_port |
|
|
|
|
|
def check_dependencies(): |
|
|
"""Check if required dependencies are installed""" |
|
|
required_packages = ['gradio', 'transformers', 'torch'] |
|
|
missing_packages = [] |
|
|
|
|
|
for package in required_packages: |
|
|
try: |
|
|
__import__(package) |
|
|
logger.info(f"โ
{package} is installed") |
|
|
except ImportError: |
|
|
missing_packages.append(package) |
|
|
logger.error(f"โ {package} is missing") |
|
|
|
|
|
if missing_packages: |
|
|
logger.error("Missing packages. Please install them:") |
|
|
logger.error(f"pip install {' '.join(missing_packages)}") |
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
def run_simple_app(port): |
|
|
"""Run the simplified app version""" |
|
|
logger.info("๐ Running simplified version...") |
|
|
|
|
|
try: |
|
|
|
|
|
import gradio as gr |
|
|
from conversation_manager import ConversationManager |
|
|
from prompt_engineering import DEFAULT_PARTS |
|
|
import random |
|
|
|
|
|
|
|
|
conv_manager = ConversationManager() |
|
|
|
|
|
def generate_persona_response(user_message: str, part_name: str, persona_name: str, user_context: str = None): |
|
|
"""Generate persona-based response using templates""" |
|
|
part_info = DEFAULT_PARTS.get(part_name, {}) |
|
|
display_name = persona_name or part_info.get("default_persona_name", "ืืืง ืคื ืืื") |
|
|
|
|
|
|
|
|
if part_name == "ืืงืื ืืืืงืืจืชื": |
|
|
responses = [ |
|
|
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}, ืืืืง ืืฆืขืืจ ืฉืื. ืื ืฉืืืจืช ื ืืืข ืืื ืฉืื." |
|
|
] |
|
|
elif part_name == "ืืืจืฆื": |
|
|
responses = [ |
|
|
f"ืื ื {display_name}, ืืืจืฆื ืฉืื. ืฉืืขืชื ืืช '{user_message}' ืืื ื ืจืืฆื ืืืืื ืฉืืืื ืืืื ืืกืืจ ืขื ืื.", |
|
|
f"ืื {display_name}. ืื ืฉืืืจืช ืขื '{user_message}' ืืืจื ืื ืืืืื - ืืื ืื ืืืื ืืคืืืข ืืืืฉืื?", |
|
|
f"ืื ื {display_name}, ืืื ื ืจืืฆื ืฉืืืื ืืืื ืืจืืฆืื ืืื." |
|
|
] |
|
|
elif part_name == "ืืืื": |
|
|
responses = [ |
|
|
f"ืื ื {display_name}, ืืืื ืฉืื. '{user_message}' - ืื ื ืืขืจืื ืืช ืืืฆื. ืืื ืื ืืืื?", |
|
|
f"ืื {display_name}. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}' ืืื ื ืืื ืืืื ื ืืช.", |
|
|
f"ืื ื {display_name}, ืืฉืืืจ ืฉืื. ืื ืฉืืืจืช ืืขืืจืจ ืื ืืช ืืืื ืกืืื ืงืืื ืืืื ืืื." |
|
|
] |
|
|
elif part_name == "ืื ืื ืข/ืช": |
|
|
responses = [ |
|
|
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}' - ืืืื ื ื ืืงืืจ ืืช ืื ืืื." |
|
|
] |
|
|
|
|
|
selected_response = random.choice(responses) |
|
|
|
|
|
if user_context: |
|
|
selected_response += f" ืืืืจ ืฉืืืจืช ืืืชืืื: {user_context[:100]}..." |
|
|
|
|
|
return selected_response |
|
|
|
|
|
def create_session(): |
|
|
return conv_manager.create_new_session() |
|
|
|
|
|
def set_context_and_part(user_context, part_choice, persona_name, state): |
|
|
state = conv_manager.set_initial_context(state, "general", user_context) |
|
|
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): |
|
|
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 = conv_manager.add_to_history(state, message, response) |
|
|
|
|
|
history.append([message, response]) |
|
|
return "", history, state |
|
|
|
|
|
|
|
|
with gr.Blocks(title="ืืจืืืช - ืืจืื ืืืฉื ืืฉืื ืคื ืืื", theme=gr.themes.Soft()) as demo: |
|
|
|
|
|
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("ืืืจ ืืืืจืืช ืืืืฅ ืขื 'ืืชืื ืฉืืื'") |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=2): |
|
|
chatbot = gr.Chatbot(height=400, label="ืืฉืืื ืฉืื") |
|
|
|
|
|
with gr.Row(): |
|
|
msg_input = gr.Textbox( |
|
|
label="ืืืืืขื ืฉืื:", |
|
|
placeholder="ืืชืื ืืช ืืืืฉืืืช ืฉืื...", |
|
|
lines=2, |
|
|
scale=4 |
|
|
) |
|
|
send_btn = gr.Button("ืฉืื", scale=1) |
|
|
|
|
|
clear_btn = gr.Button("ื ืงื ืฉืืื") |
|
|
|
|
|
|
|
|
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] |
|
|
) |
|
|
|
|
|
|
|
|
logger.info("๐ Launching simplified ืืจืืืช app...") |
|
|
demo.launch( |
|
|
server_name="127.0.0.1", |
|
|
server_port=port, |
|
|
share=True, |
|
|
show_api=False, |
|
|
show_error=True, |
|
|
inbrowser=True, |
|
|
quiet=False |
|
|
) |
|
|
return True |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"โ Simplified app failed: {e}") |
|
|
return False |
|
|
|
|
|
def run_app(): |
|
|
"""Run the ืืจืืืช application""" |
|
|
|
|
|
logger.info("๐ช Starting ืืจืืืช application...") |
|
|
|
|
|
|
|
|
if not check_dependencies(): |
|
|
logger.error("Dependencies check failed. Exiting.") |
|
|
return False |
|
|
|
|
|
|
|
|
port = find_available_port() |
|
|
logger.info(f"๐ Using port {port}") |
|
|
|
|
|
|
|
|
os.environ["GRADIO_SERVER_PORT"] = str(port) |
|
|
|
|
|
|
|
|
logger.info("๐ฏ Starting with simplified version for maximum reliability...") |
|
|
success = run_simple_app(port) |
|
|
|
|
|
if success: |
|
|
return True |
|
|
|
|
|
|
|
|
logger.info("๐ Trying subprocess approach...") |
|
|
try: |
|
|
cmd = [sys.executable, "simple_app.py"] |
|
|
subprocess.run(cmd, check=True) |
|
|
return True |
|
|
except Exception as e: |
|
|
logger.error(f"โ Subprocess approach failed: {e}") |
|
|
return False |
|
|
|
|
|
if __name__ == "__main__": |
|
|
print("๐ช ืืจืืืช - Hebrew Self-Reflective AI Agent") |
|
|
print("=" * 50) |
|
|
|
|
|
success = run_app() |
|
|
|
|
|
if not success: |
|
|
print("\nโ Failed to start application") |
|
|
print("๐ Troubleshooting:") |
|
|
print("1. Make sure you're in a virtual environment") |
|
|
print("2. Install dependencies: pip install -r requirements.txt") |
|
|
print("3. Try running directly: python simple_app.py") |
|
|
print("4. Check Gradio version: pip install gradio==4.44.0") |
|
|
sys.exit(1) |
|
|
else: |
|
|
print("\nโ
Application started successfully!") |