Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,78 @@
|
|
| 1 |
-
import
|
| 2 |
import torch
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
Only answer questions related to Python programming.
|
| 15 |
If the question is unrelated to Python, respond with:
|
| 16 |
"Sorry, I can only answer Python-related questions."
|
| 17 |
|
| 18 |
-
Answer the following instruction with a structured and informative format.
|
| 19 |
### Instruction:
|
| 20 |
{instruction}
|
| 21 |
|
| 22 |
### Response:
|
| 23 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
| 34 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
return response.split("### Response:")[-1].strip()
|
| 36 |
|
| 37 |
# Streamlit UI
|
| 38 |
-
st.set_page_config(page_title="Python Tutor Chatbot", page_icon="🐍")
|
| 39 |
st.title("🐍 Python Tutor Chatbot")
|
| 40 |
-
st.
|
| 41 |
|
| 42 |
-
user_input = st.
|
| 43 |
|
| 44 |
-
if
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
st.markdown("**Answer:**")
|
| 48 |
-
st.
|
|
|
|
|
|
|
|
|
| 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 |
+
# Offload directory for CPU inference
|
| 12 |
+
os.makedirs("offload", exist_ok=True)
|
| 13 |
|
| 14 |
+
# Load base model + quantization config
|
| 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 |
+
quantization_config=bnb_config,
|
| 27 |
+
device_map="auto",
|
| 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 |
+
# Prompt template
|
| 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.
|
| 41 |
If the question is unrelated to Python, respond with:
|
| 42 |
"Sorry, I can only answer Python-related questions."
|
| 43 |
|
|
|
|
| 44 |
### Instruction:
|
| 45 |
{instruction}
|
| 46 |
|
| 47 |
### Response:
|
| 48 |
"""
|
| 49 |
+
|
| 50 |
+
# Chat function
|
| 51 |
+
def chat(instruction):
|
| 52 |
+
prompt = format_prompt(instruction)
|
| 53 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 54 |
+
outputs = model.generate(
|
| 55 |
+
**inputs,
|
| 56 |
+
max_new_tokens=256,
|
| 57 |
+
do_sample=True,
|
| 58 |
+
temperature=0.7,
|
| 59 |
+
top_p=0.95,
|
| 60 |
+
repetition_penalty=1.2
|
| 61 |
+
)
|
|
|
|
| 62 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 63 |
return response.split("### Response:")[-1].strip()
|
| 64 |
|
| 65 |
# Streamlit UI
|
|
|
|
| 66 |
st.title("🐍 Python Tutor Chatbot")
|
| 67 |
+
st.markdown("Ask me Python programming questions!")
|
| 68 |
|
| 69 |
+
user_input = st.text_area("Your question:")
|
| 70 |
|
| 71 |
+
if st.button("Answer"):
|
| 72 |
+
if user_input.strip():
|
| 73 |
+
with st.spinner("Thinking..."):
|
| 74 |
+
answer = chat(user_input)
|
| 75 |
st.markdown("**Answer:**")
|
| 76 |
+
st.write(answer)
|
| 77 |
+
else:
|
| 78 |
+
st.warning("Please enter a question.")
|