Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,81 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
-
import streamlit as st
|
| 4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
|
| 5 |
-
from peft import LoraConfig, get_peft_model, TaskType
|
| 6 |
-
from huggingface_hub import HfApi, Repository
|
| 7 |
-
from datasets import load_dataset
|
| 8 |
-
|
| 9 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
-
REPO_NAME = "tinyllama-lora-finetuned"
|
| 11 |
-
|
| 12 |
-
# Initialize Streamlit
|
| 13 |
-
st.title("π§βπ« Python Tutor AI (Fine-tuned with LoRA)")
|
| 14 |
-
|
| 15 |
-
# Create HF repo if it doesn't exist
|
| 16 |
-
api = HfApi()
|
| 17 |
-
api.create_repo(REPO_NAME, token=HF_TOKEN, repo_type="model", exist_ok=True)
|
| 18 |
-
repo = Repository(local_dir=REPO_NAME, clone_from=f"hf://{REPO_NAME}", use_auth_token=HF_TOKEN)
|
| 19 |
-
|
| 20 |
-
# Load TinyLlama Model & Tokenizer
|
| 21 |
-
MODEL_NAME = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 22 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 23 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
-
MODEL_NAME,
|
| 25 |
-
torch_dtype=torch.float16,
|
| 26 |
-
device_map="auto"
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
# LoRA Configuration
|
| 30 |
-
lora_config = LoraConfig(
|
| 31 |
-
task_type=TaskType.CAUSAL_LM,
|
| 32 |
-
inference_mode=False,
|
| 33 |
-
r=8,
|
| 34 |
-
lora_alpha=32,
|
| 35 |
-
lora_dropout=0.1
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
# Apply LoRA to Model
|
| 39 |
-
model = get_peft_model(model, lora_config)
|
| 40 |
-
|
| 41 |
-
# Load dataset for fine-tuning
|
| 42 |
-
dataset = load_dataset("Abirate/english_python_code_instructions", split="train[:2%]")
|
| 43 |
-
|
| 44 |
-
# Fine-Tuning Parameters
|
| 45 |
-
training_args = TrainingArguments(
|
| 46 |
-
output_dir="./results",
|
| 47 |
-
per_device_train_batch_size=1,
|
| 48 |
-
gradient_accumulation_steps=4,
|
| 49 |
-
optim="adamw_torch",
|
| 50 |
-
num_train_epochs=1,
|
| 51 |
-
logging_steps=10,
|
| 52 |
-
save_strategy="no"
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
# Trainer
|
| 56 |
-
trainer = Trainer(
|
| 57 |
-
model=model,
|
| 58 |
-
args=training_args,
|
| 59 |
-
train_dataset=dataset
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
# Fine-tune Model
|
| 63 |
-
st.write("π― Fine-tuning Model (LoRA)...")
|
| 64 |
-
trainer.train()
|
| 65 |
-
st.success("β
Fine-tuning complete!")
|
| 66 |
-
|
| 67 |
-
# Push model to Hugging Face
|
| 68 |
-
model.push_to_hub(REPO_NAME, use_auth_token=HF_TOKEN)
|
| 69 |
-
tokenizer.push_to_hub(REPO_NAME, use_auth_token=HF_TOKEN)
|
| 70 |
-
st.success("π Model pushed to Hugging Face!")
|
| 71 |
-
|
| 72 |
-
# User Input for Python Tutoring
|
| 73 |
-
user_input = st.text_area("π Ask me a Python question:")
|
| 74 |
-
if st.button("Get Answer"):
|
| 75 |
-
if user_input:
|
| 76 |
-
inputs = tokenizer(user_input, return_tensors="pt").to("cuda")
|
| 77 |
-
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 78 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 79 |
-
st.write("π‘ AI Tutor:", response)
|
| 80 |
-
else:
|
| 81 |
-
st.warning("β οΈ Please enter a question.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|