krisha06 commited on
Commit
2f006a2
·
verified ·
1 Parent(s): 75a88cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -9
app.py CHANGED
@@ -3,24 +3,22 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
- # Load tokenizer
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(base_model)
9
 
10
- # Load base model in empty (meta) state and move to CPU
11
  model = AutoModelForCausalLM.from_pretrained(
12
  base_model,
13
  torch_dtype=torch.float32,
14
- low_cpu_mem_usage=True,
15
- device_map="auto"
16
  )
17
- model = model.to_empty(device=torch.device("cpu"))
18
 
19
- # Load LoRA adapter and move to CPU
20
  model = PeftModel.from_pretrained(model, "lora_adapter", device_map="cpu")
21
  model.eval()
22
 
23
- # Format prompt for Python tutoring
24
  def format_prompt(instruction):
25
  return f"""### SYSTEM:
26
  You are a helpful and expert Python programming tutor.
@@ -28,16 +26,23 @@ You only answer questions related to Python programming.
28
  If the question is unrelated to Python, say:
29
  "Sorry, I can only answer Python-related questions."
30
 
 
 
 
 
 
 
31
  ### USER:
32
  {instruction}
33
 
34
  ### ASSISTANT:
35
  """
36
 
37
- # Generate answer
38
  def chat(instruction):
39
  prompt = format_prompt(instruction)
40
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
41
  outputs = model.generate(
42
  **inputs,
43
  max_new_tokens=256,
@@ -46,8 +51,15 @@ def chat(instruction):
46
  top_p=1.0,
47
  repetition_penalty=1.1
48
  )
 
49
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
50
- return response.split("### ASSISTANT:")[-1].strip()
 
 
 
 
 
 
51
 
52
  # Streamlit UI
53
  st.set_page_config(page_title="🐍 Python Tutor Chatbot")
@@ -61,3 +73,7 @@ if st.button("Get Answer") and user_input.strip():
61
  response = chat(user_input)
62
  st.markdown("**Answer:**")
63
  st.write(response)
 
 
 
 
 
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
+ # Load base model and tokenizer
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(base_model)
9
 
10
+ # Load model in CPU mode
11
  model = AutoModelForCausalLM.from_pretrained(
12
  base_model,
13
  torch_dtype=torch.float32,
14
+ device_map="cpu"
 
15
  )
 
16
 
17
+ # Load the LoRA adapter
18
  model = PeftModel.from_pretrained(model, "lora_adapter", device_map="cpu")
19
  model.eval()
20
 
21
+ # Format prompt with example
22
  def format_prompt(instruction):
23
  return f"""### SYSTEM:
24
  You are a helpful and expert Python programming tutor.
 
26
  If the question is unrelated to Python, say:
27
  "Sorry, I can only answer Python-related questions."
28
 
29
+ ### USER:
30
+ What is the difference between a list and a tuple in Python?
31
+
32
+ ### ASSISTANT:
33
+ In Python, both lists and tuples are used to store collections of items, but they have key differences. Lists are mutable (can be changed), whereas tuples are immutable (cannot be changed). Lists use square brackets [], and tuples use parentheses (). Tuples are generally faster and use less memory.
34
+
35
  ### USER:
36
  {instruction}
37
 
38
  ### ASSISTANT:
39
  """
40
 
41
+ # Chat function
42
  def chat(instruction):
43
  prompt = format_prompt(instruction)
44
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
45
+
46
  outputs = model.generate(
47
  **inputs,
48
  max_new_tokens=256,
 
51
  top_p=1.0,
52
  repetition_penalty=1.1
53
  )
54
+
55
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
+ print("🧠 Raw Model Output:", repr(response)) # Debug log
57
+
58
+ # Extract response cleanly
59
+ if "### ASSISTANT:" in response:
60
+ return response.split("### ASSISTANT:")[-1].strip()
61
+ else:
62
+ return response.strip() # fallback
63
 
64
  # Streamlit UI
65
  st.set_page_config(page_title="🐍 Python Tutor Chatbot")
 
73
  response = chat(user_input)
74
  st.markdown("**Answer:**")
75
  st.write(response)
76
+
77
+ # Optional debug output
78
+ # st.write("**Raw model output:**")
79
+ # st.write(repr(response))