Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,46 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
from peft import PeftModel
|
| 4 |
-
import streamlit as st
|
| 5 |
-
|
| 6 |
-
# Load tokenizer and model (on CPU)
|
| 7 |
-
base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
-
adapter_path = "lora_adapter"
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
-
|
| 14 |
-
model = PeftModel.from_pretrained(base_model, adapter_path).to(device)
|
| 15 |
-
|
| 16 |
-
# Streamlit UI setup
|
| 17 |
-
st.set_page_config(page_title="Python Tutor Chatbot", page_icon="π", layout="centered")
|
| 18 |
-
st.title("π Python Tutor Chatbot")
|
| 19 |
-
st.markdown("Ask me anything about Python programming!")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
If the question is unrelated to Python, politely respond:
|
| 28 |
-
"Sorry, I can only answer Python programming questions."
|
| 29 |
|
| 30 |
### Instruction:
|
| 31 |
-
{
|
| 32 |
|
| 33 |
### Response:
|
| 34 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
user_input = st.text_input("Your Python Question:")
|
| 38 |
|
| 39 |
-
# π Inference
|
| 40 |
if user_input:
|
| 41 |
with st.spinner("Generating response..."):
|
| 42 |
-
|
| 43 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 44 |
-
|
| 45 |
-
with torch.no_grad():
|
| 46 |
-
outputs = model.generate(
|
| 47 |
-
**inputs,
|
| 48 |
-
max_new_tokens=200,
|
| 49 |
-
temperature=0.7,
|
| 50 |
-
top_p=0.9,
|
| 51 |
-
top_k=50,
|
| 52 |
-
do_sample=True,
|
| 53 |
-
pad_token_id=tokenizer.eos_token_id
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 57 |
-
final_response = response.split("### Response:")[-1].strip()
|
| 58 |
-
|
| 59 |
st.markdown("**Answer:**")
|
| 60 |
-
st.
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load model and tokenizer
|
| 6 |
+
model_name = "path/to/your/lora_adapter_or_model" # Update this to your LoRA model path
|
| 7 |
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Chat function with prompt-based filtering
|
| 12 |
+
def chat(instruction):
|
| 13 |
+
prompt = f"""You are a helpful and expert Python programming tutor.
|
| 14 |
+
If the question is about Python, explain clearly with examples.
|
| 15 |
+
If the question is unrelated to Python, respond with "Sorry, I can only answer Python-related questions."
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
### Instruction:
|
| 18 |
+
{instruction}
|
| 19 |
|
| 20 |
### Response:
|
| 21 |
"""
|
| 22 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(
|
| 25 |
+
**inputs,
|
| 26 |
+
max_new_tokens=150,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
top_p=0.95,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
pad_token_id=tokenizer.eos_token_id
|
| 31 |
+
)
|
| 32 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 33 |
+
return response.split("### Response:")[-1].strip()
|
| 34 |
+
|
| 35 |
+
# Streamlit UI
|
| 36 |
+
st.set_page_config(page_title="Python Tutor Chatbot", page_icon="π")
|
| 37 |
+
st.title("π Python Tutor Chatbot")
|
| 38 |
+
st.write("Ask me Python programming questions!")
|
| 39 |
|
| 40 |
+
user_input = st.text_input("Your question:")
|
|
|
|
| 41 |
|
|
|
|
| 42 |
if user_input:
|
| 43 |
with st.spinner("Generating response..."):
|
| 44 |
+
response = chat(user_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
st.markdown("**Answer:**")
|
| 46 |
+
st.markdown(response)
|