Izza-shahzad-13 commited on
Commit
62a166a
·
verified ·
1 Parent(s): d851e86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -14,6 +14,7 @@ if not HUGGINGFACE_TOKEN:
14
  # Function to load model and tokenizer (local or Hugging Face with token)
15
  def load_model(model_path):
16
  try:
 
17
  tokenizer = AutoTokenizer.from_pretrained(model_path, use_auth_token=HUGGINGFACE_TOKEN)
18
  model = AutoModelForSeq2SeqLM.from_pretrained(model_path, use_auth_token=HUGGINGFACE_TOKEN)
19
  return tokenizer, model
@@ -22,15 +23,17 @@ def load_model(model_path):
22
  return None, None
23
 
24
  # Set device (use GPU if available)
25
- device = "cuda" if torch.cuda.is_available() else "cpu"
26
 
27
  # Path to your model (either a local path or a Hugging Face model name)
28
  model_path = "Izza-shahzad-13/fine-tuned-flan-t5" # Use your Hugging Face model identifier
29
 
30
  # Load tokenizer and model
31
  tokenizer, model = load_model(model_path)
32
- if model:
33
  model.to(device)
 
 
34
 
35
  # Function to generate response from the model
36
  def generate_response(input_text):
@@ -55,9 +58,12 @@ st.title("FLAN-T5 Mental Health Counseling Assistant")
55
  st.write("Type your thoughts or feelings, and let the model respond.")
56
 
57
  # User input for interaction
58
- user_input = st.text_input("How are you feeling today?")
59
 
60
  # Generate and display model response when input is provided
61
- if user_input:
62
- response = generate_response(user_input)
 
63
  st.write("Model Response:", response)
 
 
 
14
  # Function to load model and tokenizer (local or Hugging Face with token)
15
  def load_model(model_path):
16
  try:
17
+ # Load tokenizer and model with authentication token if required
18
  tokenizer = AutoTokenizer.from_pretrained(model_path, use_auth_token=HUGGINGFACE_TOKEN)
19
  model = AutoModelForSeq2SeqLM.from_pretrained(model_path, use_auth_token=HUGGINGFACE_TOKEN)
20
  return tokenizer, model
 
23
  return None, None
24
 
25
  # Set device (use GPU if available)
26
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
27
 
28
  # Path to your model (either a local path or a Hugging Face model name)
29
  model_path = "Izza-shahzad-13/fine-tuned-flan-t5" # Use your Hugging Face model identifier
30
 
31
  # Load tokenizer and model
32
  tokenizer, model = load_model(model_path)
33
+ if model and tokenizer:
34
  model.to(device)
35
+ else:
36
+ st.stop() # Stop the app if model or tokenizer failed to load
37
 
38
  # Function to generate response from the model
39
  def generate_response(input_text):
 
58
  st.write("Type your thoughts or feelings, and let the model respond.")
59
 
60
  # User input for interaction
61
+ user_input = st.text_area("How are you feeling today?", placeholder="Type here...")
62
 
63
  # Generate and display model response when input is provided
64
+ if user_input.strip(): # Check if input is not empty
65
+ with st.spinner("Generating response..."):
66
+ response = generate_response(user_input)
67
  st.write("Model Response:", response)
68
+ else:
69
+ st.info("Please enter your thoughts or feelings in the text area above.")