Shim commited on
Commit ยท
7aff121
1
Parent(s): a67f1bb
Fix HF Spaces hanging issue - optimize for lightweight deployment
Browse files41 unmodified lines4243444546474849505137 unmodified lines8990919293949541 unmodified lines13713813914014114214314414514629 unmodified lines176177178179180181182183184185186187188228 unmodified lines41741841942042142242342442542642742842943043143243343443543643743843944044144244341 unmodified linesis_hf_spaces = os.getenv("SPACE_ID") is not Noneif is_hf_spaces:logger.info("Running in Hugging Face Spaces - using optimized settings")model_name = "yam-peleg/Hebrew-Mistral-7B"logger.info(f"Loading model: {model_name}")# Load tokenizerself.tokenizer = AutoTokenizer.from_pretrained(model_name)37 unmodified linesdef setup_fallback_model(self):"""Setup a fallback model for testing"""logger.warning("Using fallback model for testing")self.generator = Nonedef generate_response(self, user_message: str, conversation_state: ConversationState) -> str:41 unmodified linesresponse = response[len(full_prompt):].strip()else:# Fallback response for testingpart_info = DEFAULT_PARTS.get(conversation_state.selected_part, {})persona_name = conversation_state.persona_name or part_info.get("default_persona_name", "ืืืง ืคื ืืื")response = f"ืื ื {persona_name}, {conversation_state.selected_part} ืฉืื. ืื ื ืฉืืืข ืื ืฉืืืจืช: '{user_message}'. ืืื ืชืฉืืื ืืืืืื ืืฆืืจื ืืืืงื."return response29 unmodified linesconversation_state = gr.State(self.conversation_manager.create_new_session())# Headergr.HTML("""<div class="hebrew-text welcome-text" style="text-align: center;">๐ช ืืจืืืช: ืืจืื ืืืฉื ืืฉืื ืคื ืืื ืืืคืชื ืขื ืขืฆืื ๐ช</div><div class="hebrew-text" style="text-align: center; margin-bottom: 30px;">ืืงืื ืืืื ืืฉืืื ืขื ืืืืงืื ืืฉืื ืื ืฉื ืขืฆืื ืืืคืชื ืืื ื ืขืฆืืืช ืขืืืงื ืืืชืจ</div>""")# Main interface areas228 unmodified linesdef main():"""Main function to launch the application"""app = MirautrApp()demo = app.create_main_interface()# Launch configuration optimized for HF Spacesis_hf_spaces = os.getenv("SPACE_ID") is not Noneif is_hf_spaces:# HF Spaces configurationdemo.queue(max_size=10) # Enable queuing for better performancedemo.launch(show_error=True,enable_queue=True)else:# Local development configurationdemo.launch(server_name="0.0.0.0",server_port=7860,share=False,show_error=True)if __name__ == "__main__":main()41 unmodified lines424344454647484950515253545556575859606137 unmodified lines9910010110210310410541 unmodified lines14714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219329 unmodified lines223224225226227228229230231232233234235236237238239240241242243244245228 unmodified lines47447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051141 unmodified linesis_hf_spaces = os.getenv("SPACE_ID") is not Noneif is_hf_spaces:logger.info("Running in Hugging Face Spaces - using lightweight model")# Use a smaller, more suitable model for HF Spacesmodel_name = "microsoft/DialoGPT-medium" # Fallback to English model that workslogger.info("Using English model due to HF Spaces limitations")else:model_name = "yam-peleg/Hebrew-Mistral-7B"logger.info(f"Loading Hebrew model: {model_name}")# For HF Spaces, skip heavy model loading and use API-based approach or lighter modelif is_hf_spaces:logger.info("Skipping heavy model loading for HF Spaces - using demo mode")self.setup_fallback_model()return# Load tokenizerself.tokenizer = AutoTokenizer.from_pretrained(model_name)37 unmodified linesdef setup_fallback_model(self):"""Setup a fallback model for testing"""logger.warning("Using fallback demo mode - responses will be simulated")self.generator = Nonedef generate_response(self, user_message: str, conversation_state: ConversationState) -> str:41 unmodified linesresponse = response[len(full_prompt):].strip()else:# Fallback response for demo modepart_info = DEFAULT_PARTS.get(conversation_state.selected_part, {})persona_name = conversation_state.persona_name or part_info.get("default_persona_name", "ืืืง ืคื ืืื")# Generate contextual response based on the part and user messageresponses_by_part = {"ืืงืื ืืืืงืืจืชื": [f"ืื ื {persona_name}, ืืงืื ืืืืงืืจืชื ืฉืื. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}'. ืื ื ืืืฉื ืฉืืืื ืืืืื ืืช ืื ืืืชืจ ืืขืืืง - ืื ืืืืช ืื ืืข ืืืชื ืืื?",f"ื{persona_name}, ืื ื ืืขืืจ ืฉืื ืื - '{user_message}' - ืืื ืื ืืืืช ืื ืฉืืชื ืฆืจืื ืขืืฉืื? ืืืื ื ื ืืฉืื ืขื ืื ืืื.",f"ืื ื {persona_name} ืืื ื ืืื ืืื ืืขืืืจ ืื ืืืฉืื ืืืงืืจืชืืืช. ืื ืฉืืืจืช ืขื '{user_message}' ืืขืืจืจ ืื ืฉืืืืช - ืืื ืืื ืช ืืช ืื ืืืืืืืช?"],"ืืืื/ื ืืคื ืืืืช": [f"ืื ื {persona_name}, ืืืื/ื ืืคื ืืืืช ืฉืื. ืื ืฉืืืจืช - '{user_message}' - ืืืจื ืื ืืืจืืืฉ... ืื ืงืฆืช ืืคืืื ืืื ืื ืืขื ืืื. ืืชื ืืืืช ืฉืืืข ืืืชื?",f"ืืื, ืื ื {persona_name}! ืื ืฉืืืจืช ืขืืฉืื ืขื '{user_message}' ื ืืืข ืื ืืื. ืืคืขืืื ืื ื ืืจืืืฉ/ื ืื ืื ืงืื/ื ืืคื ืื, ืืื ืืชื ื ืืชื ืื ืืงืื ืืื.",f"ืื ื {persona_name}, ืืืืง ืืฆืขืืจ ืืืจืืืฉ ืฉืื. '{user_message}' - ืื ืืืืืจ ืื ืืื ืื ืืจืืืฉ ืคืขื ืืืืืช ืงืื/ื. ืืชื ืืืืจ ืืื ืื ืืื?"],"ืืืจืฆื": [f"ืื ื {persona_name}, ืืืจืฆื ืฉืื. ืื ืฉืืืจืช - '{user_message}' - ืื ื ืจืืฆื ืฉืืืื ืืืื ื ืืืจืืืฉื ืืกืืจ ืขื ืื. ืืื ืื ื ืืืื ืืขืืืจ ืื ืืืืืช ืืจืืฆื ืืืืฆื?",f"ื{persona_name}, ืื ื ืชืืื ืื ืกื ืืืืื ืฉืืืื ืืจืืฆืื. ืื ืฉืืืจืช ืขื '{user_message}' - ืืื ืื ืืฉืคืืข ืขื ืืืืจืื? ืื ื ืืืื ืฉืืืื ืืืื ืืกืืจ.",f"ืื ื {persona_name} ืืื ื ืืื ืืื ืืขืืืจ ืื ืืืฆืื ืืจื ืฉืืืื ืืืื ืืจืืฆืื. '{user_message}' - ืืืื ื ื ืืฆื ืคืชืจืื ืฉืืชืืื ืืืืื."],"ืืืื": [f"ืื ื {persona_name}, ืืืื ืฉืื. ืื ืฉืืืจืช - '{user_message}' - ืื ื ืืื ืืื ืืฉืืืจ ืขืืื. ืืื ืื ืืืื? ืืื ืื ื ืฆืจืื ืืืืื ืืืฉืื?",f"ื{persona_name}, ืชืคืงืืื ืืืื ืขืืื. ืื ืฉืืืจืช ืขื '{user_message}' ืืขืืจืจ ืื ืืช ืืืื ืกืืื ืงื ืืืืื ื. ืืื ืื ื ืืืื ืืืืื ืฉืืชื ืืืื?",f"ืื ื {persona_name}, ืืฉืืืจ ืื ืืื ืฉืื. '{user_message}' - ืื ื ืืืื ืื ืื ืืืื ืขืืืจื. ืืคืขืืื ืื ื ืฆืจืื ืืืืืช ืงืฉืื ืืื ืืฉืืืจ ืขืืื."],"ืื ืื ืข/ืช": [f"ืื ื {persona_name}, ืืืืง ืฉืืขืืืฃ ืืืืื ืข. ืื ืฉืืืจืช ืขื '{user_message}' - ืื ืืขืืจืจ ืื ืืจืื. ืืืื ืืืื ืคืฉืื... ืื ืืืชืขืกืง ืขื ืื ืขืืฉืื?",f"ื{persona_name}, ืื ื ืืจืืืฉ/ื ืงืฆืช ืื ืื ืื ืขื '{user_message}'. ืืืื ื ืืื ืืช ืื ืงืฆืช? ืืคืขืืื ืื ืืกืืจ ืื ืืืชืืืื ืขื ืืื ืืื.",f"ืื ื {persona_name} ืืื ื ืืขืืืฃ/ื ืืืืฉืืจ ืืฆื. ืื ืฉืืืจืช - '{user_message}' - ืื ื ืจืื ืืกืืื. ืืชื ืืืื ืฉืืชื ืจืืฆื ืืืืื ืก ืืื ืขืืฉืื?"]}# Get relevant responses for the partpart_responses = responses_by_part.get(conversation_state.selected_part, [f"ืื ื {persona_name}, {conversation_state.selected_part} ืฉืื. ืฉืืขืชื ืื ืฉืืืจืช ืขื '{user_message}'. ืืื ืืฆื ืืืืื - ืืืฆื ืืื ืืืื ืืื ืืืื ืฉืคื ืขืืจืืช."])# Select response based on conversation length for varietyresponse_index = len(conversation_state.conversation_history) % len(part_responses)response = part_responses[response_index]return response29 unmodified linesconversation_state = gr.State(self.conversation_manager.create_new_session())# Headeris_hf_spaces = os.getenv("SPACE_ID") is not Nonedemo_notice = """<div style="background-color: #fff3cd; border: 1px solid #ffeaa7; padding: 10px; margin: 10px 0; border-radius: 5px; text-align: center;"><strong>๐งช ืืฆื ืืืืื</strong><br/>ืืื ืืฆื ืืืืื - ืืชืืืืืช ืืืืกืกืืช ืขื ืืืืืืืช ืืืื ืืช ืืจืืฉ.<br/>ืืืจืกื ืืืืื ืชืืืื ืืืื ืืื ื ืืืืืืชืืช ืืชืงืื ืืชืืืืืช ืืืฉืืืช ืืืชืจ.</div>""" if is_hf_spaces else ""gr.HTML(f"""<div class="hebrew-text welcome-text" style="text-align: center;">๐ช ืืจืืืช: ืืจืื ืืืฉื ืืฉืื ืคื ืืื ืืืคืชื ืขื ืขืฆืื ๐ช</div><div class="hebrew-text" style="text-align: center; margin-bottom: 30px;">ืืงืื ืืืื ืืฉืืื ืขื ืืืืงืื ืืฉืื ืื ืฉื ืขืฆืื ืืืคืชื ืืื ื ืขืฆืืืช ืขืืืงื ืืืชืจ</div>{demo_notice}""")# Main interface areas228 unmodified linesdef main():"""Main function to launch the application"""logger.info("Starting ืืจืืืช application...")try:app = MirautrApp()demo = app.create_main_interface()# Launch configuration optimized for HF Spacesis_hf_spaces = os.getenv("SPACE_ID") is not Nonelogger.info(f"Launching app... HF Spaces: {is_hf_spaces}")if is_hf_spaces:# HF Spaces configuration - simplifiedlogger.info("Configuring for HF Spaces deployment")demo.launch(server_name="0.0.0.0",server_port=7860,show_error=True,quiet=False)else:# Local development configurationdemo.launch(server_name="0.0.0.0",server_port=7860,share=False,show_error=True)except Exception as e:logger.error(f"Failed to start application: {e}")raiseif __name__ == "__main__":main()
app.py+95-27