Spaces:
Sleeping
Sleeping
Add HuggingFace authentication and better error handling for fine-tuned model
Browse files44 unmodified lines45464748495051525354553 unmodified lines59606162636465666768697049 unmodified lines12012112212312412512612712812913013113213313444 unmodified linesdef load_fine_tuned_model():"""Load the fine-tuned model from Hugging Face Hub"""try:# Replace with your actual repository namemodel_name = "kundan621/tinyllama-makemytrip-financial-qa"# Load tokenizertokenizer = AutoTokenizer.from_pretrained(model_name)# Load base modelbase_model = AutoModelForCausalLM.from_pretrained(3 unmodified linestrust_remote_code=True,)# Load the fine-tuned PEFT modelmodel = PeftModel.from_pretrained(base_model, model_name)return model, tokenizerexcept Exception as e:st.error(f"Error loading fine-tuned model: {e}")return None, Nonedef generate_fine_tuned_response(model, tokenizer, question):49 unmodified linesst.set_page_config(page_title="Finance QA Assistant", layout="centered")st.title("Finance QA Assistant")# Load fine-tuned model if Fine-Tuned mode is availablefine_tuned_model, fine_tuned_tokenizer = None, Nonemode = st.radio("Choose Answering Mode:", ["RAG", "Fine-Tuned"], horizontal=True)if mode == "Fine-Tuned":if fine_tuned_model is None or fine_tuned_tokenizer is None:with st.spinner("Loading fine-tuned model..."):fine_tuned_model, fine_tuned_tokenizer = load_fine_tuned_model()query = st.text_input("Enter your question:")44 unmodified lines4546474849505152535455565758596061626364653 unmodified lines697071727374757677787980818283848549 unmodified lines13513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117244 unmodified linesdef load_fine_tuned_model():"""Load the fine-tuned model from Hugging Face Hub"""try:# Get HuggingFace token from environmenthf_token = os.getenv("HF_API_KEY")if not hf_token:st.error("HuggingFace API token not found. Please set HF_API_KEY in your environment.")return None, None# Replace with your actual repository namemodel_name = "kundan621/tinyllama-makemytrip-financial-qa"# Load tokenizer with authenticationtokenizer = AutoTokenizer.from_pretrained(model_name,token=hf_token,trust_remote_code=True)# Load base modelbase_model = AutoModelForCausalLM.from_pretrained(3 unmodified linestrust_remote_code=True,)# Load the fine-tuned PEFT model with authenticationmodel = PeftModel.from_pretrained(base_model,model_name,token=hf_token)return model, tokenizerexcept Exception as e:st.error(f"Error loading fine-tuned model: {e}")st.info("Make sure your model repository is public or you have the correct access permissions.")return None, Nonedef generate_fine_tuned_response(model, tokenizer, question):49 unmodified linesst.set_page_config(page_title="Finance QA Assistant", layout="centered")st.title("Finance QA Assistant")# Add information about the modeswith st.expander("ℹ️ About the Modes"):st.markdown("""**RAG Mode**: Uses Retrieval-Augmented Generation with a vector database and external LLM API.**Fine-Tuned Mode**: Uses a custom fine-tuned TinyLlama model (requires authentication).*Note: Fine-tuned mode requires a HuggingFace API token and access to the private model repository.*""")# Load fine-tuned model if Fine-Tuned mode is availablefine_tuned_model, fine_tuned_tokenizer = None, None# Check if HuggingFace token is availablehf_token = os.getenv("HF_API_KEY")if hf_token:available_modes = ["RAG", "Fine-Tuned"]else:available_modes = ["RAG"]st.warning("⚠️ Fine-Tuned mode is not available. HuggingFace API token is required for accessing private models.")mode = st.radio("Choose Answering Mode:", available_modes, horizontal=True)if mode == "Fine-Tuned":if fine_tuned_model is None or fine_tuned_tokenizer is None:with st.spinner("Loading fine-tuned model..."):fine_tuned_model, fine_tuned_tokenizer = load_fine_tuned_model()# If model loading failed, fall back to RAGif fine_tuned_model is None or fine_tuned_tokenizer is None:st.error("Failed to load fine-tuned model. Falling back to RAG mode.")mode = "RAG"query = st.text_input("Enter your question:")
src/streamlit_app.py+43-5