Spaces:
Runtime error
Runtime error
Add Gradio app for pashto-base-bloom with train/finetune/test
Browse files- README.md +15 -5
- app.py +145 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,22 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: pashto-base-bloom Training Space
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.36.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
hardware: zero-a10g
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# pashto-base-bloom Training Space
|
| 15 |
+
|
| 16 |
+
This space provides three main functionalities for the pashto-base-bloom model:
|
| 17 |
+
|
| 18 |
+
1. **Train**: Train the model from scratch
|
| 19 |
+
2. **Fine-tune**: Fine-tune the existing model
|
| 20 |
+
3. **Test**: Test the model with sample inputs
|
| 21 |
+
|
| 22 |
+
The space uses ZeroGPU for efficient GPU computation.
|
app.py
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Model configuration
|
| 8 |
+
MODEL_NAME = "tasal9/pashto-base-bloom"
|
| 9 |
+
|
| 10 |
+
@spaces.GPU
|
| 11 |
+
def load_model():
|
| 12 |
+
"""Load the model and tokenizer"""
|
| 13 |
+
try:
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16)
|
| 16 |
+
if tokenizer.pad_token is None:
|
| 17 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 18 |
+
return model, tokenizer
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return None, None
|
| 21 |
+
|
| 22 |
+
@spaces.GPU
|
| 23 |
+
def test_model(input_text, max_length=100, temperature=0.7):
|
| 24 |
+
"""Test the model with given input"""
|
| 25 |
+
if not input_text.strip():
|
| 26 |
+
return "Please enter some text to test the model."
|
| 27 |
+
|
| 28 |
+
model, tokenizer = load_model()
|
| 29 |
+
|
| 30 |
+
if model is None or tokenizer is None:
|
| 31 |
+
return "β Failed to load model. Please check if the model exists on Hugging Face Hub."
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 35 |
+
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
inputs,
|
| 39 |
+
max_length=len(inputs[0]) + max_length,
|
| 40 |
+
temperature=temperature,
|
| 41 |
+
do_sample=True,
|
| 42 |
+
pad_token_id=tokenizer.eos_token_id
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 46 |
+
return response[len(input_text):].strip()
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"β Error during generation: {str(e)}"
|
| 50 |
+
|
| 51 |
+
def train_model(dataset_text, epochs=1, learning_rate=2e-5):
|
| 52 |
+
"""Train the model (placeholder implementation)"""
|
| 53 |
+
return f"π Training started with {epochs} epochs and learning rate {learning_rate}\n\nNote: This is a placeholder. Actual training requires dataset preparation and more computational resources."
|
| 54 |
+
|
| 55 |
+
def finetune_model(dataset_text, epochs=1, learning_rate=5e-5):
|
| 56 |
+
"""Fine-tune the model (placeholder implementation)"""
|
| 57 |
+
return f"π§ Fine-tuning started with {epochs} epochs and learning rate {learning_rate}\n\nNote: This is a placeholder. Actual fine-tuning requires dataset preparation and more computational resources."
|
| 58 |
+
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
with gr.Blocks(title="pashto-base-bloom Training Space", theme=gr.themes.Soft()) as iface:
|
| 61 |
+
gr.Markdown(f"# pashto-base-bloom Training Space")
|
| 62 |
+
gr.Markdown("Choose your operation: Train, Fine-tune, or Test the model")
|
| 63 |
+
|
| 64 |
+
with gr.Tabs():
|
| 65 |
+
# Test Tab
|
| 66 |
+
with gr.TabItem("π§ͺ Test Model"):
|
| 67 |
+
gr.Markdown("### Test the model with your input")
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
test_input = gr.Textbox(
|
| 71 |
+
label="Input Text",
|
| 72 |
+
placeholder="Enter text to test the model...",
|
| 73 |
+
lines=3
|
| 74 |
+
)
|
| 75 |
+
max_length_slider = gr.Slider(
|
| 76 |
+
minimum=10,
|
| 77 |
+
maximum=500,
|
| 78 |
+
value=100,
|
| 79 |
+
label="Max Length"
|
| 80 |
+
)
|
| 81 |
+
temperature_slider = gr.Slider(
|
| 82 |
+
minimum=0.1,
|
| 83 |
+
maximum=2.0,
|
| 84 |
+
value=0.7,
|
| 85 |
+
label="Temperature"
|
| 86 |
+
)
|
| 87 |
+
test_btn = gr.Button("π Generate", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Column():
|
| 90 |
+
test_output = gr.Textbox(
|
| 91 |
+
label="Model Output",
|
| 92 |
+
lines=5,
|
| 93 |
+
interactive=False
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
test_btn.click(
|
| 97 |
+
fn=test_model,
|
| 98 |
+
inputs=[test_input, max_length_slider, temperature_slider],
|
| 99 |
+
outputs=test_output
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Train Tab
|
| 103 |
+
with gr.TabItem("ποΈ Train Model"):
|
| 104 |
+
gr.Markdown("### Train the model from scratch")
|
| 105 |
+
train_dataset = gr.Textbox(
|
| 106 |
+
label="Training Dataset",
|
| 107 |
+
placeholder="Upload or paste your training data...",
|
| 108 |
+
lines=5
|
| 109 |
+
)
|
| 110 |
+
with gr.Row():
|
| 111 |
+
train_epochs = gr.Number(label="Epochs", value=1, minimum=1)
|
| 112 |
+
train_lr = gr.Number(label="Learning Rate", value=2e-5, minimum=1e-6)
|
| 113 |
+
|
| 114 |
+
train_btn = gr.Button("π Start Training", variant="primary")
|
| 115 |
+
train_output = gr.Textbox(label="Training Output", lines=5, interactive=False)
|
| 116 |
+
|
| 117 |
+
train_btn.click(
|
| 118 |
+
fn=train_model,
|
| 119 |
+
inputs=[train_dataset, train_epochs, train_lr],
|
| 120 |
+
outputs=train_output
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
# Fine-tune Tab
|
| 124 |
+
with gr.TabItem("π§ Fine-tune Model"):
|
| 125 |
+
gr.Markdown("### Fine-tune the existing model")
|
| 126 |
+
finetune_dataset = gr.Textbox(
|
| 127 |
+
label="Fine-tuning Dataset",
|
| 128 |
+
placeholder="Upload or paste your fine-tuning data...",
|
| 129 |
+
lines=5
|
| 130 |
+
)
|
| 131 |
+
with gr.Row():
|
| 132 |
+
finetune_epochs = gr.Number(label="Epochs", value=1, minimum=1)
|
| 133 |
+
finetune_lr = gr.Number(label="Learning Rate", value=5e-5, minimum=1e-6)
|
| 134 |
+
|
| 135 |
+
finetune_btn = gr.Button("π§ Start Fine-tuning", variant="primary")
|
| 136 |
+
finetune_output = gr.Textbox(label="Fine-tuning Output", lines=5, interactive=False)
|
| 137 |
+
|
| 138 |
+
finetune_btn.click(
|
| 139 |
+
fn=finetune_model,
|
| 140 |
+
inputs=[finetune_dataset, finetune_epochs, finetune_lr],
|
| 141 |
+
outputs=finetune_output
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.36.1
|
| 2 |
+
spaces
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
datasets
|
| 6 |
+
accelerate
|