Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,54 +3,53 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 3 |
from peft import PeftModel
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
|
| 7 |
-
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
-
lora_path = "./lora_adapter"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Load base model normally (for CPU)
|
| 13 |
-
model = AutoModelForCausalLM.from_pretrained(base_model)
|
| 14 |
-
model = PeftModel.from_pretrained(model, lora_path)
|
| 15 |
-
model.eval()
|
| 16 |
-
|
| 17 |
-
# Move to CPU explicitly
|
| 18 |
-
device = torch.device("cpu")
|
| 19 |
-
model.to(device)
|
| 20 |
-
|
| 21 |
-
# Streamlit UI
|
| 22 |
-
st.set_page_config(page_title="🧠 TinyLLaMA Python Tutor (LoRA)")
|
| 23 |
st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
|
| 24 |
-
st.write("Ask me any
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
with st.spinner("Thinking..."):
|
| 44 |
-
outputs = model.generate(
|
| 45 |
-
**inputs,
|
| 46 |
-
max_new_tokens=150,
|
| 47 |
-
temperature=0.7,
|
| 48 |
-
top_p=0.95,
|
| 49 |
-
do_sample=True,
|
| 50 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 51 |
-
pad_token_id=tokenizer.eos_token_id
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 55 |
-
answer = decoded_output.split("<|assistant|>")[-1].strip()
|
| 56 |
-
st.success(f"💬 Answer:\n\n{answer}")
|
|
|
|
| 3 |
from peft import PeftModel
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="TinyLLaMA Python Tutor", layout="centered")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
|
| 10 |
+
st.write("Ask me any Python programming question:")
|
| 11 |
+
|
| 12 |
+
# Load base model and LoRA adapter
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model():
|
| 15 |
+
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 16 |
+
adapter_path = "lora_adapter"
|
| 17 |
+
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float32)
|
| 20 |
+
model = PeftModel.from_pretrained(model, adapter_path)
|
| 21 |
+
model.eval()
|
| 22 |
+
|
| 23 |
+
return tokenizer, model
|
| 24 |
+
|
| 25 |
+
tokenizer, model = load_model()
|
| 26 |
+
|
| 27 |
+
# Prompt template
|
| 28 |
+
def build_prompt(question):
|
| 29 |
+
return (
|
| 30 |
+
"You are a helpful AI tutor that only answers Python programming questions. "
|
| 31 |
+
"If the user asks something unrelated to Python, respond with: "
|
| 32 |
+
"'Sorry, I can only answer Python-related questions.'\n\n"
|
| 33 |
+
f"Question: {question}\nAnswer:"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# Input box
|
| 37 |
+
question = st.text_input("Your question")
|
| 38 |
+
|
| 39 |
+
if question:
|
| 40 |
+
prompt = build_prompt(question)
|
| 41 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 42 |
+
|
| 43 |
+
with st.spinner("Thinking..."):
|
| 44 |
+
outputs = model.generate(
|
| 45 |
+
**inputs,
|
| 46 |
+
max_new_tokens=300, # 🔼 Increased from 200 to 300
|
| 47 |
+
temperature=0.6, # 🔽 More deterministic
|
| 48 |
+
top_p=0.9,
|
| 49 |
+
pad_token_id=tokenizer.eos_token_id
|
| 50 |
)
|
| 51 |
+
|
| 52 |
+
decoded_output = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
+
answer = decoded_output.split("Answer:")[-1].strip()
|
| 54 |
+
|
| 55 |
+
st.markdown(f"**💬 Answer:**\n\n{answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|