krisha06 commited on
Commit
8c87824
·
verified ·
1 Parent(s): 10a1bb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -36
app.py CHANGED
@@ -1,53 +1,55 @@
1
  import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
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
- st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
9
- st.write("Ask me any Python programming question:")
 
 
 
 
10
 
11
- @st.cache_resource
12
- def load_model():
13
- base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
14
- adapter_path = "lora_adapter"
15
 
16
- tokenizer = AutoTokenizer.from_pretrained(base_model)
17
- model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float32)
18
- model = PeftModel.from_pretrained(model, adapter_path)
19
- model.eval()
20
 
21
- return tokenizer, model
 
22
 
23
- tokenizer, model = load_model()
 
24
 
25
- def build_prompt(question):
26
- return (
27
- "You are a helpful and concise Python programming tutor. "
28
- "If the question is not about Python, respond with: "
29
- "'Sorry, I can only answer Python-related questions.'\n\n"
30
- f"Question: {question}\nAnswer:"
31
- )
32
 
33
- question = st.text_input("Your question")
 
34
 
35
- if question:
36
- prompt = build_prompt(question)
37
- inputs = tokenizer(prompt, return_tensors="pt")
38
 
39
- with st.spinner("Thinking..."):
40
- outputs = model.generate(
41
  **inputs,
42
- max_new_tokens=250,
43
- temperature=0.6,
44
- top_p=0.85,
45
- repetition_penalty=1.2,
46
- pad_token_id=tokenizer.eos_token_id,
47
- eos_token_id=tokenizer.eos_token_id,
48
  )
49
 
50
- decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
51
- answer = decoded_output.split("Answer:")[-1].strip()
 
 
 
52
 
53
- st.markdown(f"**💬 Answer:**\n\n{answer}")
 
1
  import torch
 
2
  from peft import PeftModel
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import streamlit as st
5
 
6
+ # Load tokenizer
7
+ tokenizer = AutoTokenizer.from_pretrained("TinyLLaMA/TinyLLaMA-1.1B-Chat-v1.0")
8
 
9
+ # Load base model
10
+ base_model = AutoModelForCausalLM.from_pretrained(
11
+ "TinyLLaMA/TinyLLaMA-1.1B-Chat-v1.0",
12
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
13
+ device_map="auto"
14
+ )
15
 
16
+ # Load LoRA adapter
17
+ model = PeftModel.from_pretrained(base_model, "lora_adapter")
 
 
18
 
19
+ # Set title
20
+ st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
21
+ st.markdown("Ask me any **Python programming** question:")
 
22
 
23
+ # User input
24
+ user_question = st.text_input("Your question")
25
 
26
+ if user_question:
27
+ with st.spinner("Thinking..."):
28
 
29
+ # Clean prompt
30
+ prompt = f"""
31
+ You are a helpful and expert Python programming tutor.
32
+ If the question is about Python, explain clearly with examples.
33
+ If the question is unrelated to Python, respond with "Sorry, I can only answer Python-related questions."
 
 
34
 
35
+ Question: {user_question}
36
+ Answer:"""
37
 
38
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
 
39
 
40
+ output = model.generate(
 
41
  **inputs,
42
+ max_new_tokens=256,
43
+ do_sample=True,
44
+ top_p=0.9,
45
+ temperature=0.7,
46
+ repetition_penalty=1.1
 
47
  )
48
 
49
+ decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
50
+
51
+ # Extract only the generated answer after "Answer:"
52
+ answer_start = decoded_output.find("Answer:")
53
+ answer = decoded_output[answer_start + len("Answer:"):].strip() if answer_start != -1 else decoded_output.strip()
54
 
55
+ st.markdown(f"💬 **Answer:**\n\n{answer}")