Shim
commited on
Commit
ยท
7aff121
1
Parent(s):
a67f1bb
Fix HF Spaces hanging issue - optimize for lightweight deployment
Browse files
app.py
CHANGED
|
@@ -42,10 +42,20 @@ class MirautrApp:
|
|
| 42 |
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 43 |
|
| 44 |
if is_hf_spaces:
|
| 45 |
-
logger.info("Running in Hugging Face Spaces - using
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Load tokenizer
|
| 51 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
@@ -89,7 +99,7 @@ class MirautrApp:
|
|
| 89 |
|
| 90 |
def setup_fallback_model(self):
|
| 91 |
"""Setup a fallback model for testing"""
|
| 92 |
-
logger.warning("Using fallback
|
| 93 |
self.generator = None
|
| 94 |
|
| 95 |
def generate_response(self, user_message: str, conversation_state: ConversationState) -> str:
|
|
@@ -137,10 +147,47 @@ class MirautrApp:
|
|
| 137 |
response = response[len(full_prompt):].strip()
|
| 138 |
|
| 139 |
else:
|
| 140 |
-
# Fallback response for
|
| 141 |
part_info = DEFAULT_PARTS.get(conversation_state.selected_part, {})
|
| 142 |
persona_name = conversation_state.persona_name or part_info.get("default_persona_name", "ืืืง ืคื ืืื")
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
return response
|
| 146 |
|
|
@@ -176,13 +223,23 @@ class MirautrApp:
|
|
| 176 |
conversation_state = gr.State(self.conversation_manager.create_new_session())
|
| 177 |
|
| 178 |
# Header
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
<div class="hebrew-text welcome-text" style="text-align: center;">
|
| 181 |
๐ช ืืจืืืช: ืืจืื ืืืฉื ืืฉืื ืคื ืืื ืืืคืชื ืขื ืขืฆืื ๐ช
|
| 182 |
</div>
|
| 183 |
<div class="hebrew-text" style="text-align: center; margin-bottom: 30px;">
|
| 184 |
ืืงืื ืืืื ืืฉืืื ืขื ืืืืงืื ืืฉืื ืื ืฉื ืขืฆืื ืืืคืชื ืืื ื ืขืฆืืืช ืขืืืงื ืืืชืจ
|
| 185 |
</div>
|
|
|
|
| 186 |
""")
|
| 187 |
|
| 188 |
# Main interface areas
|
|
@@ -417,27 +474,38 @@ class MirautrApp:
|
|
| 417 |
|
| 418 |
def main():
|
| 419 |
"""Main function to launch the application"""
|
| 420 |
-
|
| 421 |
-
demo = app.create_main_interface()
|
| 422 |
-
|
| 423 |
-
# Launch configuration optimized for HF Spaces
|
| 424 |
-
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 425 |
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
demo.
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
if __name__ == "__main__":
|
| 443 |
main()
|
|
|
|
| 42 |
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 43 |
|
| 44 |
if is_hf_spaces:
|
| 45 |
+
logger.info("Running in Hugging Face Spaces - using lightweight model")
|
| 46 |
+
# Use a smaller, more suitable model for HF Spaces
|
| 47 |
+
model_name = "microsoft/DialoGPT-medium" # Fallback to English model that works
|
| 48 |
+
logger.info("Using English model due to HF Spaces limitations")
|
| 49 |
+
|
| 50 |
+
else:
|
| 51 |
+
model_name = "yam-peleg/Hebrew-Mistral-7B"
|
| 52 |
+
logger.info(f"Loading Hebrew model: {model_name}")
|
| 53 |
|
| 54 |
+
# For HF Spaces, skip heavy model loading and use API-based approach or lighter model
|
| 55 |
+
if is_hf_spaces:
|
| 56 |
+
logger.info("Skipping heavy model loading for HF Spaces - using demo mode")
|
| 57 |
+
self.setup_fallback_model()
|
| 58 |
+
return
|
| 59 |
|
| 60 |
# Load tokenizer
|
| 61 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 99 |
|
| 100 |
def setup_fallback_model(self):
|
| 101 |
"""Setup a fallback model for testing"""
|
| 102 |
+
logger.warning("Using fallback demo mode - responses will be simulated")
|
| 103 |
self.generator = None
|
| 104 |
|
| 105 |
def generate_response(self, user_message: str, conversation_state: ConversationState) -> str:
|
|
|
|
| 147 |
response = response[len(full_prompt):].strip()
|
| 148 |
|
| 149 |
else:
|
| 150 |
+
# Fallback response for demo mode
|
| 151 |
part_info = DEFAULT_PARTS.get(conversation_state.selected_part, {})
|
| 152 |
persona_name = conversation_state.persona_name or part_info.get("default_persona_name", "ืืืง ืคื ืืื")
|
| 153 |
+
|
| 154 |
+
# Generate contextual response based on the part and user message
|
| 155 |
+
responses_by_part = {
|
| 156 |
+
"ืืงืื ืืืืงืืจืชื": [
|
| 157 |
+
f"ืื ื {persona_name}, ืืงืื ืืืืงืืจืชื ืฉืื. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}'. ืื ื ืืืฉื ืฉืืืื ืืืืื ืืช ืื ืืืชืจ ืืขืืืง - ืื ืืืืช ืื ืืข ืืืชื ืืื?",
|
| 158 |
+
f"ื{persona_name}, ืื ื ืืขืืจ ืฉืื ืื - '{user_message}' - ืืื ืื ืืืืช ืื ืฉืืชื ืฆืจืื ืขืืฉืื? ืืืื ื ื ืืฉืื ืขื ืื ืืื.",
|
| 159 |
+
f"ืื ื {persona_name} ืืื ื ืืื ืืื ืืขืืืจ ืื ืืืฉืื ืืืงืืจืชืืืช. ืื ืฉืืืจืช ืขื '{user_message}' ืืขืืจืจ ืื ืฉืืืืช - ืืื ืืื ืช ืืช ืื ืืืืืืืช?"
|
| 160 |
+
],
|
| 161 |
+
"ืืืื/ื ืืคื ืืืืช": [
|
| 162 |
+
f"ืื ื {persona_name}, ืืืื/ื ืืคื ืืืืช ืฉืื. ืื ืฉืืืจืช - '{user_message}' - ืืืจื ืื ืืืจืืืฉ... ืื ืงืฆืช ืืคืืื ืืื ืื ืืขื ืืื. ืืชื ืืืืช ืฉืืืข ืืืชื?",
|
| 163 |
+
f"ืืื, ืื ื {persona_name}! ืื ืฉืืืจืช ืขืืฉืื ืขื '{user_message}' ื ืืืข ืื ืืื. ืืคืขืืื ืื ื ืืจืืืฉ/ื ืื ืื ืงืื/ื ืืคื ืื, ืืื ืืชื ื ืืชื ืื ืืงืื ืืื.",
|
| 164 |
+
f"ืื ื {persona_name}, ืืืืง ืืฆืขืืจ ืืืจืืืฉ ืฉืื. '{user_message}' - ืื ืืืืืจ ืื ืืื ืื ืืจืืืฉ ืคืขื ืืืืืช ืงืื/ื. ืืชื ืืืืจ ืืื ืื ืืื?"
|
| 165 |
+
],
|
| 166 |
+
"ืืืจืฆื": [
|
| 167 |
+
f"ืื ื {persona_name}, ืืืจืฆื ืฉืื. ืื ืฉืืืจืช - '{user_message}' - ืื ื ืจืืฆื ืฉืืืื ืืืื ื ืืืจืืืฉื ืืกืืจ ืขื ืื. ืืื ืื ื ืืืื ืืขืืืจ ืื ืืืืืช ืืจืืฆื ืืืืฆื?",
|
| 168 |
+
f"ื{persona_name}, ืื ื ืชืืื ืื ืกื ืืืืื ืฉืืืื ืืจืืฆืื. ืื ืฉืืืจืช ืขื '{user_message}' - ืืื ืื ืืฉืคืืข ืขื ืืืืจืื? ืื ื ืืืื ืฉืืืื ืืืื ืืกืืจ.",
|
| 169 |
+
f"ืื ื {persona_name} ืืื ื ืืื ืืื ืืขืืืจ ืื ืืืฆืื ืืจื ืฉืืืื ืืืื ืืจืืฆืื. '{user_message}' - ืืืื ื ื ืืฆื ืคืชืจืื ืฉืืชืืื ืืืืื."
|
| 170 |
+
],
|
| 171 |
+
"ืืืื": [
|
| 172 |
+
f"ืื ื {persona_name}, ืืืื ืฉืื. ืื ืฉืืืจืช - '{user_message}' - ืื ื ืืื ืืื ืืฉืืืจ ืขืืื. ืืื ืื ืืืื? ืืื ืื ื ืฆืจืื ืืืืื ืืืฉืื?",
|
| 173 |
+
f"ื{persona_name}, ืชืคืงืืื ืืืื ืขืืื. ืื ืฉืืืจืช ืขื '{user_message}' ืืขืืจืจ ืื ืืช ืืืื ืกืืื ืงื ืืืืื ื. ืืื ืื ื ืืืื ืืืืื ืฉืืชื ืืืื?",
|
| 174 |
+
f"ืื ื {persona_name}, ืืฉืืืจ ืื ืืื ืฉืื. '{user_message}' - ืื ื ืืืื ืื ืื ืืืื ืขืืืจื. ืืคืขืืื ืื ื ืฆืจืื ืืืืืช ืงืฉืื ืืื ืืฉืืืจ ืขืืื."
|
| 175 |
+
],
|
| 176 |
+
"ืื ืื ืข/ืช": [
|
| 177 |
+
f"ืื ื {persona_name}, ืืืืง ืฉืืขืืืฃ ืืืืื ืข. ืื ืฉืืืจืช ืขื '{user_message}' - ืื ืืขืืจืจ ืื ืืจืื. ืืืื ืืืื ืคืฉืื... ืื ืืืชืขืกืง ืขื ืื ืขืืฉืื?",
|
| 178 |
+
f"ื{persona_name}, ืื ื ืืจืืืฉ/ื ืงืฆืช ืื ืื ืื ืขื '{user_message}'. ืืืื ื ืืื ืืช ืื ืงืฆืช? ืืคืขืืื ืื ืืกืืจ ืื ืืืชืืืื ืขื ืืื ืืื.",
|
| 179 |
+
f"ืื ื {persona_name} ืืื ื ืืขืืืฃ/ื ืืืืฉืืจ ืืฆื. ืื ืฉืืืจืช - '{user_message}' - ืื ื ืจืื ืืกืืื. ืืชื ืืืื ืฉืืชื ืจืืฆื ืืืืื ืก ืืื ืขืืฉืื?"
|
| 180 |
+
]
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
# Get relevant responses for the part
|
| 184 |
+
part_responses = responses_by_part.get(conversation_state.selected_part, [
|
| 185 |
+
f"ืื ื {persona_name}, {conversation_state.selected_part} ืฉืื. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}'. ืืื ืืฆื ืืืืื - ืืืฆื ืืื ืืืื ืืื ืืืื ืฉืคื ืขืืจืืช."
|
| 186 |
+
])
|
| 187 |
+
|
| 188 |
+
# Select response based on conversation length for variety
|
| 189 |
+
response_index = len(conversation_state.conversation_history) % len(part_responses)
|
| 190 |
+
response = part_responses[response_index]
|
| 191 |
|
| 192 |
return response
|
| 193 |
|
|
|
|
| 223 |
conversation_state = gr.State(self.conversation_manager.create_new_session())
|
| 224 |
|
| 225 |
# Header
|
| 226 |
+
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 227 |
+
demo_notice = """
|
| 228 |
+
<div style="background-color: #fff3cd; border: 1px solid #ffeaa7; padding: 10px; margin: 10px 0; border-radius: 5px; text-align: center;">
|
| 229 |
+
<strong>๐งช ืืฆื ืืืืื</strong><br/>
|
| 230 |
+
ืืื ืืฆื ืืืืื - ืืชืืืืืช ืืืืกืกืืช ืขื ืืืืืืืช ืืืื ืืช ืืจืืฉ.<br/>
|
| 231 |
+
ืืืจืกื ืืืืื ืชืืืื ืืืื ืืื ื ืืืืืืชืืช ืืชืงืื ืืชืืืืืช ืืืฉืืืช ืืืชืจ.
|
| 232 |
+
</div>
|
| 233 |
+
""" if is_hf_spaces else ""
|
| 234 |
+
|
| 235 |
+
gr.HTML(f"""
|
| 236 |
<div class="hebrew-text welcome-text" style="text-align: center;">
|
| 237 |
๐ช ืืจืืืช: ืืจืื ืืืฉื ืืฉืื ืคื ืืื ืืืคืชื ืขื ืขืฆืื ๐ช
|
| 238 |
</div>
|
| 239 |
<div class="hebrew-text" style="text-align: center; margin-bottom: 30px;">
|
| 240 |
ืืงืื ืืืื ืืฉืืื ืขื ืืืืงืื ืืฉืื ืื ืฉื ืขืฆืื ืืืคืชื ืืื ื ืขืฆืืืช ืขืืืงื ืืืชืจ
|
| 241 |
</div>
|
| 242 |
+
{demo_notice}
|
| 243 |
""")
|
| 244 |
|
| 245 |
# Main interface areas
|
|
|
|
| 474 |
|
| 475 |
def main():
|
| 476 |
"""Main function to launch the application"""
|
| 477 |
+
logger.info("Starting ืืจืืืช application...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
+
try:
|
| 480 |
+
app = MirautrApp()
|
| 481 |
+
demo = app.create_main_interface()
|
| 482 |
+
|
| 483 |
+
# Launch configuration optimized for HF Spaces
|
| 484 |
+
is_hf_spaces = os.getenv("SPACE_ID") is not None
|
| 485 |
+
|
| 486 |
+
logger.info(f"Launching app... HF Spaces: {is_hf_spaces}")
|
| 487 |
+
|
| 488 |
+
if is_hf_spaces:
|
| 489 |
+
# HF Spaces configuration - simplified
|
| 490 |
+
logger.info("Configuring for HF Spaces deployment")
|
| 491 |
+
demo.launch(
|
| 492 |
+
server_name="0.0.0.0",
|
| 493 |
+
server_port=7860,
|
| 494 |
+
show_error=True,
|
| 495 |
+
quiet=False
|
| 496 |
+
)
|
| 497 |
+
else:
|
| 498 |
+
# Local development configuration
|
| 499 |
+
demo.launch(
|
| 500 |
+
server_name="0.0.0.0",
|
| 501 |
+
server_port=7860,
|
| 502 |
+
share=False,
|
| 503 |
+
show_error=True
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
except Exception as e:
|
| 507 |
+
logger.error(f"Failed to start application: {e}")
|
| 508 |
+
raise
|
| 509 |
|
| 510 |
if __name__ == "__main__":
|
| 511 |
main()
|