File size: 2,149 Bytes
75b206c
b085c4c
a2519bb
5b56a67
 
 
83e427a
b085c4c
83e427a
 
 
b085c4c
 
83e427a
5b56a67
 
b085c4c
 
 
 
5b56a67
83e427a
5b56a67
 
 
83e427a
5b56a67
 
b085c4c
83e427a
 
5b56a67
83e427a
383b071
 
 
83e427a
 
 
b085c4c
83e427a
 
 
 
 
 
 
 
 
 
 
 
 
b085c4c
83e427a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import streamlit as st

# Load tokenizer and base model
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
lora_path = "./lora_adapter"

tokenizer = AutoTokenizer.from_pretrained(base_model)

# Load base model normally (for CPU)
model = AutoModelForCausalLM.from_pretrained(base_model)
model = PeftModel.from_pretrained(model, lora_path)
model.eval()

# Move to CPU explicitly
device = torch.device("cpu")
model.to(device)

# Streamlit UI
st.set_page_config(page_title="🧠 TinyLLaMA Python Tutor (LoRA)")
st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
st.write("Ask me any **Python programming** question:")

user_input = st.text_input("Your question", placeholder="e.g. What is a lambda function in Python?")

if user_input:
    # Check if it's a Python-related question
    if "python" not in user_input.lower() and "py" not in user_input.lower():
        st.warning("❌ Sorry, I can only answer Python programming questions.")
    else:
        system_prompt = (
            "You are an expert Python tutor. Provide clear, concise, and accurate explanations with examples. "
            "If the user's question is not related to Python programming, respond with: "
            "'Sorry, I can only help with Python programming questions.'"
        )
        prompt = f"<|system|>\n{system_prompt}</s>\n<|user|>\n{user_input}</s>\n<|assistant|>"

        inputs = tokenizer(prompt, return_tensors="pt").to(device)

        with torch.no_grad():
            with st.spinner("Thinking..."):
                outputs = model.generate(
                    **inputs,
                    max_new_tokens=150,
                    temperature=0.7,
                    top_p=0.95,
                    do_sample=True,
                    eos_token_id=tokenizer.eos_token_id,
                    pad_token_id=tokenizer.eos_token_id
                )

            decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
            answer = decoded_output.split("<|assistant|>")[-1].strip()
            st.success(f"💬 Answer:\n\n{answer}")