Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
# Load the model and tokenizer with memory optimizations | |
model_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 set | |
if model.config.pad_token_id is None: | |
model.config.pad_token_id = model.config.eos_token_id | |
# Streamlit App Interface | |
st.title("Nutrition Chatbot") | |
user_input = st.text_input("Ask me about nutrition:") | |
if user_input: | |
# Truncate input and convert to tensors | |
inputs = 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 ID | |
try: | |
# Limit output length to save memory | |
outputs = 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 display | |
decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
st.write("Decoded Answer:", decoded_output) | |
except Exception as e: | |
st.write("Error generating output:", str(e)) | |