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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
app.py CHANGED
@@ -7,18 +7,18 @@ import streamlit as st
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,12 +26,6 @@ You only answer questions related to Python programming.
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
 
@@ -43,23 +37,26 @@ 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,
49
- do_sample=False,
50
- temperature=0.0,
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,7 +70,3 @@ if st.button("Get Answer") and user_input.strip():
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))
 
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(base_model)
9
 
10
+ # Load base model on CPU
11
  model = AutoModelForCausalLM.from_pretrained(
12
  base_model,
13
  torch_dtype=torch.float32,
14
  device_map="cpu"
15
  )
16
 
17
+ # Load LoRA adapter
18
  model = PeftModel.from_pretrained(model, "lora_adapter", device_map="cpu")
19
  model.eval()
20
 
21
+ # Prompt template
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
  {instruction}
31
 
 
37
  prompt = format_prompt(instruction)
38
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
39
 
40
+ with torch.no_grad():
41
+ outputs = model.generate(
42
+ **inputs,
43
+ max_new_tokens=512,
44
+ do_sample=False,
45
+ temperature=0.7,
46
+ top_p=0.9,
47
+ repetition_penalty=1.1
48
+ )
49
 
50
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
51
 
52
+ # Debugging optional:
53
+ # print("🧠 Full output:", repr(response))
54
+
55
+ # Split and return the assistant's answer
56
  if "### ASSISTANT:" in response:
57
  return response.split("### ASSISTANT:")[-1].strip()
58
  else:
59
+ return response.strip()
60
 
61
  # Streamlit UI
62
  st.set_page_config(page_title="🐍 Python Tutor Chatbot")
 
70
  response = chat(user_input)
71
  st.markdown("**Answer:**")
72
  st.write(response)