Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, Trainer, TrainingArguments
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
|
| 6 |
+
# Load the GPT-J model and tokenizer from Hugging Face
|
| 7 |
+
model_name = "EleutherAI/gpt-j-6B" # GPT-J model
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
# Define a function for generating responses
|
| 12 |
+
def generate_response(prompt):
|
| 13 |
+
# Tokenize the input text
|
| 14 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 15 |
+
|
| 16 |
+
# Generate response using the model
|
| 17 |
+
outputs = model.generate(
|
| 18 |
+
inputs.input_ids,
|
| 19 |
+
max_length=200, # Adjust response length
|
| 20 |
+
do_sample=True, # Sample from the model
|
| 21 |
+
temperature=0.7, # Adjust creativity (lower = more conservative)
|
| 22 |
+
top_p=0.9, # Nucleus sampling for diverse answers
|
| 23 |
+
pad_token_id=tokenizer.eos_token_id # EOS token to avoid error
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Decode and return the response
|
| 27 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
+
|
| 29 |
+
# Create a Gradio interface for the chatbot
|
| 30 |
+
def chatbot_interface(user_input):
|
| 31 |
+
response = generate_response(user_input)
|
| 32 |
+
return response
|
| 33 |
+
|
| 34 |
+
# Fine-tuning function
|
| 35 |
+
def fine_tune_model(dataset_name, output_dir='./fine-tuned-model'):
|
| 36 |
+
# Load dataset (assuming it's in Hugging Face format or a similar format)
|
| 37 |
+
dataset = load_dataset(dataset_name)
|
| 38 |
+
|
| 39 |
+
# Define training arguments
|
| 40 |
+
training_args = TrainingArguments(
|
| 41 |
+
output_dir=output_dir, # Output directory for the fine-tuned model
|
| 42 |
+
per_device_train_batch_size=2, # Adjust batch size according to GPU capacity
|
| 43 |
+
num_train_epochs=1, # Adjust the number of epochs based on the dataset
|
| 44 |
+
save_steps=500, # Save checkpoint every 500 steps
|
| 45 |
+
save_total_limit=2, # Keep only last two checkpoints
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Define the Trainer object
|
| 49 |
+
trainer = Trainer(
|
| 50 |
+
model=model,
|
| 51 |
+
args=training_args,
|
| 52 |
+
train_dataset=dataset['train'],
|
| 53 |
+
eval_dataset=dataset['test'],
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Fine-tune the model
|
| 57 |
+
trainer.train()
|
| 58 |
+
|
| 59 |
+
return f"Model fine-tuned and saved to {output_dir}"
|
| 60 |
+
|
| 61 |
+
# Gradio interface for fine-tuning the model
|
| 62 |
+
def fine_tune_interface(dataset_name):
|
| 63 |
+
result = fine_tune_model(dataset_name)
|
| 64 |
+
return result
|
| 65 |
+
|
| 66 |
+
# Gradio UI for both chatbot and fine-tuning functionalities
|
| 67 |
+
with gr.Blocks() as demo:
|
| 68 |
+
gr.Markdown("# Cable Industry Customer Service Chatbot")
|
| 69 |
+
|
| 70 |
+
# Chatbot section
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
chatbot_input = gr.Textbox(label="Ask the Cable Industry Chatbot")
|
| 74 |
+
chatbot_output = gr.Textbox(label="Chatbot Response")
|
| 75 |
+
chatbot_button = gr.Button("Generate Response")
|
| 76 |
+
|
| 77 |
+
chatbot_button.click(fn=chatbot_interface, inputs=chatbot_input, outputs=chatbot_output)
|
| 78 |
+
|
| 79 |
+
# Fine-tuning section
|
| 80 |
+
with gr.Row():
|
| 81 |
+
with gr.Column():
|
| 82 |
+
gr.Markdown("### Fine-tune the Chatbot")
|
| 83 |
+
dataset_input = gr.Textbox(label="Enter Hugging Face Dataset Name (e.g., 'cable_data')")
|
| 84 |
+
fine_tune_button = gr.Button("Fine-tune Model")
|
| 85 |
+
fine_tune_output = gr.Textbox(label="Fine-tune Status")
|
| 86 |
+
|
| 87 |
+
fine_tune_button.click(fn=fine_tune_interface, inputs=dataset_input, outputs=fine_tune_output)
|
| 88 |
+
|
| 89 |
+
# Launch the Gradio interface
|
| 90 |
+
demo.launch()
|