amaltese commited on
Commit
f7717dd
·
verified ·
1 Parent(s): 55c0785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -37
app.py CHANGED
@@ -1,21 +1,3 @@
1
- import streamlit as st
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
-
5
- # Load the Zephyr-7B-Alpha model (fully open and optimized for instruction-following)
6
- MODEL_NAME = "HuggingFaceH4/zephyr-7b-alpha"
7
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
- model = AutoModelForCausalLM.from_pretrained(
9
- MODEL_NAME,
10
- torch_dtype=torch.float16,
11
- device_map="cpu", # Forces CPU usage
12
- low_cpu_mem_usage=True # Helps reduce memory spikes
13
- )
14
-
15
- # Initialize conversation history if not present
16
- if "conversation" not in st.session_state:
17
- st.session_state.conversation = []
18
-
19
  def get_response(user_input):
20
  """Generate a thoughtful response that includes a follow-up question."""
21
  history = "\n".join(st.session_state.conversation[-5:]) # Keep only the last 5 turns
@@ -26,24 +8,19 @@ def get_response(user_input):
26
  f"Student: {user_input}\n"
27
  f"Coach: "
28
  )
29
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
30
- with torch.no_grad():
31
- output = model.generate(input_ids, max_length=300)
32
- response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
33
- return response
34
-
35
- # Streamlit UI
36
- st.title("📚 Study Buddy Chatbot")
37
- st.write("Ask a question or type a topic, and I'll help you learn interactively!")
38
 
39
- user_input = st.text_input("Type your question or topic:")
40
- if user_input:
41
- response = get_response(user_input)
42
- st.session_state.conversation.append(f"Student: {user_input}")
43
- st.session_state.conversation.append(f"Coach: {response}")
44
- st.write("🤖 Coach:", response)
45
 
46
- # Display conversation history
47
- st.subheader("Conversation History")
48
- for chat in st.session_state.conversation[-10:]:
49
- st.write(chat)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def get_response(user_input):
2
  """Generate a thoughtful response that includes a follow-up question."""
3
  history = "\n".join(st.session_state.conversation[-5:]) # Keep only the last 5 turns
 
8
  f"Student: {user_input}\n"
9
  f"Coach: "
10
  )
 
 
 
 
 
 
 
 
 
11
 
12
+ # Tokenize input with padding and attention mask
13
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
14
+ input_ids = inputs.input_ids.to(model.device)
15
+ attention_mask = inputs.attention_mask.to(model.device)
 
 
16
 
17
+ with torch.no_grad():
18
+ output = model.generate(
19
+ input_ids,
20
+ attention_mask=attention_mask,
21
+ max_length=300,
22
+ pad_token_id=tokenizer.eos_token_id # Ensures correct token handling
23
+ )
24
+
25
+ response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
26
+ return response