krisha06 commited on
Commit
01247e0
·
verified ·
1 Parent(s): 5deaa96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -35
app.py CHANGED
@@ -1,65 +1,58 @@
1
- import os
2
  import torch
3
- import streamlit as st
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
5
  from peft import PeftModel
6
-
7
- # Set CPU device
8
- device = torch.device("cpu")
9
 
10
  # Load base model and tokenizer
11
- model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
12
- tokenizer = AutoTokenizer.from_pretrained(model_name)
13
 
14
- base_model = AutoModelForCausalLM.from_pretrained(
15
- model_name,
16
- torch_dtype=torch.float32, # You can try float16 if supported
17
- device_map={"": device}
18
  )
19
 
20
- # Load LoRA adapter (your fine-tuned weights)
21
- model = PeftModel.from_pretrained(base_model, "lora_adapter", device_map={"": device})
22
  model.eval()
23
 
24
- # Format the prompt with filtering
25
  def format_prompt(instruction):
26
- return f"""You are a helpful and expert Python programming tutor.
27
- Only answer questions related to Python programming.
28
- If the question is unrelated to Python, respond with:
29
- "Sorry, I can only answer Python-related questions."
 
30
 
31
- ### Instruction:
32
  {instruction}
33
 
34
- ### Response:
35
  """
36
 
37
- # Generate answer
38
  def chat(instruction):
39
  prompt = format_prompt(instruction)
40
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
41
  outputs = model.generate(
42
  **inputs,
43
  max_new_tokens=256,
44
- do_sample=True,
45
- temperature=0.7,
46
- top_p=0.9,
47
  repetition_penalty=1.1
48
  )
49
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
50
- return response.split("### Response:")[-1].strip()
51
 
52
  # Streamlit UI
 
53
  st.title("🐍 Python Tutor Chatbot")
54
- st.markdown("Ask me Python programming questions!")
55
 
56
- question = st.text_area("Your question:")
57
 
58
- if st.button("Answer"):
59
- if question.strip():
60
- with st.spinner("Thinking..."):
61
- response = chat(question)
62
  st.markdown("**Answer:**")
63
  st.write(response)
64
- else:
65
- st.warning("Please enter a question.")
 
 
1
  import torch
2
+ 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 in CPU-only mode
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ base_model,
13
+ device_map="cpu"
14
  )
15
 
16
+ # Load the LoRA adapter
17
+ model = PeftModel.from_pretrained(model, "lora_adapter")
18
  model.eval()
19
 
 
20
  def format_prompt(instruction):
21
+ return f"""### SYSTEM:
22
+ You are a helpful and expert Python programming tutor.
23
+ You only answer questions related to Python programming.
24
+ If the question is unrelated to Python, say:
25
+ \"Sorry, I can only answer Python-related questions.\"
26
 
27
+ ### USER:
28
  {instruction}
29
 
30
+ ### ASSISTANT:
31
  """
32
 
 
33
  def chat(instruction):
34
  prompt = format_prompt(instruction)
35
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
36
  outputs = model.generate(
37
  **inputs,
38
  max_new_tokens=256,
39
+ do_sample=False,
40
+ temperature=0.0,
41
+ top_p=1.0,
42
  repetition_penalty=1.1
43
  )
44
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
45
+ return response.split("### ASSISTANT:")[-1].strip()
46
 
47
  # Streamlit UI
48
+ st.set_page_config(page_title="🐍 Python Tutor Chatbot")
49
  st.title("🐍 Python Tutor Chatbot")
50
+ st.write("Ask me Python programming questions!")
51
 
52
+ user_input = st.text_area("Your question:")
53
 
54
+ if st.button("Get Answer") and user_input.strip():
55
+ with st.spinner("Thinking..."):
56
+ response = chat(user_input)
 
57
  st.markdown("**Answer:**")
58
  st.write(response)