File size: 12,862 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Local startup script for ืืจืืืช (Mirrors) application
Handles environment setup and provides fallback options
"""
import os
import sys
import socket
import subprocess
import logging
# Configure 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 and run simple app directly
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):
"""Generate persona-based response using templates"""
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}', ืืื ืื ื ืืจืืืฉ ืฉืื ืื ื ืฆืจืืืื ืืืืืช ืืืชืจ ืืืงืืจืชืืื ืืื."
]
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
# Create simplified interface without API docs
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("ืืืจ ืืืืจืืช ืืืืฅ ืขื 'ืืชืื ืฉืืื'")
# Chat interface
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("ื ืงื ืฉืืื")
# 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]
)
# Launch with minimal configuration to avoid schema issues
logger.info("๐ Launching simplified ืืจืืืช app...")
demo.launch(
server_name="127.0.0.1",
server_port=port,
share=True,
show_api=False, # Disable API to prevent schema errors
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...")
# Check dependencies
if not check_dependencies():
logger.error("Dependencies check failed. Exiting.")
return False
# Find available port
port = find_available_port()
logger.info(f"๐ Using port {port}")
# Set environment variables for local development
os.environ["GRADIO_SERVER_PORT"] = str(port)
# Try simplified app first (more reliable)
logger.info("๐ฏ Starting with simplified version for maximum reliability...")
success = run_simple_app(port)
if success:
return True
# If simplified app failed, try subprocess approach
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!") |