Faizal2805 commited on
Commit
c0c2682
·
verified ·
1 Parent(s): e702117

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -50
app.py CHANGED
@@ -1,63 +1,54 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
4
- from datasets import Dataset, load_dataset
5
  from peft import LoraConfig, get_peft_model
6
  import torch
7
 
8
  # Initialize Hugging Face Inference Client
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
- # Load GPT-2 model and tokenizer
12
  model_name = "gpt2"
13
  tokenizer = AutoTokenizer.from_pretrained(model_name)
14
  model = AutoModelForCausalLM.from_pretrained(model_name)
15
 
16
- # Add padding token (GPT-2 fix)
17
- if tokenizer.pad_token is None:
18
- tokenizer.pad_token = tokenizer.eos_token
19
 
20
- # Custom Dataset (Predefined Q&A Pairs for Project Expo)
21
  custom_data = [
22
- {"text": "Who are you?", "label": "I am Eva, a virtual voice assistant."},
23
- {"text": "What is your name?", "label": "I am Eva, how can I help you?"},
24
- {"text": "What can you do?", "label": "I can assist with answering questions, searching the web, and much more!"},
25
- {"text": "Who invented the computer?", "label": "Charles Babbage is known as the father of the computer."},
26
- {"text": "Tell me a joke.", "label": "Why don’t scientists trust atoms? Because they make up everything!"},
27
- {"text": "Who is the Prime Minister of India?", "label": "The current Prime Minister of India is Narendra Modi."},
28
- {"text": "Who created you?", "label": "I was created by an expert team specializing in AI fine-tuning and web development."}
29
  ]
30
 
31
  # Convert custom dataset to Hugging Face Dataset
32
- dataset_custom = Dataset.from_dict({
33
- "text": [d['text'] for d in custom_data],
34
- "label": [d['label'] for d in custom_data]
35
- })
36
 
37
- # Load OpenWebText dataset (5% portion to avoid streaming issues)
38
- dataset = load_dataset("Skylion007/openwebtext", split="train[:20%]")
39
 
40
  # Tokenization function
41
  def tokenize_function(examples):
42
- return tokenizer(
43
- examples["text"],
44
- truncation=True,
45
- padding="max_length",
46
- max_length=512
47
- )
48
 
49
- tokenized_datasets = dataset_custom.map(tokenize_function, batched=True)
50
 
51
- # Apply LoRA for efficient fine-tuning
52
  lora_config = LoraConfig(
53
  r=8, lora_alpha=32, lora_dropout=0.05, bias="none",
54
- target_modules=["c_attn", "c_proj"] # Apply LoRA to attention layers
55
  )
56
 
57
  model = get_peft_model(model, lora_config)
58
- model.gradient_checkpointing_enable() # Enable checkpointing for memory efficiency
59
 
60
- # Training arguments
61
  training_args = TrainingArguments(
62
  output_dir="gpt2_finetuned",
63
  auto_find_batch_size=True,
@@ -70,14 +61,14 @@ training_args = TrainingArguments(
70
  push_to_hub=True
71
  )
72
 
73
- # Trainer setup
74
  trainer = Trainer(
75
  model=model,
76
  args=training_args,
77
  train_dataset=tokenized_datasets
78
  )
79
 
80
- # Start fine-tuning
81
  trainer.train()
82
 
83
  # Save and push the model to Hugging Face Hub
@@ -85,28 +76,21 @@ trainer.save_model("gpt2_finetuned")
85
  tokenizer.save_pretrained("gpt2_finetuned")
86
  trainer.push_to_hub()
87
 
88
- # Deploy as Gradio Interface
89
- def generate_response(prompt):
90
- inputs = tokenizer(prompt, return_tensors="pt")
91
- outputs = model.generate(**inputs, max_length=100)
92
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
93
-
94
- # Gradio Chat Interface
95
  def respond(message, history, system_message, max_tokens, temperature, top_p):
96
  messages = [{"role": "system", "content": system_message}]
97
 
98
- # Ensure 'history' is handled as a list of dicts
99
  if isinstance(history, list):
100
  for entry in history:
101
  if isinstance(entry, dict):
102
- messages.append(entry) # Correct format already
103
  elif isinstance(entry, tuple) and len(entry) == 2:
104
  messages.append({"role": "user", "content": entry[0]})
105
  messages.append({"role": "assistant", "content": entry[1]})
106
 
107
  messages.append({"role": "user", "content": message})
108
- response = ""
109
 
 
110
  for message in client.chat_completion(
111
  messages,
112
  max_tokens=max_tokens,
@@ -118,6 +102,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
118
  response += token
119
  yield response
120
 
 
121
  demo = gr.ChatInterface(
122
  respond,
123
  chatbot=gr.Chatbot(type="messages"),
@@ -125,16 +110,10 @@ demo = gr.ChatInterface(
125
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
126
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
127
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
128
- gr.Slider(
129
- minimum=0.1,
130
- maximum=1.0,
131
- value=0.95,
132
- step=0.05,
133
- label="Top-p (nucleus sampling)",
134
- ),
135
  ],
136
  )
137
 
138
- # Launch the Gradio app
139
  if __name__ == "__main__":
140
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
4
+ from datasets import load_dataset
5
  from peft import LoraConfig, get_peft_model
6
  import torch
7
 
8
  # Initialize Hugging Face Inference Client
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
+ # GPT-2 Model Setup
12
  model_name = "gpt2"
13
  tokenizer = AutoTokenizer.from_pretrained(model_name)
14
  model = AutoModelForCausalLM.from_pretrained(model_name)
15
 
16
+ # Add padding token
17
+ tokenizer.pad_token = tokenizer.eos_token
 
18
 
19
+ # Custom Dataset
20
  custom_data = [
21
+ {"prompt": "Who are you?", "response": "I am Eva, a virtual voice assistant."},
22
+ {"prompt": "What is your name?", "response": "I am Eva, how can I help you?"},
23
+ {"prompt": "What can you do?", "response": "I can assist with answering questions, searching the web, and much more!"},
24
+ {"prompt": "Who invented the computer?", "response": "Charles Babbage is known as the father of the computer."},
25
+ {"prompt": "Tell me a joke.", "response": "Why don’t scientists trust atoms? Because they make up everything!"},
26
+ {"prompt": "Who is the Prime Minister of India?", "response": "The current Prime Minister of India is Narendra Modi."},
27
+ {"prompt": "Who created you?", "response": "I was created by an expert team specializing in AI fine-tuning and web development."}
28
  ]
29
 
30
  # Convert custom dataset to Hugging Face Dataset
31
+ dataset_custom = load_dataset("json", data_files={"train": custom_data})
 
 
 
32
 
33
+ # Load OpenWebText dataset with `trust_remote_code=True`
34
+ dataset = load_dataset("Skylion007/openwebtext", split="train[:20%]", trust_remote_code=True)
35
 
36
  # Tokenization function
37
  def tokenize_function(examples):
38
+ return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
 
 
 
 
 
39
 
40
+ tokenized_datasets = dataset.map(tokenize_function, batched=True)
41
 
42
+ # LoRA Configuration
43
  lora_config = LoraConfig(
44
  r=8, lora_alpha=32, lora_dropout=0.05, bias="none",
45
+ target_modules=["c_attn", "c_proj"]
46
  )
47
 
48
  model = get_peft_model(model, lora_config)
49
+ model.gradient_checkpointing_enable()
50
 
51
+ # Training Arguments
52
  training_args = TrainingArguments(
53
  output_dir="gpt2_finetuned",
54
  auto_find_batch_size=True,
 
61
  push_to_hub=True
62
  )
63
 
64
+ # Trainer Setup
65
  trainer = Trainer(
66
  model=model,
67
  args=training_args,
68
  train_dataset=tokenized_datasets
69
  )
70
 
71
+ # Fine-tuning
72
  trainer.train()
73
 
74
  # Save and push the model to Hugging Face Hub
 
76
  tokenizer.save_pretrained("gpt2_finetuned")
77
  trainer.push_to_hub()
78
 
79
+ # Chatbot Response Function
 
 
 
 
 
 
80
  def respond(message, history, system_message, max_tokens, temperature, top_p):
81
  messages = [{"role": "system", "content": system_message}]
82
 
 
83
  if isinstance(history, list):
84
  for entry in history:
85
  if isinstance(entry, dict):
86
+ messages.append(entry)
87
  elif isinstance(entry, tuple) and len(entry) == 2:
88
  messages.append({"role": "user", "content": entry[0]})
89
  messages.append({"role": "assistant", "content": entry[1]})
90
 
91
  messages.append({"role": "user", "content": message})
 
92
 
93
+ response = ""
94
  for message in client.chat_completion(
95
  messages,
96
  max_tokens=max_tokens,
 
102
  response += token
103
  yield response
104
 
105
+ # Gradio Chatbot Interface
106
  demo = gr.ChatInterface(
107
  respond,
108
  chatbot=gr.Chatbot(type="messages"),
 
110
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
111
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
112
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
113
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
114
  ],
115
  )
116
 
117
+ # Launch the App
118
  if __name__ == "__main__":
119
  demo.launch()