krisha06 commited on
Commit
9b175a1
·
verified ·
1 Parent(s): 84376c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -28
app.py CHANGED
@@ -3,35 +3,25 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
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 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.
25
  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
- {instruction}
31
-
32
- ### ASSISTANT:
33
- """
34
 
 
35
  def chat(instruction):
36
  prompt = format_prompt(instruction)
37
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
@@ -48,17 +38,12 @@ def chat(instruction):
48
 
49
  full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
50
 
51
- # Extract only the final answer
52
- if "### Answer:" in full_output:
53
- return full_output.split("### Answer:")[-1].strip()
54
- elif "### ASSISTANT:" in full_output:
55
- return full_output.split("### ASSISTANT:")[-1].strip()
56
- else:
57
- return full_output.strip()
58
 
59
  # Streamlit UI
60
- st.set_page_config(page_title="🐍 Python Tutor Chatbot")
61
- st.title("🐍 Python Tutor Chatbot(LoRA")
62
  st.write("Ask me Python programming questions!")
63
 
64
  user_input = st.text_area("Your question:")
@@ -67,4 +52,7 @@ if st.button("Get Answer") and user_input.strip():
67
  with st.spinner("Thinking..."):
68
  response = chat(user_input)
69
  st.markdown("**Answer:**")
70
- st.write(response)
 
 
 
 
3
  from peft import PeftModel
4
  import streamlit as st
5
 
6
+ # Load tokenizer and model
7
  base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
8
  tokenizer = AutoTokenizer.from_pretrained(base_model)
9
 
10
+ model = AutoModelForCausalLM.from_pretrained(base_model, device_map="cpu")
11
+ model = PeftModel.from_pretrained(model, "lora_adapter") # Change if needed
 
 
 
 
 
 
 
12
  model.eval()
13
 
14
+ # Format prompt (no USER/ASSISTANT lines to confuse model)
15
  def format_prompt(instruction):
16
+ return f"""You are a helpful and expert Python programming tutor.
 
17
  You only answer questions related to Python programming.
18
  If the question is unrelated to Python, say:
19
  "Sorry, I can only answer Python-related questions."
20
 
21
+ Question: {instruction}
22
+ Answer:"""
 
 
 
23
 
24
+ # Generate answer
25
  def chat(instruction):
26
  prompt = format_prompt(instruction)
27
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
38
 
39
  full_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
 
41
+ # Only return the model's answer
42
+ return full_output.split("Answer:")[-1].strip()
 
 
 
 
 
43
 
44
  # Streamlit UI
45
+ st.set_page_config(page_title="🐍 Python Tutor Chatbot (LoRA)")
46
+ st.title("🐍 Python Tutor Chatbot (LoRA)")
47
  st.write("Ask me Python programming questions!")
48
 
49
  user_input = st.text_area("Your question:")
 
52
  with st.spinner("Thinking..."):
53
  response = chat(user_input)
54
  st.markdown("**Answer:**")
55
+ if "```" in response:
56
+ st.markdown(response)
57
+ else:
58
+ st.write(response)