Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,27 @@
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import streamlit as st
|
| 4 |
-
from transformers import
|
| 5 |
-
AutoModelForCausalLM,
|
| 6 |
-
AutoTokenizer,
|
| 7 |
-
BitsAndBytesConfig
|
| 8 |
-
)
|
| 9 |
from peft import PeftModel
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
|
| 14 |
-
# Load base model
|
| 15 |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
|
|
|
| 16 |
|
| 17 |
-
bnb_config = BitsAndBytesConfig(
|
| 18 |
-
load_in_8bit=True,
|
| 19 |
-
llm_int8_threshold=6.0,
|
| 20 |
-
llm_int8_enable_fp32_cpu_offload=True
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 24 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
model_name,
|
| 26 |
-
|
| 27 |
-
device_map="
|
| 28 |
-
offload_folder="offload"
|
| 29 |
)
|
| 30 |
|
| 31 |
-
# Load LoRA adapter
|
| 32 |
-
model = PeftModel.from_pretrained(base_model, "lora_adapter")
|
| 33 |
-
|
| 34 |
-
# Evaluation mode
|
| 35 |
model.eval()
|
| 36 |
|
| 37 |
-
#
|
| 38 |
def format_prompt(instruction):
|
| 39 |
return f"""You are a helpful and expert Python programming tutor.
|
| 40 |
Only answer questions related to Python programming.
|
|
@@ -47,17 +34,17 @@ If the question is unrelated to Python, respond with:
|
|
| 47 |
### Response:
|
| 48 |
"""
|
| 49 |
|
| 50 |
-
#
|
| 51 |
def chat(instruction):
|
| 52 |
prompt = format_prompt(instruction)
|
| 53 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(
|
| 54 |
outputs = model.generate(
|
| 55 |
**inputs,
|
| 56 |
max_new_tokens=256,
|
| 57 |
do_sample=True,
|
| 58 |
temperature=0.7,
|
| 59 |
-
top_p=0.
|
| 60 |
-
repetition_penalty=1.
|
| 61 |
)
|
| 62 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
return response.split("### Response:")[-1].strip()
|
|
@@ -66,13 +53,13 @@ def chat(instruction):
|
|
| 66 |
st.title("🐍 Python Tutor Chatbot")
|
| 67 |
st.markdown("Ask me Python programming questions!")
|
| 68 |
|
| 69 |
-
|
| 70 |
|
| 71 |
if st.button("Answer"):
|
| 72 |
-
if
|
| 73 |
with st.spinner("Thinking..."):
|
| 74 |
-
|
| 75 |
st.markdown("**Answer:**")
|
| 76 |
-
st.write(
|
| 77 |
else:
|
| 78 |
st.warning("Please enter a question.")
|
|
|
|
| 1 |
import os
|
| 2 |
import torch
|
| 3 |
import streamlit as st
|
| 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 |
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
model_name,
|
| 16 |
+
torch_dtype=torch.float32, # You can try float16 if supported
|
| 17 |
+
device_map={"": device}
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Load LoRA adapter (your fine-tuned weights)
|
| 21 |
+
model = PeftModel.from_pretrained(base_model, "lora_adapter", device_map={"": device})
|
|
|
|
|
|
|
| 22 |
model.eval()
|
| 23 |
|
| 24 |
+
# Format the prompt with filtering
|
| 25 |
def format_prompt(instruction):
|
| 26 |
return f"""You are a helpful and expert Python programming tutor.
|
| 27 |
Only answer questions related to Python programming.
|
|
|
|
| 34 |
### Response:
|
| 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=True,
|
| 45 |
temperature=0.7,
|
| 46 |
+
top_p=0.9,
|
| 47 |
+
repetition_penalty=1.1
|
| 48 |
)
|
| 49 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
return response.split("### Response:")[-1].strip()
|
|
|
|
| 53 |
st.title("🐍 Python Tutor Chatbot")
|
| 54 |
st.markdown("Ask me Python programming questions!")
|
| 55 |
|
| 56 |
+
question = st.text_area("Your question:")
|
| 57 |
|
| 58 |
if st.button("Answer"):
|
| 59 |
+
if question.strip():
|
| 60 |
with st.spinner("Thinking..."):
|
| 61 |
+
response = chat(question)
|
| 62 |
st.markdown("**Answer:**")
|
| 63 |
+
st.write(response)
|
| 64 |
else:
|
| 65 |
st.warning("Please enter a question.")
|