krisha06 commited on
Commit
dea7d9a
·
verified ·
1 Parent(s): 39a14f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -26
app.py CHANGED
@@ -1,48 +1,78 @@
1
- import streamlit as st
2
  import torch
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
4
 
5
- # Load model and tokenizer
6
- model_name = "lora_adapter" # Update this to your LoRA model path
7
 
8
- tokenizer = AutoTokenizer.from_pretrained(model_name)
9
- model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
10
 
11
- # Chat function with prompt-based filtering
12
- def chat(instruction):
13
- prompt = f"""You are a helpful and expert Python programming tutor.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  Only answer questions related to Python programming.
15
  If the question is unrelated to Python, respond with:
16
  "Sorry, I can only answer Python-related questions."
17
 
18
- Answer the following instruction with a structured and informative format.
19
  ### Instruction:
20
  {instruction}
21
 
22
  ### Response:
23
  """
 
 
 
 
24
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
25
- with torch.no_grad():
26
- outputs = model.generate(
27
- **inputs,
28
- max_new_tokens=150,
29
- temperature=0.7,
30
- top_p=0.95,
31
- do_sample=True,
32
- pad_token_id=tokenizer.eos_token_id
33
- )
34
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
  return response.split("### Response:")[-1].strip()
36
 
37
  # Streamlit UI
38
- st.set_page_config(page_title="Python Tutor Chatbot", page_icon="🐍")
39
  st.title("🐍 Python Tutor Chatbot")
40
- st.write("Ask me Python programming questions!")
41
 
42
- user_input = st.text_input("Your question:")
43
 
44
- if user_input:
45
- with st.spinner("Generating response..."):
46
- response = chat(user_input)
 
47
  st.markdown("**Answer:**")
48
- st.markdown(response)
 
 
 
1
+ import os
2
  import torch
3
+ import streamlit as st
4
+ from transformers import (
5
+ AutoModelForCausalLM,
6
+ AutoTokenizer,
7
+ BitsAndBytesConfig
8
+ )
9
+ from peft import PeftModel
10
 
11
+ # Offload directory for CPU inference
12
+ os.makedirs("offload", exist_ok=True)
13
 
14
+ # Load base model + quantization config
15
+ model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
16
 
17
+ bnb_config = BitsAndBytesConfig(
18
+ load_in_8bit=True,
19
+ llm_int8_threshold=6.0,
20
+ llm_int8_enable_fp32_cpu_offload=True
21
+ )
22
+
23
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
24
+ base_model = AutoModelForCausalLM.from_pretrained(
25
+ model_name,
26
+ quantization_config=bnb_config,
27
+ device_map="auto",
28
+ offload_folder="offload"
29
+ )
30
+
31
+ # Load LoRA adapter
32
+ model = PeftModel.from_pretrained(base_model, "lora_adapter")
33
+
34
+ # Evaluation mode
35
+ model.eval()
36
+
37
+ # Prompt template
38
+ def format_prompt(instruction):
39
+ return f"""You are a helpful and expert Python programming tutor.
40
  Only answer questions related to Python programming.
41
  If the question is unrelated to Python, respond with:
42
  "Sorry, I can only answer Python-related questions."
43
 
 
44
  ### Instruction:
45
  {instruction}
46
 
47
  ### Response:
48
  """
49
+
50
+ # Chat function
51
+ def chat(instruction):
52
+ prompt = format_prompt(instruction)
53
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
54
+ outputs = model.generate(
55
+ **inputs,
56
+ max_new_tokens=256,
57
+ do_sample=True,
58
+ temperature=0.7,
59
+ top_p=0.95,
60
+ repetition_penalty=1.2
61
+ )
 
62
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
63
  return response.split("### Response:")[-1].strip()
64
 
65
  # Streamlit UI
 
66
  st.title("🐍 Python Tutor Chatbot")
67
+ st.markdown("Ask me Python programming questions!")
68
 
69
+ user_input = st.text_area("Your question:")
70
 
71
+ if st.button("Answer"):
72
+ if user_input.strip():
73
+ with st.spinner("Thinking..."):
74
+ answer = chat(user_input)
75
  st.markdown("**Answer:**")
76
+ st.write(answer)
77
+ else:
78
+ st.warning("Please enter a question.")