Shim commited on
Commit
7aff121
ยท
1 Parent(s): a67f1bb

Fix HF Spaces hanging issue - optimize for lightweight deployment

Browse files
Files changed (1) hide show
  1. app.py +95 -27
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 optimized settings")
 
 
 
 
 
 
 
46
 
47
- model_name = "yam-peleg/Hebrew-Mistral-7B"
48
- logger.info(f"Loading model: {model_name}")
 
 
 
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 model for testing")
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 testing
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
- response = f"ืื ื™ {persona_name}, {conversation_state.selected_part} ืฉืœืš. ืื ื™ ืฉื•ืžืข ืžื” ืฉืืžืจืช: '{user_message}'. ื–ื”ื• ืชืฉื•ื‘ื” ืœื“ื•ื’ืžื” ืœืฆื•ืจืš ื‘ื“ื™ืงื”."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- gr.HTML("""
 
 
 
 
 
 
 
 
 
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
- app = MirautrApp()
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
- if is_hf_spaces:
427
- # HF Spaces configuration
428
- demo.queue(max_size=10) # Enable queuing for better performance
429
- demo.launch(
430
- show_error=True,
431
- enable_queue=True
432
- )
433
- else:
434
- # Local development configuration
435
- demo.launch(
436
- server_name="0.0.0.0",
437
- server_port=7860,
438
- share=False,
439
- show_error=True
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()