Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,24 @@
|
|
| 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 |
-
|
| 7 |
-
# Set up offload directory for CPU offloading
|
| 8 |
-
offload_dir = "./offload"
|
| 9 |
|
| 10 |
# Load tokenizer
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 12 |
|
| 13 |
-
# Load base model
|
| 14 |
-
bnb_config = BitsAndBytesConfig(load_in_4bit=True)
|
| 15 |
-
|
| 16 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
offload_folder=offload_dir # this is the key line
|
| 21 |
)
|
| 22 |
|
| 23 |
-
# Load
|
| 24 |
model = PeftModel.from_pretrained(base_model, "lora_adapter", device_map="auto")
|
| 25 |
|
| 26 |
-
#
|
| 27 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 28 |
|
| 29 |
# Streamlit UI
|
|
@@ -33,16 +28,13 @@ st.write("Ask me any Python programming question:")
|
|
| 33 |
user_input = st.text_input("Your question")
|
| 34 |
|
| 35 |
if user_input:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
prompt = f"""You are a helpful Python tutor. Answer only Python programming questions.
|
| 39 |
-
Respond clearly with examples. Avoid repeating the question.
|
| 40 |
|
| 41 |
Question: {user_input}
|
| 42 |
Answer:"""
|
| 43 |
-
|
| 44 |
-
response = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 45 |
answer = response.split("Answer:")[-1].strip()
|
| 46 |
st.markdown(f"💬 **Answer:**\n\n{answer}")
|
| 47 |
else:
|
| 48 |
-
st.
|
|
|
|
| 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 |
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
|
| 10 |
|
| 11 |
+
# Load base model
|
|
|
|
|
|
|
| 12 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 14 |
+
device_map="auto",
|
| 15 |
+
torch_dtype=torch.float32
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# Load LoRA adapter
|
| 19 |
model = PeftModel.from_pretrained(base_model, "lora_adapter", device_map="auto")
|
| 20 |
|
| 21 |
+
# Load pipeline
|
| 22 |
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 23 |
|
| 24 |
# Streamlit UI
|
|
|
|
| 28 |
user_input = st.text_input("Your question")
|
| 29 |
|
| 30 |
if user_input:
|
| 31 |
+
if "python" in user_input.lower() or "list" in user_input.lower() or "tuple" in user_input.lower() or "def " in user_input.lower() or "class" in user_input.lower():
|
| 32 |
+
prompt = f"""You are a helpful and friendly Python tutor. Only answer Python programming questions. Be clear and concise.
|
|
|
|
|
|
|
| 33 |
|
| 34 |
Question: {user_input}
|
| 35 |
Answer:"""
|
| 36 |
+
response = pipe(prompt, max_new_tokens=256, temperature=0.7, do_sample=True)[0]["generated_text"]
|
|
|
|
| 37 |
answer = response.split("Answer:")[-1].strip()
|
| 38 |
st.markdown(f"💬 **Answer:**\n\n{answer}")
|
| 39 |
else:
|
| 40 |
+
st.warning("❌ Sorry, I can only answer Python programming questions.")
|