File size: 2,404 Bytes
75b206c
01247e0
dea7d9a
01247e0
5baf039
2f006a2
01247e0
 
5baf039
2f006a2
01247e0
 
ca66e20
2f006a2
dea7d9a
ca66e20
2f006a2
ca66e20
dea7d9a
 
2f006a2
dea7d9a
01247e0
 
 
 
5d1b435
 
2f006a2
 
 
 
 
 
01247e0
a8f1aa1
6c5f774
01247e0
5baf039
dea7d9a
2f006a2
dea7d9a
 
01247e0
2f006a2
dea7d9a
 
 
01247e0
 
 
5deaa96
dea7d9a
2f006a2
a8f1aa1
2f006a2
 
 
 
 
 
 
a8f1aa1
 
01247e0
a8f1aa1
01247e0
6c5f774
01247e0
6c5f774
01247e0
 
 
5baf039
5deaa96
2f006a2
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import streamlit as st

# Load base model and tokenizer
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
tokenizer = AutoTokenizer.from_pretrained(base_model)

# Load model in CPU mode
model = AutoModelForCausalLM.from_pretrained(
    base_model,
    torch_dtype=torch.float32,
    device_map="cpu"
)

# Load the LoRA adapter
model = PeftModel.from_pretrained(model, "lora_adapter", device_map="cpu")
model.eval()

# Format prompt with example
def format_prompt(instruction):
    return f"""### SYSTEM:
You are a helpful and expert Python programming tutor.
You only answer questions related to Python programming.
If the question is unrelated to Python, say:
"Sorry, I can only answer Python-related questions."

### USER:
What is the difference between a list and a tuple in Python?

### ASSISTANT:
In Python, both lists and tuples are used to store collections of items, but they have key differences. Lists are mutable (can be changed), whereas tuples are immutable (cannot be changed). Lists use square brackets [], and tuples use parentheses (). Tuples are generally faster and use less memory.

### USER:
{instruction}

### ASSISTANT:
"""

# Chat function
def chat(instruction):
    prompt = format_prompt(instruction)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

    outputs = model.generate(
        **inputs,
        max_new_tokens=256,
        do_sample=False,
        temperature=0.0,
        top_p=1.0,
        repetition_penalty=1.1
    )

    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    print("🧠 Raw Model Output:", repr(response))  # Debug log

    # Extract response cleanly
    if "### ASSISTANT:" in response:
        return response.split("### ASSISTANT:")[-1].strip()
    else:
        return response.strip()  # fallback

# Streamlit UI
st.set_page_config(page_title="🐍 Python Tutor Chatbot")
st.title("🐍 Python Tutor Chatbot")
st.write("Ask me Python programming questions!")

user_input = st.text_area("Your question:")

if st.button("Get Answer") and user_input.strip():
    with st.spinner("Thinking..."):
        response = chat(user_input)
        st.markdown("**Answer:**")
        st.write(response)

        # Optional debug output
        # st.write("**Raw model output:**")
        # st.write(repr(response))