krisha06 commited on
Commit
e667312
Β·
verified Β·
1 Parent(s): f566850

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -20
app.py CHANGED
@@ -1,28 +1,81 @@
1
- import streamlit as st
2
  import torch
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Load fine-tuned model
6
- model_path = "tinyllama-lora-finetuned"
7
- st.write("Loading fine-tuned TinyLlama... (CPU Mode)")
 
 
 
 
 
8
 
9
- model = AutoModelForCausalLM.from_pretrained(model_path, device_map="cpu")
10
- tokenizer = AutoTokenizer.from_pretrained(model_path)
 
 
 
 
 
 
11
 
12
- st.title("Fine-Tuned TinyLlama Chatbot (LoRA)")
13
- st.write("πŸš€ Chatbot trained with LoRA on CPU.")
14
 
15
- user_input = st.text_area("Enter your prompt:", "")
 
16
 
17
- if st.button("Generate Response"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  if user_input:
19
- with st.spinner("Generating response..."):
20
- inputs = tokenizer(user_input, return_tensors="pt").to("cpu")
21
- output = model.generate(**inputs, max_length=100)
22
- response = tokenizer.decode(output[0], skip_special_tokens=True)
23
- st.write("**Response:**")
24
- st.write(response)
25
  else:
26
- st.warning("Please enter a prompt!")
27
-
28
- st.write("βœ… Running on CPU - May be slow.")
 
 
1
  import torch
2
+ import streamlit as st
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer
4
+ from peft import LoraConfig, get_peft_model, TaskType
5
+ from huggingface_hub import HfApi, Repository
6
+ from datasets import load_dataset
7
+
8
+ # Hugging Face details
9
+ HF_TOKEN = "your_huggingface_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.")