Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,55 @@
|
|
| 1 |
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
from peft import PeftModel
|
|
|
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 14 |
-
adapter_path = "lora_adapter"
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
model.eval()
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
f"Question: {question}\nAnswer:"
|
| 31 |
-
)
|
| 32 |
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
prompt = build_prompt(question)
|
| 37 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
outputs = model.generate(
|
| 41 |
**inputs,
|
| 42 |
-
max_new_tokens=
|
| 43 |
-
|
| 44 |
-
top_p=0.
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from peft import PeftModel
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
+
# Load tokenizer
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("TinyLLaMA/TinyLLaMA-1.1B-Chat-v1.0")
|
| 8 |
|
| 9 |
+
# Load base model
|
| 10 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
"TinyLLaMA/TinyLLaMA-1.1B-Chat-v1.0",
|
| 12 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 13 |
+
device_map="auto"
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
# Load LoRA adapter
|
| 17 |
+
model = PeftModel.from_pretrained(base_model, "lora_adapter")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Set title
|
| 20 |
+
st.title("🧠 TinyLLaMA Python Tutor (LoRA)")
|
| 21 |
+
st.markdown("Ask me any **Python programming** question:")
|
|
|
|
| 22 |
|
| 23 |
+
# User input
|
| 24 |
+
user_question = st.text_input("Your question")
|
| 25 |
|
| 26 |
+
if user_question:
|
| 27 |
+
with st.spinner("Thinking..."):
|
| 28 |
|
| 29 |
+
# Clean prompt
|
| 30 |
+
prompt = f"""
|
| 31 |
+
You are a helpful and expert Python programming tutor.
|
| 32 |
+
If the question is about Python, explain clearly with examples.
|
| 33 |
+
If the question is unrelated to Python, respond with "Sorry, I can only answer Python-related questions."
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
Question: {user_question}
|
| 36 |
+
Answer:"""
|
| 37 |
|
| 38 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
output = model.generate(
|
|
|
|
| 41 |
**inputs,
|
| 42 |
+
max_new_tokens=256,
|
| 43 |
+
do_sample=True,
|
| 44 |
+
top_p=0.9,
|
| 45 |
+
temperature=0.7,
|
| 46 |
+
repetition_penalty=1.1
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 50 |
+
|
| 51 |
+
# Extract only the generated answer after "Answer:"
|
| 52 |
+
answer_start = decoded_output.find("Answer:")
|
| 53 |
+
answer = decoded_output[answer_start + len("Answer:"):].strip() if answer_start != -1 else decoded_output.strip()
|
| 54 |
|
| 55 |
+
st.markdown(f"💬 **Answer:**\n\n{answer}")
|