Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,10 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 3 |
from peft import PeftModel
|
| 4 |
import streamlit as st
|
| 5 |
|
| 6 |
-
# Load tokenizer and model (CPU)
|
| 7 |
base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
-
adapter_path = "lora_adapter"
|
| 9 |
|
| 10 |
-
# Force CPU usage
|
| 11 |
device = torch.device("cpu")
|
| 12 |
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_fast=True)
|
|
@@ -15,20 +14,18 @@ base_model = AutoModelForCausalLM.from_pretrained(base_model_name).to(device)
|
|
| 15 |
model = PeftModel.from_pretrained(base_model, adapter_path).to(device)
|
| 16 |
|
| 17 |
# Streamlit UI setup
|
| 18 |
-
st.set_page_config(
|
| 19 |
-
page_title="Python Tutor Chatbot",
|
| 20 |
-
page_icon="π",
|
| 21 |
-
layout="centered"
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
st.title("π Python Tutor Chatbot")
|
| 25 |
st.markdown("Ask me anything about Python programming!")
|
| 26 |
|
| 27 |
-
# Prompt template
|
| 28 |
def create_prompt(user_input):
|
| 29 |
-
return f"""
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
### Instruction:
|
| 34 |
{user_input}
|
|
@@ -36,25 +33,27 @@ If the question is unrelated to Python, kindly respond with: "Sorry, I can only
|
|
| 36 |
### Response:
|
| 37 |
"""
|
| 38 |
|
| 39 |
-
#
|
| 40 |
user_input = st.text_input("Your Python Question:")
|
| 41 |
|
|
|
|
| 42 |
if user_input:
|
| 43 |
with st.spinner("Generating response..."):
|
| 44 |
prompt = create_prompt(user_input)
|
| 45 |
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 46 |
|
| 47 |
with torch.no_grad():
|
| 48 |
-
|
| 49 |
**inputs,
|
| 50 |
max_new_tokens=200,
|
| 51 |
temperature=0.7,
|
| 52 |
-
do_sample=True,
|
| 53 |
top_p=0.9,
|
| 54 |
-
top_k=50
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
-
response = tokenizer.decode(
|
| 58 |
final_response = response.split("### Response:")[-1].strip()
|
| 59 |
|
| 60 |
st.markdown("**Answer:**")
|
|
|
|
| 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 |
device = torch.device("cpu")
|
| 11 |
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_fast=True)
|
|
|
|
| 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 |
+
# π Prompt template with instruction to ignore unrelated queries
|
| 22 |
def create_prompt(user_input):
|
| 23 |
+
return f"""You are a helpful and expert AI Python tutor.
|
| 24 |
+
|
| 25 |
+
Your job is to only answer questions strictly related to Python programming (syntax, concepts, libraries, frameworks, tools, errors, etc.).
|
| 26 |
+
|
| 27 |
+
If the question is unrelated to Python, politely respond:
|
| 28 |
+
"Sorry, I can only answer Python programming questions."
|
| 29 |
|
| 30 |
### Instruction:
|
| 31 |
{user_input}
|
|
|
|
| 33 |
### Response:
|
| 34 |
"""
|
| 35 |
|
| 36 |
+
# π User Input
|
| 37 |
user_input = st.text_input("Your Python Question:")
|
| 38 |
|
| 39 |
+
# π Inference
|
| 40 |
if user_input:
|
| 41 |
with st.spinner("Generating response..."):
|
| 42 |
prompt = create_prompt(user_input)
|
| 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:**")
|