Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,60 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
"""
|
43 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
-
"""
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
respond,
|
47 |
-
additional_inputs=[
|
48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
-
gr.Slider(
|
52 |
-
minimum=0.1,
|
53 |
-
maximum=1.0,
|
54 |
-
value=0.95,
|
55 |
-
step=0.05,
|
56 |
-
label="Top-p (nucleus sampling)",
|
57 |
-
),
|
58 |
-
],
|
59 |
)
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
|
|
1 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel, Trainer, TrainingArguments
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
+
|
5 |
+
# Define the training arguments
|
6 |
+
training_args = TrainingArguments(
|
7 |
+
per_device_train_batch_size=4,
|
8 |
+
num_train_epochs=3,
|
9 |
+
logging_dir='./logs',
|
10 |
+
)
|
11 |
+
|
12 |
+
# Load the pre-trained GPT-2 model and tokenizer
|
13 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
14 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2")
|
15 |
+
|
16 |
+
# Example training data
|
17 |
+
training_data = [
|
18 |
+
"What is your name?",
|
19 |
+
"How are you?",
|
20 |
+
"What do you do?",
|
21 |
+
"Tell me about yourself."
|
22 |
+
]
|
23 |
+
|
24 |
+
# Tokenize the training data
|
25 |
+
input_ids = tokenizer(training_data, return_tensors="pt", padding=True, truncation=True)["input_ids"]
|
26 |
+
|
27 |
+
# Define a dummy data collator (required by Trainer)
|
28 |
+
class DummyDataCollator:
|
29 |
+
def __call__(self, features):
|
30 |
+
return features
|
31 |
+
|
32 |
+
# Define a Trainer instance
|
33 |
+
trainer = Trainer(
|
34 |
+
model=model,
|
35 |
+
args=training_args,
|
36 |
+
data_collator=DummyDataCollator(),
|
37 |
+
train_dataset=input_ids
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
)
|
39 |
|
40 |
+
# Train the model
|
41 |
+
trainer.train()
|
42 |
+
|
43 |
+
# Define the chatbot function
|
44 |
+
def chatbot(input_text):
|
45 |
+
# Tokenize input text
|
46 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
47 |
+
|
48 |
+
# Generate response from the model
|
49 |
+
output_ids = model.generate(input_ids, max_length=50, pad_token_id=tokenizer.eos_token_id)
|
50 |
+
|
51 |
+
# Decode the generated response
|
52 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
53 |
+
|
54 |
+
return response
|
55 |
+
|
56 |
+
# Create the Gradio interface
|
57 |
+
chatbot_interface = gr.Interface(chatbot, "textbox", "textbox", title="Chatbot")
|
58 |
|
59 |
+
# Launch the Gradio interface
|
60 |
+
chatbot_interface.launch()
|