Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,61 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from peft import PeftModel
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
from transformers import pipeline
|
| 5 |
import streamlit as st
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
-
# Load tokenizer
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
)
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
st.write("Ask me any Python programming question:")
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
if user_input:
|
| 34 |
-
|
| 35 |
-
prompt =
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
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" # Your LoRA adapter folder path
|
| 9 |
|
| 10 |
+
# Force CPU usage
|
| 11 |
+
device = torch.device("cpu")
|
| 12 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_fast=True)
|
| 14 |
+
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 |
+
You are a helpful and knowledgeable AI Python Tutor. Your job is to answer only Python-related programming questions.
|
| 31 |
+
If the question is unrelated to Python, kindly respond with: "Sorry, I can only answer Python programming questions."
|
| 32 |
+
|
| 33 |
+
### Instruction:
|
| 34 |
+
{user_input}
|
| 35 |
|
| 36 |
+
### Response:
|
| 37 |
+
"""
|
|
|
|
| 38 |
|
| 39 |
+
# Chat interface
|
| 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 |
+
output = model.generate(
|
| 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(output[0], skip_special_tokens=True)
|
| 58 |
+
final_response = response.split("### Response:")[-1].strip()
|
| 59 |
+
|
| 60 |
+
st.markdown("**Answer:**")
|
| 61 |
+
st.write(final_response)
|