Spaces:
Build error
Build error
Update app.py
Browse files1 unmodified line23456789105 unmodified lines161718192021222324252627282930313233343536373839401 unmodified linefrom transformers import AutoModelForCausalLM, AutoTokenizerimport torch# Load the model and tokenizermodel_name = "Tom158/Nutri_Assist"model = AutoModelForCausalLM.from_pretrained(model_name)tokenizer = AutoTokenizer.from_pretrained(model_name)# Set pad token if not already set5 unmodified linesuser_input = st.text_input("Ask me about nutrition:")if user_input:# Use encode_plus to get both input_ids and attention_maskinputs = tokenizer.encode_plus(user_input, return_tensors="pt", padding=True, truncation=True)input_ids = inputs['input_ids']attention_mask = inputs['attention_mask']# Print the input tensors for debuggingst.write("Input IDs:", input_ids)st.write("Attention Mask:", attention_mask)# Generate output with attention mask and pad token IDtry:# Increased max_length to 100 for more space for generation# Added temperature and top_k for better randomness and diversityoutputs = model.generate(input_ids, attention_mask=attention_mask, max_length=150,temperature=0.7, top_k=50, num_return_sequences=1)# Debugging model raw output (just the token ids)st.write("Model Output (Raw Token IDs):", outputs)# Decode the output and displaydecoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)st.write("Decoded Answer:", decoded_output)1 unmodified line23456789105 unmodified lines16171819202122232425262728293031321 unmodified linefrom transformers import AutoModelForCausalLM, AutoTokenizerimport torch# Load the model and tokenizer with memory optimizationsmodel_name = "Tom158/Nutri_Assist"model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")tokenizer = AutoTokenizer.from_pretrained(model_name)# Set pad token if not already set5 unmodified linesuser_input = st.text_input("Ask me about nutrition:")if user_input:# Truncate input and convert to tensorsinputs = tokenizer.encode_plus(user_input, return_tensors="pt", padding=True, truncation=True, max_length=512)input_ids = inputs['input_ids']attention_mask = inputs['attention_mask']# Generate output with attention mask and pad token IDtry:# Limit output length to save memoryoutputs = model.generate(input_ids, attention_mask=attention_mask, max_length=100,temperature=0.7, top_k=50, num_return_sequences=1)# Decode the output and displaydecoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)st.write("Decoded Answer:", decoded_output)
app.py+8-16