addcorpbc commited on
Commit ·
821a209
1
Parent(s): 97f322e
Update app.py to include Hugging Face token handling
Browse files123456789101112132 unmodified lines1617181920212210 unmodified lines333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980from transformers import AutoTokenizer, AutoModelForCausalLMimport gradio as grimport osimport time# Authenticate using a token from environment variableshf_token = os.getenv("HFTOKEN")if not hf_token:raise ValueError("Hugging Face token is missing. Please add it as an environment variable named 'HFTOKEN'.")# Set up caching directorycache_dir = "./model_cache"os.makedirs(cache_dir, exist_ok=True)2 unmodified linestokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf", cache_dir=cache_dir, use_auth_token=hf_token)model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf",device_map="auto", # Automatically map layers to available hardware (CPU here)cache_dir=cache_dir,use_auth_token=hf_token # Pass the token for authenticated model loading)10 unmodified linesoutputs = model.generate(**inputs, max_new_tokens=200)return tokenizer.decode(outputs[0], skip_special_tokens=True)def welcome_message():"""Simulate typing out the welcome message in the output box."""message = """Hi, I'm your Magic City Career Counselor! Big Magic made me to help you plan for your future.He cares about his employees and wants to make sure you have a future after Magic City.To do that, we've consulted dozens of experts to help you decide on the direction you may want to take in the future.We offer five different assessments you can take, or we can simply chat about your plans. The assessments include:1. The Myers-Briggs Type Indicator2. The Indecisiveness Scale3. RIASEC4. The Career Thought Inventory5. The Career Values Card Sort"""words = message.split(" ")output = ""for word in words:output += word + " "yield output # Output progressivelytime.sleep(0.1) # Typing delay between words# Define the Gradio interface with default designinterface = gr.Blocks()with interface:# Welcome message that types out in the output fieldgr.Interface(fn=welcome_message,inputs=None,outputs=gr.Textbox(label="Welcome Message", interactive=False, elem_id="welcome-box"), # Output in the same boxtitle="AI-Powered Magic City Career Counselor", # Updated titledescription="Let’s start by exploring your career interests! You will see a welcome message typing out in the box below.")# Career recommendation input/outputgr.Interface(fn=recommend,inputs=gr.Textbox(label="Describe your career interests and goals", lines=2, elem_id="input-box"),outputs=gr.Textbox(label="Career Recommendations", lines=4, elem_id="output-box"),title="AI-Powered Magic City Career Counselor", # Updated titledescription="Provide a description of your career goals, and get personalized career suggestions!" # Description update)# Launch the Gradio app with default theme (no Cobalt)interface.launch(share=True)1234567891011121314152 unmodified lines181920212223242510 unmodified lines3637383940414243444546474849from transformers import AutoTokenizer, AutoModelForCausalLMimport gradio as grimport os# Authenticate using a token from environment variableshf_token = os.getenv("HFTOKEN") # Updated to match your environment variable nameif not hf_token:raise ValueError("Hugging Face token is missing. Please add it as an environment variable named 'HFTOKEN'.")# Print the token for debuggingprint("Hugging Face Token:", hf_token)# Set up caching directorycache_dir = "./model_cache"os.makedirs(cache_dir, exist_ok=True)2 unmodified linestokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf", cache_dir=cache_dir, use_auth_token=hf_token)model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf",load_in_8bit=True, # Enable quantization for better performancedevice_map="auto", # Automatically map layers to available hardwarecache_dir=cache_dir,use_auth_token=hf_token # Pass the token for authenticated model loading)10 unmodified linesoutputs = model.generate(**inputs, max_new_tokens=200)return tokenizer.decode(outputs[0], skip_special_tokens=True)# Define the Gradio interfaceinterface = gr.Interface(fn=recommend,inputs=gr.Textbox(label="Describe your career interests and goals"),outputs="text",title="AI-Powered Career Recommendation Tool (LLaMA)",description="Provide a description of your career goals, and get personalized career suggestions!")# Launch the Gradio appinterface.launch()
app.py+16-47