krisha06 commited on
Commit
6c5f774
·
verified ·
1 Parent(s): 383b071

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -48
app.py CHANGED
@@ -3,54 +3,53 @@ 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)")
24
- st.write("Ask me any **Python programming** question:")
25
-
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 an expert Python tutor. Provide clear, concise, and accurate explanations with examples. "
35
- "If the user's question is not related to Python programming, respond with: "
36
- "'Sorry, I can only help with Python programming questions.'"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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..."):
44
- outputs = model.generate(
45
- **inputs,
46
- max_new_tokens=150,
47
- temperature=0.7,
48
- top_p=0.95,
49
- do_sample=True,
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}")
 
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
+ st.set_page_config(page_title="TinyLLaMA Python Tutor", layout="centered")
 
 
7
 
8
+ # Title
 
 
 
 
 
 
 
 
 
 
 
 
9
  st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
10
+ st.write("Ask me any Python programming question:")
11
+
12
+ # Load base model and LoRA adapter
13
+ @st.cache_resource
14
+ def load_model():
15
+ base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
16
+ adapter_path = "lora_adapter"
17
+
18
+ tokenizer = AutoTokenizer.from_pretrained(base_model)
19
+ model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float32)
20
+ model = PeftModel.from_pretrained(model, adapter_path)
21
+ model.eval()
22
+
23
+ return tokenizer, model
24
+
25
+ tokenizer, model = load_model()
26
+
27
+ # Prompt template
28
+ def build_prompt(question):
29
+ return (
30
+ "You are a helpful AI tutor that only answers Python programming questions. "
31
+ "If the user asks something unrelated to Python, respond with: "
32
+ "'Sorry, I can only answer Python-related questions.'\n\n"
33
+ f"Question: {question}\nAnswer:"
34
+ )
35
+
36
+ # Input box
37
+ question = st.text_input("Your question")
38
+
39
+ if question:
40
+ prompt = build_prompt(question)
41
+ inputs = tokenizer(prompt, return_tensors="pt")
42
+
43
+ with st.spinner("Thinking..."):
44
+ outputs = model.generate(
45
+ **inputs,
46
+ max_new_tokens=300, # 🔼 Increased from 200 to 300
47
+ temperature=0.6, # 🔽 More deterministic
48
+ top_p=0.9,
49
+ pad_token_id=tokenizer.eos_token_id
50
  )
51
+
52
+ decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
53
+ answer = decoded_output.split("Answer:")[-1].strip()
54
+
55
+ st.markdown(f"**💬 Answer:**\n\n{answer}")