Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
-
|
10 |
def respond(
|
11 |
message,
|
12 |
-
history
|
13 |
system_message,
|
14 |
max_tokens,
|
15 |
temperature,
|
@@ -17,11 +13,10 @@ def respond(
|
|
17 |
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
for
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
@@ -35,58 +30,39 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
respond,
|
48 |
-
chatbot=gr.Chatbot(type="messages"),
|
49 |
additional_inputs=[
|
50 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
51 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
52 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
53 |
-
gr.Slider(
|
54 |
-
minimum=0.1,
|
55 |
-
maximum=1.0,
|
56 |
-
value=0.95,
|
57 |
-
step=0.05,
|
58 |
-
label="Top-p (nucleus sampling)",
|
59 |
-
),
|
60 |
],
|
61 |
)
|
62 |
|
63 |
-
|
64 |
if __name__ == "__main__":
|
65 |
demo.launch()
|
66 |
|
67 |
-
#
|
68 |
-
|
69 |
-
#
|
70 |
-
# Install required libraries (Run this separately in a terminal or notebook cell)
|
71 |
-
# !pip install transformers datasets peft accelerate bitsandbytes torch torchvision torchaudio gradio -q
|
72 |
-
|
73 |
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
74 |
from datasets import load_dataset
|
75 |
from peft import LoraConfig, get_peft_model
|
76 |
import torch
|
77 |
|
78 |
-
# Authenticate Hugging Face
|
79 |
from huggingface_hub import notebook_login
|
80 |
notebook_login()
|
81 |
|
82 |
-
# Load GPT-2 model and tokenizer
|
83 |
model_name = "gpt2"
|
84 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
85 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
86 |
|
87 |
-
# Load the OpenWebText dataset using streaming (No download required)
|
88 |
-
|
89 |
-
# Custom Dataset (Predefined Q&A Pairs for Project Expo)
|
90 |
custom_data = [
|
91 |
{"prompt": "Who are you?", "response": "I am Eva, a virtual voice assistant."},
|
92 |
{"prompt": "What is your name?", "response": "I am Eva, how can I help you?"},
|
@@ -97,30 +73,22 @@ custom_data = [
|
|
97 |
{"prompt": "Who created you?", "response": "I was created by an expert team specializing in AI fine-tuning and web development."}
|
98 |
]
|
99 |
|
100 |
-
# Convert custom dataset to Hugging Face Dataset
|
101 |
dataset_custom = load_dataset("json", data_files={"train": custom_data})
|
|
|
102 |
|
103 |
-
# Merge with OpenWebText dataset
|
104 |
-
dataset = load_dataset("Skylion007/openwebtext", split="train[:20%]") # Load 5% to avoid streaming issues
|
105 |
-
|
106 |
-
# Tokenization function
|
107 |
def tokenize_function(examples):
|
108 |
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
|
109 |
|
110 |
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
111 |
|
112 |
-
# Apply LoRA for efficient fine-tuning
|
113 |
lora_config = LoraConfig(
|
114 |
r=8, lora_alpha=32, lora_dropout=0.05, bias="none",
|
115 |
-
target_modules=["c_attn", "c_proj"]
|
116 |
)
|
117 |
|
118 |
model = get_peft_model(model, lora_config)
|
119 |
-
|
120 |
-
# Enable gradient checkpointing to reduce memory usage
|
121 |
model.gradient_checkpointing_enable()
|
122 |
|
123 |
-
# Training arguments
|
124 |
training_args = TrainingArguments(
|
125 |
output_dir="gpt2_finetuned",
|
126 |
auto_find_batch_size=True,
|
@@ -133,22 +101,17 @@ training_args = TrainingArguments(
|
|
133 |
push_to_hub=True
|
134 |
)
|
135 |
|
136 |
-
# Trainer setup
|
137 |
trainer = Trainer(
|
138 |
model=model,
|
139 |
args=training_args,
|
140 |
train_dataset=tokenized_datasets
|
141 |
)
|
142 |
|
143 |
-
# Start fine-tuning
|
144 |
trainer.train()
|
145 |
-
|
146 |
-
# Save and push the model to Hugging Face Hub
|
147 |
trainer.save_model("gpt2_finetuned")
|
148 |
tokenizer.save_pretrained("gpt2_finetuned")
|
149 |
trainer.push_to_hub()
|
150 |
|
151 |
-
# Deploy as Gradio Interface
|
152 |
def generate_response(prompt):
|
153 |
inputs = tokenizer(prompt, return_tensors="pt")
|
154 |
outputs = model.generate(**inputs, max_length=100)
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
|
|
|
|
|
|
|
4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
5 |
|
|
|
6 |
def respond(
|
7 |
message,
|
8 |
+
history,
|
9 |
system_message,
|
10 |
max_tokens,
|
11 |
temperature,
|
|
|
13 |
):
|
14 |
messages = [{"role": "system", "content": system_message}]
|
15 |
|
16 |
+
# Updated for OpenAI-style format (replacing tuples)
|
17 |
+
for entry in history:
|
18 |
+
role = "user" if entry["role"] == "user" else "assistant"
|
19 |
+
messages.append({"role": role, "content": entry["content"]})
|
|
|
20 |
|
21 |
messages.append({"role": "user", "content": message})
|
22 |
|
|
|
30 |
top_p=top_p,
|
31 |
):
|
32 |
token = message.choices[0].delta.content
|
|
|
33 |
response += token
|
34 |
yield response
|
35 |
|
36 |
+
# Updated ChatInterface with correct type
|
|
|
|
|
|
|
37 |
demo = gr.ChatInterface(
|
38 |
respond,
|
39 |
+
chatbot=gr.Chatbot(type="messages"), # Correct format
|
40 |
additional_inputs=[
|
41 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
42 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
43 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
44 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
],
|
46 |
)
|
47 |
|
|
|
48 |
if __name__ == "__main__":
|
49 |
demo.launch()
|
50 |
|
51 |
+
# -----------------------------------------------
|
52 |
+
# Fine-Tuning GPT-2 on Hugging Face Spaces (Improved Section)
|
53 |
+
# -----------------------------------------------
|
|
|
|
|
|
|
54 |
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
55 |
from datasets import load_dataset
|
56 |
from peft import LoraConfig, get_peft_model
|
57 |
import torch
|
58 |
|
|
|
59 |
from huggingface_hub import notebook_login
|
60 |
notebook_login()
|
61 |
|
|
|
62 |
model_name = "gpt2"
|
63 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
64 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
65 |
|
|
|
|
|
|
|
66 |
custom_data = [
|
67 |
{"prompt": "Who are you?", "response": "I am Eva, a virtual voice assistant."},
|
68 |
{"prompt": "What is your name?", "response": "I am Eva, how can I help you?"},
|
|
|
73 |
{"prompt": "Who created you?", "response": "I was created by an expert team specializing in AI fine-tuning and web development."}
|
74 |
]
|
75 |
|
|
|
76 |
dataset_custom = load_dataset("json", data_files={"train": custom_data})
|
77 |
+
dataset = load_dataset("Skylion007/openwebtext", split="train[:20%]")
|
78 |
|
|
|
|
|
|
|
|
|
79 |
def tokenize_function(examples):
|
80 |
return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=512)
|
81 |
|
82 |
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
83 |
|
|
|
84 |
lora_config = LoraConfig(
|
85 |
r=8, lora_alpha=32, lora_dropout=0.05, bias="none",
|
86 |
+
target_modules=["c_attn", "c_proj"]
|
87 |
)
|
88 |
|
89 |
model = get_peft_model(model, lora_config)
|
|
|
|
|
90 |
model.gradient_checkpointing_enable()
|
91 |
|
|
|
92 |
training_args = TrainingArguments(
|
93 |
output_dir="gpt2_finetuned",
|
94 |
auto_find_batch_size=True,
|
|
|
101 |
push_to_hub=True
|
102 |
)
|
103 |
|
|
|
104 |
trainer = Trainer(
|
105 |
model=model,
|
106 |
args=training_args,
|
107 |
train_dataset=tokenized_datasets
|
108 |
)
|
109 |
|
|
|
110 |
trainer.train()
|
|
|
|
|
111 |
trainer.save_model("gpt2_finetuned")
|
112 |
tokenizer.save_pretrained("gpt2_finetuned")
|
113 |
trainer.push_to_hub()
|
114 |
|
|
|
115 |
def generate_response(prompt):
|
116 |
inputs = tokenizer(prompt, return_tensors="pt")
|
117 |
outputs = model.generate(**inputs, max_length=100)
|