Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,64 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
from peft import PeftModel
|
| 4 |
-
import streamlit as st
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
def format_prompt(instruction):
|
| 16 |
-
return f"""You are a helpful and expert Python programming tutor.
|
| 17 |
-
You only answer questions related to Python programming.
|
| 18 |
-
If the question is unrelated to Python, say:
|
| 19 |
"Sorry, I can only answer Python-related questions."
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
def chat(instruction):
|
| 26 |
-
prompt = format_prompt(instruction)
|
| 27 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with torch.no_grad():
|
| 30 |
-
|
| 31 |
**inputs,
|
| 32 |
-
max_new_tokens=
|
| 33 |
-
do_sample=
|
| 34 |
temperature=0.7,
|
| 35 |
top_p=0.9,
|
| 36 |
-
|
| 37 |
)
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Only return the model's answer
|
| 42 |
-
return full_output.split("Answer:")[-1].strip()
|
| 43 |
|
| 44 |
# Streamlit UI
|
| 45 |
-
st.
|
| 46 |
-
st.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
st.
|
| 55 |
-
if "```" in response:
|
| 56 |
-
st.markdown(response)
|
| 57 |
-
else:
|
| 58 |
-
st.write(response)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from peft import PeftModel
|
|
|
|
| 5 |
|
| 6 |
+
# Load base model & tokenizer
|
| 7 |
+
base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
+
adapter_path = "lora_adapter" # path to your LoRA adapter directory
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
| 13 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map="auto")
|
| 14 |
+
model = PeftModel.from_pretrained(base_model, adapter_path)
|
| 15 |
+
model.eval()
|
| 16 |
+
return tokenizer, model
|
| 17 |
+
|
| 18 |
+
tokenizer, model = load_model()
|
| 19 |
+
|
| 20 |
+
# Prompt formatting
|
| 21 |
+
def format_prompt(user_input):
|
| 22 |
+
return f"""You are a helpful and knowledgeable Python tutor chatbot.
|
| 23 |
|
| 24 |
+
You only answer questions related to Python programming, including:
|
| 25 |
+
- Python syntax, functions, loops, and conditionals
|
| 26 |
+
- Standard libraries and popular packages (e.g., NumPy, pandas)
|
| 27 |
+
- Debugging and code explanation
|
| 28 |
+
- Python tools, environments, and tips
|
| 29 |
|
| 30 |
+
If a question is not related to Python, reply with:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
"Sorry, I can only answer Python-related questions."
|
| 32 |
|
| 33 |
+
### Instruction:
|
| 34 |
+
{user_input}
|
| 35 |
|
| 36 |
+
### Response:"""
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Chat handler
|
| 39 |
+
def chat(user_input):
|
| 40 |
+
prompt = format_prompt(user_input)
|
| 41 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 42 |
with torch.no_grad():
|
| 43 |
+
output = model.generate(
|
| 44 |
**inputs,
|
| 45 |
+
max_new_tokens=200,
|
| 46 |
+
do_sample=True,
|
| 47 |
temperature=0.7,
|
| 48 |
top_p=0.9,
|
| 49 |
+
pad_token_id=tokenizer.eos_token_id
|
| 50 |
)
|
| 51 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 52 |
+
return decoded.split("### Response:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
# Streamlit UI
|
| 55 |
+
st.title("🧑🏫 Python Tutor Chatbot")
|
| 56 |
+
st.write("Ask me anything about Python programming!")
|
| 57 |
+
|
| 58 |
+
user_input = st.text_area("Your Question", height=150)
|
| 59 |
+
if st.button("Ask"):
|
| 60 |
+
if user_input.strip():
|
| 61 |
+
with st.spinner("Thinking..."):
|
| 62 |
+
answer = chat(user_input)
|
| 63 |
+
st.markdown("### 💡 Answer:")
|
| 64 |
+
st.write(answer)
|
|
|
|
|
|
|
|
|
|
|
|