File size: 2,027 Bytes
75b206c
01247e0
60e502c
dd686dc
60e502c
 
 
dd686dc
60e502c
 
 
 
 
 
 
 
dd686dc
60e502c
dd686dc
3472004
5baf039
3472004
5d1b435
 
3472004
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dea7d9a
60e502c
 
 
dd686dc
60e502c
 
 
 
 
 
 
 
3472004
 
60e502c
 
dd686dc
 
 
60e502c
 
 
 
 
 
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
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import streamlit as st

# Load base + LoRA model
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
adapter_path = "./lora_adapter"  # your uploaded LoRA adapter

tokenizer = AutoTokenizer.from_pretrained(base_model)
model = AutoModelForCausalLM.from_pretrained(
    base_model,
    device_map="auto",
    torch_dtype=torch.float32
)
model.load_adapter(adapter_path)
model.eval()

# ---- Prompt template ----
def format_prompt(user_input):
    return f"""You are PythonGPT, an expert tutor that ONLY answers questions about Python programming.

If the user asks anything unrelated to Python (like greetings, jokes, math problems, or general trivia), respond strictly with:
"Sorry, I can only answer Python-related questions."

Examples:
Q: What is a function in Python?
A: In Python, a function is a block of reusable code that performs a specific task...

Q: Hello!
A: Sorry, I can only answer Python-related questions.

Q: What is numpy?
A: NumPy is a library in Python used for numerical computations...

Q: What's your name?
A: Sorry, I can only answer Python-related questions.

Q: {user_input}
A:"""

# ---- Chat generation ----
def get_response(user_input):
    prompt = format_prompt(user_input)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    outputs = model.generate(
        **inputs,
        max_new_tokens=200,
        do_sample=True,
        temperature=0.7,
        pad_token_id=tokenizer.eos_token_id
    )
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return response.split("A:")[-1].strip()

# ---- Streamlit App ----
st.set_page_config(page_title="πŸ§‘β€πŸ« Python Tutor Chatbot")
st.title("πŸ§‘β€πŸ« Python Tutor Chatbot")
st.write("Ask me anything about Python programming!")

user_query = st.text_input("Your Question", "")

if user_query:
    with st.spinner("Thinking..."):
        response = get_response(user_query)
        st.markdown(f"πŸ’‘ **Answer:**\n\n{response}")