Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,57 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from peft import PeftModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
inputs = tokenizer(user_input, return_tensors="pt")
|
| 21 |
-
outputs = model.generate(**inputs, max_new_tokens=150)
|
| 22 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
-
st.write("**Answer:**", response)
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
from peft import PeftModel
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# Load tokenizer and base model
|
| 7 |
+
base_model_path = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_path)
|
| 9 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
base_model_path,
|
| 11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 12 |
+
device_map="auto" if torch.cuda.is_available() else None
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Load LoRA adapter
|
| 16 |
+
model = PeftModel.from_pretrained(base_model, "lora_adapter")
|
| 17 |
+
model.eval()
|
| 18 |
+
|
| 19 |
+
# Streamlit UI
|
| 20 |
+
st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
|
| 21 |
+
st.write("Ask me any **Python programming** question:")
|
| 22 |
+
|
| 23 |
+
user_input = st.text_input("Your question")
|
| 24 |
+
|
| 25 |
+
if user_input:
|
| 26 |
+
# Prompt template that helps model decide to answer or reject
|
| 27 |
+
prompt = f"""
|
| 28 |
+
You are a helpful and expert Python programming tutor.
|
| 29 |
+
Answer only questions related to Python programming.
|
| 30 |
+
If the question is unrelated to Python (like history, math, etc), politely respond:
|
| 31 |
+
"Sorry, I can only answer Python-related questions."
|
| 32 |
+
|
| 33 |
+
### Question:
|
| 34 |
+
{user_input}
|
| 35 |
|
| 36 |
+
### Answer:
|
| 37 |
+
"""
|
| 38 |
|
| 39 |
+
inputs = tokenizer(prompt, return_tensors="pt", return_attention_mask=True).to(model.device)
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
output = model.generate(
|
| 42 |
+
**inputs,
|
| 43 |
+
max_new_tokens=200,
|
| 44 |
+
temperature=0.7,
|
| 45 |
+
do_sample=True,
|
| 46 |
+
pad_token_id=tokenizer.eos_token_id
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 50 |
|
| 51 |
+
# Clean the output to only show the answer
|
| 52 |
+
if "### Answer:" in decoded:
|
| 53 |
+
final_answer = decoded.split("### Answer:")[-1].strip()
|
| 54 |
+
else:
|
| 55 |
+
final_answer = decoded.strip()
|
| 56 |
|
| 57 |
+
st.markdown(f"**Answer:** {final_answer}")
|
|
|
|
|
|
|
|
|
|
|
|