Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,58 @@
|
|
| 1 |
-
import os
|
| 2 |
import torch
|
| 3 |
-
|
| 4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
from peft import PeftModel
|
| 6 |
-
|
| 7 |
-
# Set CPU device
|
| 8 |
-
device = torch.device("cpu")
|
| 9 |
|
| 10 |
# Load base model and tokenizer
|
| 11 |
-
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
device_map=
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# Load LoRA adapter
|
| 21 |
-
model = PeftModel.from_pretrained(
|
| 22 |
model.eval()
|
| 23 |
|
| 24 |
-
# Format the prompt with filtering
|
| 25 |
def format_prompt(instruction):
|
| 26 |
-
return f"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
###
|
| 32 |
{instruction}
|
| 33 |
|
| 34 |
-
###
|
| 35 |
"""
|
| 36 |
|
| 37 |
-
# Generate answer
|
| 38 |
def chat(instruction):
|
| 39 |
prompt = format_prompt(instruction)
|
| 40 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 41 |
outputs = model.generate(
|
| 42 |
**inputs,
|
| 43 |
max_new_tokens=256,
|
| 44 |
-
do_sample=
|
| 45 |
-
temperature=0.
|
| 46 |
-
top_p=
|
| 47 |
repetition_penalty=1.1
|
| 48 |
)
|
| 49 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
-
return response.split("###
|
| 51 |
|
| 52 |
# Streamlit UI
|
|
|
|
| 53 |
st.title("🐍 Python Tutor Chatbot")
|
| 54 |
-
st.
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
if st.button("Answer"):
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
response = chat(question)
|
| 62 |
st.markdown("**Answer:**")
|
| 63 |
st.write(response)
|
| 64 |
-
else:
|
| 65 |
-
st.warning("Please enter a question.")
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 3 |
from peft import PeftModel
|
| 4 |
+
import streamlit as st
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load base model and tokenizer
|
| 7 |
+
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 9 |
|
| 10 |
+
# Load base model in CPU-only mode
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
base_model,
|
| 13 |
+
device_map="cpu"
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# Load the LoRA adapter
|
| 17 |
+
model = PeftModel.from_pretrained(model, "lora_adapter")
|
| 18 |
model.eval()
|
| 19 |
|
|
|
|
| 20 |
def format_prompt(instruction):
|
| 21 |
+
return f"""### SYSTEM:
|
| 22 |
+
You are a helpful and expert Python programming tutor.
|
| 23 |
+
You only answer questions related to Python programming.
|
| 24 |
+
If the question is unrelated to Python, say:
|
| 25 |
+
\"Sorry, I can only answer Python-related questions.\"
|
| 26 |
|
| 27 |
+
### USER:
|
| 28 |
{instruction}
|
| 29 |
|
| 30 |
+
### ASSISTANT:
|
| 31 |
"""
|
| 32 |
|
|
|
|
| 33 |
def chat(instruction):
|
| 34 |
prompt = format_prompt(instruction)
|
| 35 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 36 |
outputs = model.generate(
|
| 37 |
**inputs,
|
| 38 |
max_new_tokens=256,
|
| 39 |
+
do_sample=False,
|
| 40 |
+
temperature=0.0,
|
| 41 |
+
top_p=1.0,
|
| 42 |
repetition_penalty=1.1
|
| 43 |
)
|
| 44 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
return response.split("### ASSISTANT:")[-1].strip()
|
| 46 |
|
| 47 |
# Streamlit UI
|
| 48 |
+
st.set_page_config(page_title="🐍 Python Tutor Chatbot")
|
| 49 |
st.title("🐍 Python Tutor Chatbot")
|
| 50 |
+
st.write("Ask me Python programming questions!")
|
| 51 |
|
| 52 |
+
user_input = st.text_area("Your question:")
|
| 53 |
|
| 54 |
+
if st.button("Get Answer") and user_input.strip():
|
| 55 |
+
with st.spinner("Thinking..."):
|
| 56 |
+
response = chat(user_input)
|
|
|
|
| 57 |
st.markdown("**Answer:**")
|
| 58 |
st.write(response)
|
|
|
|
|
|