Spaces:
Sleeping
Sleeping
remove fallback model completely, and uncomment
Browse files77 unmodified lines78798081828384858687888990919293949596979861 unmodified lines16016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319411 unmodified lines20620720820921021121221321443 unmodified lines258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342360 unmodified lines70370470570670770870971071171271371471571671771871972016 unmodified lines73773873974074174274374474574674774874975075175275375475575675777 unmodified lines"description": "Lightweight T5 model optimized for instruction following","dtype": torch.float16 if torch.cuda.is_available() else torch.float32,"is_t5": True},"Fallback Model": {"name": "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T","description": "Model sangat ringan untuk fallback","dtype": torch.float16 if torch.cuda.is_available() else torch.float32}}# Tambahkan model fallback ke MODEL_CONFIG# MODEL_CONFIG["Fallback Model"] = {# "name": "TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T",# "description": "Model sangat ringan untuk fallback",# "dtype": torch.float16 if torch.cuda.is_available() else torch.float32# }def initialize_model_once(model_key):with MODEL_CACHE["init_lock"]:current_model = MODEL_CACHE["model_name"]61 unmodified lines# Handle standard HF modelselse:MODEL_CACHE["tokenizer"] = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)# Only use quantization if CUDA is availableif torch.cuda.is_available():quantization_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_compute_dtype=torch.float16,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=True)MODEL_CACHE["model"] = AutoModelForCausalLM.from_pretrained(model_name,quantization_config=quantization_config,torch_dtype=model_info["dtype"],device_map="auto",low_cpu_mem_usage=True,trust_remote_code=True)else:# For CPU-only environments, load without quantizationMODEL_CACHE["model"] = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float32, # Use float32 for CPUdevice_map=None,low_cpu_mem_usage=True,trust_remote_code=True)MODEL_CACHE["is_gguf"] = Falseprint(f"Model {model_name} loaded successfully")11 unmodified linesprint(f"Creating pipeline for model: {model_key}")tokenizer, model, is_gguf = initialize_model_once(model_key)# Get the model info for referencemodel_info = MODEL_CONFIG[model_key]if model is None:raise ValueError(f"Model is None for {model_key}")43 unmodified linesimport tracebackprint(f"Error creating pipeline: {str(e)}")print(traceback.format_exc())# Fallback ke model sederhana jika yang utama gagalif model_key != "Fallback Model":print(f"Trying fallback model")try:return create_fallback_pipeline()except:raise RuntimeError(f"Failed to create pipeline: {str(e)}")else:raise RuntimeError(f"Failed to create pipeline: {str(e)}")def create_fallback_pipeline():"""Create a fallback pipeline with a very small model"""model_key = "Fallback Model"print(f"Creating minimal fallback pipeline with {MODEL_CONFIG[model_key]['name']}")# Avoid using bitsandbytes for quantization when CUDA is not availabletry:tokenizer = AutoTokenizer.from_pretrained(MODEL_CONFIG[model_key]["name"])# Load model in 8-bit or without quantization for CPUif torch.cuda.is_available():model = AutoModelForCausalLM.from_pretrained(MODEL_CONFIG[model_key]["name"],torch_dtype=MODEL_CONFIG[model_key]["dtype"],device_map="auto",low_cpu_mem_usage=True)else:# For CPU-only environments, avoid quantizationmodel = AutoModelForCausalLM.from_pretrained(MODEL_CONFIG[model_key]["name"],torch_dtype=torch.float32, # Use float32 for CPUlow_cpu_mem_usage=True)pipe = pipeline("text-generation",model=model,tokenizer=tokenizer,max_new_tokens=64, # Reduced for CPU performancetemperature=0.3,return_full_text=False,)return HuggingFacePipeline(pipeline=pipe)except Exception as e:print(f"Error creating minimal fallback pipeline: {str(e)}")raisedef handle_model_loading_error(model_key, session_id):"""Handle model loading errors with fallback options"""fallback_hierarchy = ["DeepSeek Coder Instruct", # 1.3B model"Phi-4", # 1.5B model"TinyLlama-Chat", # 1.1B model"Flan-T5-Small" # Paling ringan]# Jika model yang gagal sudah merupakan fallback terakhir, berikan pesan errorif model_key == fallback_hierarchy[-1]:return None, f"Tidak dapat memuat model {model_key}. Harap coba lagi nanti."# Temukan posisi model yang gagal dalam hirarkitry:current_index = fallback_hierarchy.index(model_key)except ValueError:current_index = -1# Coba model berikutnya dalam hirarkifor fallback_model in fallback_hierarchy[current_index+1:]:try:print(f"Trying fallback model: {fallback_model}")chatbot = ChatBot(session_id, fallback_model)return chatbot, f"Model {model_key} tidak tersedia. Menggunakan {fallback_model} sebagai alternatif."except Exception as e:print(f"Fallback model {fallback_model} also failed: {str(e)}")return None, "Semua model gagal dimuat. Harap coba lagi nanti."def create_conversational_chain(db, file_path, model_key):llm = create_llm_pipeline(model_key)360 unmodified linesimport tracebackprint(f"Error processing file with {model_key}: {str(e)}")print(traceback.format_exc())# Coba dengan model fallbacktry:chatbot, message = handle_model_loading_error(model_key, sess_id)if chatbot is not None:result = chatbot.process_file(file)return chatbot, True, [(None, message), (None, result)]else:return None, False, [(None, message)]except Exception as fb_err:error_msg = f"Error dengan model {model_key}: {str(e)}\n\nFallback juga gagal: {str(fb_err)}"return None, False, [(None, error_msg)]process_button.click(fn=handle_process_file,16 unmodified linesoutputs=[chatbot_state, model_selected, chatbot_interface, model_dropdown])# Change model handler# def handle_model_change(model_key, chatbot, sess_id):# if chatbot is None:# chatbot = ChatBot(sess_id, model_key)# return chatbot, [(None, f"Model diatur ke {model_key}. Silakan upload file CSV.")]# result = chatbot.change_model(model_key)# return chatbot, chatbot.chat_history + [(None, result)]# change_model_button.click(# fn=handle_model_change,# inputs=[model_dropdown, chatbot_state, session_id],# outputs=[chatbot_state, chatbot_interface]# )# Chat handlersdef user_message_submitted(message, history, chatbot, sess_id):history = history + [(message, None)]77 unmodified lines78798081828384858661 unmodified lines14814915015115215315415515615715815916016116216316416516616716811 unmodified lines18018118218318418543 unmodified lines229230231232233234235236237238239240241242243244245246247248249250360 unmodified lines61161261361461561616 unmodified lines63363463563663763877 unmodified lines"description": "Lightweight T5 model optimized for instruction following","dtype": torch.float16 if torch.cuda.is_available() else torch.float32,"is_t5": True}}def initialize_model_once(model_key):with MODEL_CACHE["init_lock"]:current_model = MODEL_CACHE["model_name"]61 unmodified lines# Handle standard HF modelselse:quantization_config = BitsAndBytesConfig(load_in_4bit=True,bnb_4bit_compute_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,bnb_4bit_quant_type="nf4",bnb_4bit_use_double_quant=True)MODEL_CACHE["tokenizer"] = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)MODEL_CACHE["model"] = AutoModelForCausalLM.from_pretrained(model_name,quantization_config=quantization_config,torch_dtype=model_info["dtype"],device_map="auto" if torch.cuda.is_available() else None,low_cpu_mem_usage=True,trust_remote_code=True)MODEL_CACHE["is_gguf"] = Falseprint(f"Model {model_name} loaded successfully")11 unmodified linesprint(f"Creating pipeline for model: {model_key}")tokenizer, model, is_gguf = initialize_model_once(model_key)if model is None:raise ValueError(f"Model is None for {model_key}")43 unmodified linesimport tracebackprint(f"Error creating pipeline: {str(e)}")print(traceback.format_exc())def handle_model_loading_error(model_key, session_id):"""Handle model loading errors by providing alternative model suggestions"""suggested_models = ["DeepSeek Coder Instruct", # 1.3B model"Phi-4 Mini Instruct", # Light model"TinyLlama Chat", # 1.1B model"Flan T5 Small" # Lightweight T5]# Remove the current model from suggestions if it's in the listif model_key in suggested_models:suggested_models.remove(model_key)suggestions = ", ".join(suggested_models[:3]) # Only show top 3 suggestionsreturn None, f"Tidak dapat memuat model {model_key}. Silakan coba model lain seperti: {suggestions}"def create_conversational_chain(db, file_path, model_key):llm = create_llm_pipeline(model_key)360 unmodified linesimport tracebackprint(f"Error processing file with {model_key}: {str(e)}")print(traceback.format_exc())process_button.click(fn=handle_process_file,16 unmodified linesoutputs=[chatbot_state, model_selected, chatbot_interface, model_dropdown])# Chat handlersdef user_message_submitted(message, history, chatbot, sess_id):history = history + [(message, None)]
app.py+24-143