Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,22 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Load base model & tokenizer
|
| 7 |
-
base_model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 8 |
-
adapter_path = "lora_adapter" # path to your LoRA adapter directory
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
base_model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map="auto")
|
| 14 |
-
model = PeftModel.from_pretrained(base_model, adapter_path)
|
| 15 |
-
model.eval()
|
| 16 |
-
return tokenizer, model
|
| 17 |
|
| 18 |
-
tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
# Prompt
|
| 21 |
def format_prompt(user_input):
|
| 22 |
-
return def format_prompt(user_input):
|
| 23 |
return f"""You are PythonGPT, an expert tutor that ONLY answers questions about Python programming.
|
| 24 |
|
| 25 |
If the user asks anything unrelated to Python (like greetings, jokes, math problems, or general trivia), respond strictly with:
|
|
@@ -41,24 +38,28 @@ A: Sorry, I can only answer Python-related questions.
|
|
| 41 |
Q: {user_input}
|
| 42 |
A:"""
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
prompt = format_prompt(instruction)
|
| 48 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return response.split("A:")[-1].strip()
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
st.title("π§βπ« Python Tutor Chatbot")
|
| 56 |
st.write("Ask me anything about Python programming!")
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.markdown("
|
| 64 |
-
st.write(answer)
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Load base + LoRA model
|
| 6 |
+
base_model = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 7 |
+
adapter_path = "./lora_adapter" # your uploaded LoRA adapter
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
base_model,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
torch_dtype=torch.float32
|
| 14 |
+
)
|
| 15 |
+
model.load_adapter(adapter_path)
|
| 16 |
+
model.eval()
|
| 17 |
|
| 18 |
+
# ---- Prompt template ----
|
| 19 |
def format_prompt(user_input):
|
|
|
|
| 20 |
return f"""You are PythonGPT, an expert tutor that ONLY answers questions about Python programming.
|
| 21 |
|
| 22 |
If the user asks anything unrelated to Python (like greetings, jokes, math problems, or general trivia), respond strictly with:
|
|
|
|
| 38 |
Q: {user_input}
|
| 39 |
A:"""
|
| 40 |
|
| 41 |
+
# ---- Chat generation ----
|
| 42 |
+
def get_response(user_input):
|
| 43 |
+
prompt = format_prompt(user_input)
|
|
|
|
| 44 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 45 |
+
outputs = model.generate(
|
| 46 |
+
**inputs,
|
| 47 |
+
max_new_tokens=200,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
pad_token_id=tokenizer.eos_token_id
|
| 51 |
+
)
|
| 52 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
return response.split("A:")[-1].strip()
|
| 54 |
|
| 55 |
+
# ---- Streamlit App ----
|
| 56 |
+
st.set_page_config(page_title="π§βπ« Python Tutor Chatbot")
|
| 57 |
st.title("π§βπ« Python Tutor Chatbot")
|
| 58 |
st.write("Ask me anything about Python programming!")
|
| 59 |
|
| 60 |
+
user_query = st.text_input("Your Question", "")
|
| 61 |
+
|
| 62 |
+
if user_query:
|
| 63 |
+
with st.spinner("Thinking..."):
|
| 64 |
+
response = get_response(user_query)
|
| 65 |
+
st.markdown(f"π‘ **Answer:**\n\n{response}")
|
|
|