Mattral commited on
Commit
a0c938b
·
verified ·
1 Parent(s): b88e8f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -19
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
- from transformers import pipeline
 
3
  import os
4
  from dotenv import load_dotenv
5
 
@@ -11,22 +12,33 @@ HF_TOKEN = os.getenv("HF_TOKEN")
11
  st.title("I am Your GrowBuddy 🌱")
12
  st.write("Let me help you start gardening. Let's grow together!")
13
 
14
- # Function to load the pipeline
15
- def load_pipeline():
16
  try:
17
- # Create the text-generation pipeline using the model from Hugging Face
18
- pipe = pipeline("text-generation", model="TheSheBots/UrbanGardening", use_auth_token=HF_TOKEN)
19
- return pipe
 
 
 
 
 
 
 
20
  except Exception as e:
21
- st.error(f"Failed to load model pipeline: {e}")
22
- return None
23
 
24
- # Load the pipeline (cached)
25
- pipe = load_pipeline()
26
 
27
- if not pipe:
28
  st.stop()
29
 
 
 
 
 
30
  # Initialize session state messages
31
  if "messages" not in st.session_state:
32
  st.session_state.messages = [
@@ -38,6 +50,37 @@ for message in st.session_state.messages:
38
  with st.chat_message(message["role"]):
39
  st.write(message["content"])
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  # User input field for gardening questions
42
  user_input = st.chat_input("Type your gardening question here:")
43
 
@@ -47,15 +90,10 @@ if user_input:
47
 
48
  with st.chat_message("assistant"):
49
  with st.spinner("Generating your answer..."):
50
- try:
51
- # Generate the response using the pipeline
52
- response = pipe(user_input, max_length=150, num_return_sequences=1, temperature=0.7)[0]['generated_text']
53
- st.write(response)
54
- except Exception as e:
55
- st.error(f"Error during text generation: {e}")
56
- response = "Sorry, I couldn't process your request."
57
- st.write(response)
58
 
59
  # Update session state
60
  st.session_state.messages.append({"role": "user", "content": user_input})
61
  st.session_state.messages.append({"role": "assistant", "content": response})
 
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
  import os
5
  from dotenv import load_dotenv
6
 
 
12
  st.title("I am Your GrowBuddy 🌱")
13
  st.write("Let me help you start gardening. Let's grow together!")
14
 
15
+ # Function to load model only once
16
+ def load_model():
17
  try:
18
+ # If model and tokenizer are already in session state, return them
19
+ if "tokenizer" in st.session_state and "model" in st.session_state:
20
+ return st.session_state.tokenizer, st.session_state.model
21
+ else:
22
+ tokenizer = AutoTokenizer.from_pretrained("TheSheBots/UrbanGardening", use_auth_token=HF_TOKEN)
23
+ model = AutoModelForCausalLM.from_pretrained("TheSheBots/UrbanGardening", use_auth_token=HF_TOKEN)
24
+ # Store the model and tokenizer in session state
25
+ st.session_state.tokenizer = tokenizer
26
+ st.session_state.model = model
27
+ return tokenizer, model
28
  except Exception as e:
29
+ st.error(f"Failed to load model: {e}")
30
+ return None, None
31
 
32
+ # Load model and tokenizer (cached)
33
+ tokenizer, model = load_model()
34
 
35
+ if not tokenizer or not model:
36
  st.stop()
37
 
38
+ # Default to CPU, or use GPU if available
39
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40
+ model = model.to(device)
41
+
42
  # Initialize session state messages
43
  if "messages" not in st.session_state:
44
  st.session_state.messages = [
 
50
  with st.chat_message(message["role"]):
51
  st.write(message["content"])
52
 
53
+ # Create a text area to display logs
54
+ log_box = st.empty()
55
+
56
+ # Function to generate response with debugging logs
57
+ def generate_response(prompt):
58
+ try:
59
+ # Tokenize input prompt with dynamic padding and truncation
60
+ log_box.text_area("Debugging Logs", "Tokenizing the prompt...", height=200)
61
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True, max_length=512).to(device)
62
+
63
+ # Display tokenized inputs
64
+ log_box.text_area("Debugging Logs", f"Tokenized inputs: {inputs['input_ids']}", height=200)
65
+
66
+ # Generate output from model
67
+ log_box.text_area("Debugging Logs", "Generating output...", height=200)
68
+ outputs = model.generate(inputs["input_ids"], max_new_tokens=100, temperature=0.7, do_sample=True)
69
+
70
+ # Display the raw output from the model
71
+ log_box.text_area("Debugging Logs", f"Raw model output (tokens): {outputs}", height=200)
72
+
73
+ # Decode and return response
74
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
75
+
76
+ # Display the final decoded response
77
+ log_box.text_area("Debugging Logs", f"Decoded response: {response}", height=200)
78
+
79
+ return response
80
+ except Exception as e:
81
+ st.error(f"Error during text generation: {e}")
82
+ return "Sorry, I couldn't process your request."
83
+
84
  # User input field for gardening questions
85
  user_input = st.chat_input("Type your gardening question here:")
86
 
 
90
 
91
  with st.chat_message("assistant"):
92
  with st.spinner("Generating your answer..."):
93
+ response = generate_response(user_input)
94
+ st.write(response)
 
 
 
 
 
 
95
 
96
  # Update session state
97
  st.session_state.messages.append({"role": "user", "content": user_input})
98
  st.session_state.messages.append({"role": "assistant", "content": response})
99
+