jonACE commited on
Commit
71c0be4
·
verified ·
1 Parent(s): d352b02

Create app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +114 -0
app-backup.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fitz # PyMuPDF for PDF extraction
2
+ import re
3
+ import unsloth
4
+ import os
5
+ from huggingface_hub import login
6
+ from datasets import Dataset
7
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
8
+ from peft import LoraConfig, get_peft_model
9
+ import gradio as gr
10
+ from transformers import pipeline
11
+
12
+
13
+ def extract_text_from_pdf(pdf_path):
14
+ """Extract text from a PDF file"""
15
+ doc = fitz.open(pdf_path)
16
+ text = "\n".join([page.get_text("text") for page in doc])
17
+ return text.strip()
18
+
19
+ def preprocess_text(text):
20
+ """Basic text preprocessing"""
21
+ return re.sub(r"\s+", " ", text).strip()
22
+
23
+ pdf_text = extract_text_from_pdf("new-american-standard-bible.pdf")
24
+ clean_text = preprocess_text(pdf_text)
25
+
26
+
27
+ # Read the Hugging Face token from environment variables
28
+ hf_token = os.getenv("access_token")
29
+
30
+ if hf_token is None:
31
+ raise ValueError("'access_token' is not set. Add it as a secret variable in Hugging Face Spaces.")
32
+
33
+ # Log in to Hugging Face
34
+ login(token=hf_token)
35
+
36
+ #model_name = "meta-llama/Llama-2-7b-hf" # You can use a smaller one like "meta-llama/Llama-2-7b-chat-hf"
37
+ model_name = "unsloth/llama-2-7b-chat"
38
+
39
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
40
+
41
+ # Create dataset
42
+ data = {"text": [clean_text]}
43
+ dataset = Dataset.from_dict(data)
44
+
45
+ # Set a padding token manually
46
+ tokenizer.pad_token = tokenizer.eos_token # Use EOS as PAD token
47
+ # Alternatively, add a new custom pad token
48
+ # tokenizer.add_special_tokens({'pad_token': '[PAD]'})
49
+
50
+ # Tokenization function
51
+ def tokenize_function(examples):
52
+ tokens = tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
53
+ tokens["labels"] = tokens["input_ids"].copy() # Use input as labels for text generation
54
+ return tokens
55
+
56
+ tokenized_datasets = dataset.map(tokenize_function, batched=True)
57
+
58
+ # Load LLaMA 2 model in 4-bit mode to save memory
59
+ model = AutoModelForCausalLM.from_pretrained(
60
+ model_name,
61
+ load_in_4bit=True, # Use 4-bit quantization for efficiency
62
+ device_map="auto"
63
+ #device_map="cpu",
64
+ #quantization_config=None
65
+ )
66
+
67
+ # Apply LoRA (efficient fine-tuning)
68
+ lora_config = LoraConfig(
69
+ r=8, # Low-rank parameter
70
+ lora_alpha=32,
71
+ target_modules=["q_proj", "v_proj"], # Applies only to attention layers
72
+ lora_dropout=0.05
73
+ )
74
+
75
+ model = get_peft_model(model, lora_config)
76
+
77
+ training_args = TrainingArguments(
78
+ output_dir="./results",
79
+ evaluation_strategy="no", # Disable evaluation (to enable, change value to 'epoch')
80
+ learning_rate=2e-4,
81
+ per_device_train_batch_size=1, # Reduce batch size for memory efficiency
82
+ per_device_eval_batch_size=1,
83
+ num_train_epochs=3,
84
+ weight_decay=0.01,
85
+ save_strategy="epoch",
86
+ logging_dir="./logs",
87
+ logging_steps=10,
88
+ )
89
+
90
+ trainer = Trainer(
91
+ model=model,
92
+ args=training_args,
93
+ train_dataset=tokenized_datasets,
94
+ tokenizer=tokenizer,
95
+ )
96
+
97
+ def perform_training():
98
+ trainer.train()
99
+
100
+ perform_training()
101
+
102
+ model.save_pretrained("./fine_tuned_llama2")
103
+ tokenizer.save_pretrained("./fine_tuned_llama2")
104
+
105
+
106
+ # CHATBOT START
107
+ chatbot = pipeline("text-generation", model="./fine_tuned_llama2")
108
+
109
+ def chatbot_response(prompt):
110
+ result = chatbot(prompt, max_length=100, do_sample=True, temperature=0.7)
111
+ return result[0]["generated_text"]
112
+
113
+ iface = gr.Interface(fn=chatbot_response, inputs="text", outputs="text")
114
+ iface.launch()