krisha06 commited on
Commit
b085c4c
·
verified ·
1 Parent(s): 83e427a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -1,24 +1,23 @@
1
  import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
  # Load tokenizer and base model
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
- lora_path = "./lora_adapter" # make sure your LoRA adapter folder is named like this
9
-
10
- bnb_config = BitsAndBytesConfig(load_in_4bit=True,
11
- bnb_4bit_compute_dtype=torch.bfloat16)
12
 
13
  tokenizer = AutoTokenizer.from_pretrained(base_model)
14
- model = AutoModelForCausalLM.from_pretrained(base_model,
15
- quantization_config=bnb_config,
16
- torch_dtype=torch.bfloat16,
17
- device_map="auto")
18
 
 
 
19
  model = PeftModel.from_pretrained(model, lora_path)
20
  model.eval()
21
 
 
 
 
 
22
  # Streamlit UI
23
  st.set_page_config(page_title="🧠 TinyLLaMA Python Tutor (LoRA)")
24
  st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
@@ -27,11 +26,10 @@ st.write("Ask me any **Python programming** question:")
27
  user_input = st.text_input("Your question", placeholder="e.g. What is a lambda function in Python?")
28
 
29
  if user_input:
30
- # Filtering logic: Only answer Python-related queries
31
  if "python" not in user_input.lower() and "py" not in user_input.lower():
32
  st.warning("❌ Sorry, I can only answer Python programming questions.")
33
  else:
34
- # System prompt for tutor behavior
35
  system_prompt = (
36
  "You are a helpful and knowledgeable Python tutor. "
37
  "Answer the user's Python programming questions clearly and concisely. "
@@ -39,7 +37,7 @@ if user_input:
39
  )
40
  prompt = f"<|system|>\n{system_prompt}</s>\n<|user|>\n{user_input}</s>\n<|assistant|>"
41
 
42
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
43
 
44
  with torch.no_grad():
45
  with st.spinner("Thinking..."):
@@ -52,8 +50,7 @@ if user_input:
52
  eos_token_id=tokenizer.eos_token_id,
53
  pad_token_id=tokenizer.eos_token_id
54
  )
55
- decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
 
57
- # Extract answer only (remove prompt)
58
  answer = decoded_output.split("<|assistant|>")[-1].strip()
59
  st.success(f"💬 Answer:\n\n{answer}")
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
  # Load tokenizer and base model
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
+ lora_path = "./lora_adapter"
 
 
 
9
 
10
  tokenizer = AutoTokenizer.from_pretrained(base_model)
 
 
 
 
11
 
12
+ # Load base model normally (for CPU)
13
+ model = AutoModelForCausalLM.from_pretrained(base_model)
14
  model = PeftModel.from_pretrained(model, lora_path)
15
  model.eval()
16
 
17
+ # Move to CPU explicitly
18
+ device = torch.device("cpu")
19
+ model.to(device)
20
+
21
  # Streamlit UI
22
  st.set_page_config(page_title="🧠 TinyLLaMA Python Tutor (LoRA)")
23
  st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
 
26
  user_input = st.text_input("Your question", placeholder="e.g. What is a lambda function in Python?")
27
 
28
  if user_input:
29
+ # Check if it's a Python-related question
30
  if "python" not in user_input.lower() and "py" not in user_input.lower():
31
  st.warning("❌ Sorry, I can only answer Python programming questions.")
32
  else:
 
33
  system_prompt = (
34
  "You are a helpful and knowledgeable Python tutor. "
35
  "Answer the user's Python programming questions clearly and concisely. "
 
37
  )
38
  prompt = f"<|system|>\n{system_prompt}</s>\n<|user|>\n{user_input}</s>\n<|assistant|>"
39
 
40
+ inputs = tokenizer(prompt, return_tensors="pt").to(device)
41
 
42
  with torch.no_grad():
43
  with st.spinner("Thinking..."):
 
50
  eos_token_id=tokenizer.eos_token_id,
51
  pad_token_id=tokenizer.eos_token_id
52
  )
 
53
 
54
+ decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
55
  answer = decoded_output.split("<|assistant|>")[-1].strip()
56
  st.success(f"💬 Answer:\n\n{answer}")