krisha06 commited on
Commit
d029762
·
verified ·
1 Parent(s): 489fde8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -40
app.py CHANGED
@@ -1,56 +1,48 @@
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=512, # allow longer answers
43
- do_sample=True,
44
- top_p=0.9,
45
- temperature=0.7,
46
- repetition_penalty=1.1
47
-
48
- )
49
-
50
- decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
51
-
52
- # Extract only the generated answer after "Answer:"
53
- answer_start = decoded_output.find("Answer:")
54
- answer = decoded_output[answer_start + len("Answer:"):].strip() if answer_start != -1 else decoded_output.strip()
55
-
56
  st.markdown(f"💬 **Answer:**\n\n{answer}")
 
 
 
1
  import torch
2
  from peft import PeftModel
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
4
+ from transformers import pipeline
5
  import streamlit as st
6
 
7
+ # Set up offload directory for CPU offloading
8
+ offload_dir = "./offload"
9
+
10
  # Load tokenizer
11
+ tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
12
+
13
+ # Load base model with CPU offloading config
14
+ bnb_config = BitsAndBytesConfig(load_in_4bit=True)
15
 
 
16
  base_model = AutoModelForCausalLM.from_pretrained(
17
+ "TinyLlama/TinyLlama-1.1B-Chat-v1.0",
18
+ quantization_config=bnb_config,
19
+ device_map="auto", # required for offloading
20
+ offload_folder=offload_dir # this is the key line
21
  )
22
 
23
+ # Load your LoRA adapter
24
+ model = PeftModel.from_pretrained(base_model, "lora_adapter", device_map="auto")
25
 
26
+ # Text generation pipeline
27
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
 
28
 
29
+ # Streamlit UI
30
+ st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
31
+ st.write("Ask me any Python programming question:")
32
 
33
+ user_input = st.text_input("Your question")
 
34
 
35
+ if user_input:
36
+ # Check if question is Python-related
37
+ if "python" in user_input.lower() or "list" in user_input.lower() or "def " in user_input.lower() or "tuple" in user_input.lower() or "function" in user_input.lower():
38
+ prompt = f"""You are a helpful Python tutor. Answer only Python programming questions.
39
+ Respond clearly with examples. Avoid repeating the question.
40
 
41
+ Question: {user_input}
42
  Answer:"""
43
+
44
+ response = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)[0]["generated_text"]
45
+ answer = response.split("Answer:")[-1].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  st.markdown(f"💬 **Answer:**\n\n{answer}")
47
+ else:
48
+ st.markdown("❌ Sorry, I can only answer Python programming questions.")