tasal9 commited on
Commit
74d8f99
·
verified ·
1 Parent(s): 07bc0ab

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +42 -8
  2. app.py +136 -19
  3. requirements.txt +4 -0
README.md CHANGED
@@ -13,14 +13,31 @@ hardware: zero-gpu-a10g
13
 
14
  # ZamAI-Mistral-7B-Pashto Space
15
 
16
- This is a Space for the ZamAI-Mistral-7B-Pashto model. You can:
17
 
18
- 1. Test the model
19
- 2. Train the model
20
- 3. Fine-tune the model
21
 
22
  Uses ZeroGPU for efficient GPU acceleration.
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  ## Example Usage
25
 
26
  **Input:**
@@ -33,10 +50,27 @@ Uses ZeroGPU for efficient GPU acceleration.
33
  زه ښه یم، مننه!
34
  ```
35
 
36
- ## Instructions
37
 
38
- 1. Enter your Pashto text in the input box.
39
- 2. Click "Generate" to get the model's response.
40
- 3. For best results, keep input under 512 characters.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  ---
 
13
 
14
  # ZamAI-Mistral-7B-Pashto Space
15
 
16
+ This Hugging Face Space provides an interface for:
17
 
18
+ 1. **Testing the ZamAI-Mistral-7B-Pashto model** - Try out text generation
19
+ 2. **Fine-tuning the model** - Train on your own dataset
20
+ 3. **Downloading your fine-tuned model** - Get your customized model
21
 
22
  Uses ZeroGPU for efficient GPU acceleration.
23
 
24
+ ## How to Use
25
+
26
+ ### Test Model Tab
27
+ 1. Enter your Pashto text in the input box
28
+ 2. Click "Generate" to get the model's response
29
+ 3. For best results, keep input under 512 characters
30
+
31
+ ### Fine-tune Model Tab
32
+ 1. Enter a Hugging Face dataset name (e.g., "username/dataset")
33
+ 2. Set hyperparameters:
34
+ - Learning rate (default: 5e-5)
35
+ - Number of epochs (default: 3)
36
+ - Batch size (default: 8)
37
+ 3. Click "Start Fine-tuning"
38
+ 4. Check status with "Check Status" button
39
+ 5. Once complete, you can download your fine-tuned model
40
+
41
  ## Example Usage
42
 
43
  **Input:**
 
50
  زه ښه یم، مننه!
51
  ```
52
 
53
+ ## Training Data Format
54
 
55
+ The expected dataset format for fine-tuning is:
56
+ ```json
57
+ {
58
+ "train": [
59
+ {"text": "Your training examples here"}
60
+ ],
61
+ "validation": [
62
+ {"text": "Your validation examples here"}
63
+ ]
64
+ }
65
+ ```
66
+
67
+ You can also use the `instruction` and `response` format for instruction tuning:
68
+ ```json
69
+ {
70
+ "train": [
71
+ {"instruction": "Your instruction", "response": "Expected response"}
72
+ ]
73
+ }
74
+ ```
75
 
76
  ---
app.py CHANGED
@@ -1,12 +1,24 @@
1
  import gradio as gr
2
  import spaces
3
  import torch
4
- from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
5
  import threading
 
6
 
7
  # Model configuration
8
  MODEL_NAME = "tasal9/ZamAI-Mistral-7B-Pashto"
9
 
 
 
 
 
 
 
 
 
10
 
11
  # Cache model and tokenizer
12
  model_tokenizer_cache = {"model": None, "tokenizer": None, "loaded": False, "error": None}
@@ -51,28 +63,133 @@ def test_model(input_text):
51
  except Exception as e:
52
  return f"Model inference error: {str(e)}"
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  # Create Gradio interface
55
  with gr.Blocks(title="ZamAI-Mistral-7B-Pashto Space") as iface:
56
  gr.Markdown(f"# ZamAI-Mistral-7B-Pashto")
57
- gr.Markdown("""
58
- Example input:
59
- > سلام، څنګه یی؟
60
- """)
61
- loading = gr.State(False)
62
- with gr.Row():
63
- with gr.Column():
64
- input_text = gr.Textbox(label="Input", lines=3, value="سلام، څنګه یی؟")
65
- submit_btn = gr.Button("Generate")
66
- with gr.Column():
67
- output_text = gr.Textbox(label="Output", lines=3)
68
-
69
- def wrapped_test_model(input_text):
70
- loading.set(True)
71
- result = test_model(input_text)
72
- loading.set(False)
73
- return result
74
 
75
- submit_btn.click(fn=test_model, inputs=input_text, outputs=output_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  if __name__ == "__main__":
78
  iface.launch()
 
1
  import gradio as gr
2
  import spaces
3
  import torch
4
+ import os
5
+ import json
6
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments, Trainer
7
+ from datasets import load_dataset
8
  import threading
9
+ from datetime import datetime
10
 
11
  # Model configuration
12
  MODEL_NAME = "tasal9/ZamAI-Mistral-7B-Pashto"
13
 
14
+ # Fine-tuning configuration
15
+ FINE_TUNING_STATUS = {
16
+ "in_progress": False,
17
+ "completed": False,
18
+ "error": None,
19
+ "progress": 0,
20
+ "model_path": None
21
+ }
22
 
23
  # Cache model and tokenizer
24
  model_tokenizer_cache = {"model": None, "tokenizer": None, "loaded": False, "error": None}
 
63
  except Exception as e:
64
  return f"Model inference error: {str(e)}"
65
 
66
+ @spaces.GPU
67
+ def finetune_model(dataset_name, learning_rate, num_epochs, batch_size, progress=gr.Progress()):
68
+ """Fine-tune the model on a given dataset"""
69
+ FINE_TUNING_STATUS["in_progress"] = True
70
+ FINE_TUNING_STATUS["completed"] = False
71
+ FINE_TUNING_STATUS["error"] = None
72
+ FINE_TUNING_STATUS["progress"] = 0
73
+
74
+ try:
75
+ # Load dataset
76
+ progress(0.1, desc="Loading dataset...")
77
+ dataset = load_dataset(dataset_name)
78
+ progress(0.2, desc="Dataset loaded")
79
+
80
+ # Load model and tokenizer
81
+ progress(0.3, desc="Loading model and tokenizer...")
82
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
83
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
84
+ progress(0.4, desc="Model and tokenizer loaded")
85
+
86
+ # Prepare dataset
87
+ progress(0.5, desc="Preparing dataset...")
88
+ def tokenize_function(examples):
89
+ return tokenizer(examples["text"], truncation=True, padding="max_length", max_length=128)
90
+
91
+ tokenized_dataset = dataset.map(tokenize_function, batched=True)
92
+ progress(0.6, desc="Dataset prepared")
93
+
94
+ # Define training arguments
95
+ output_dir = f"fine-tuned-{datetime.now().strftime('%Y%m%d-%H%M%S')}"
96
+ training_args = TrainingArguments(
97
+ output_dir=output_dir,
98
+ learning_rate=float(learning_rate),
99
+ num_train_epochs=int(num_epochs),
100
+ per_device_train_batch_size=int(batch_size),
101
+ save_strategy="epoch",
102
+ logging_steps=10,
103
+ save_total_limit=2,
104
+ )
105
+
106
+ # Create trainer
107
+ progress(0.7, desc="Setting up trainer...")
108
+ trainer = Trainer(
109
+ model=model,
110
+ args=training_args,
111
+ train_dataset=tokenized_dataset["train"],
112
+ tokenizer=tokenizer,
113
+ )
114
+
115
+ # Train model
116
+ progress(0.8, desc="Training model...")
117
+ trainer.train()
118
+ progress(0.9, desc="Training complete")
119
+
120
+ # Save model
121
+ progress(0.95, desc="Saving model...")
122
+ model.save_pretrained(output_dir)
123
+ tokenizer.save_pretrained(output_dir)
124
+
125
+ FINE_TUNING_STATUS["completed"] = True
126
+ FINE_TUNING_STATUS["in_progress"] = False
127
+ FINE_TUNING_STATUS["progress"] = 100
128
+ FINE_TUNING_STATUS["model_path"] = output_dir
129
+
130
+ progress(1.0, desc="Fine-tuning complete!")
131
+ return f"Fine-tuning completed successfully. Model saved to {output_dir}"
132
+ except Exception as e:
133
+ FINE_TUNING_STATUS["error"] = str(e)
134
+ FINE_TUNING_STATUS["in_progress"] = False
135
+ return f"Fine-tuning failed: {str(e)}"
136
+
137
+ def get_finetune_status():
138
+ """Get the current status of fine-tuning"""
139
+ if FINE_TUNING_STATUS["in_progress"]:
140
+ return f"Fine-tuning in progress... ({FINE_TUNING_STATUS['progress']}%)"
141
+ elif FINE_TUNING_STATUS["completed"]:
142
+ return f"Fine-tuning completed. Model saved to {FINE_TUNING_STATUS['model_path']}"
143
+ elif FINE_TUNING_STATUS["error"]:
144
+ return f"Fine-tuning failed: {FINE_TUNING_STATUS['error']}"
145
+ else:
146
+ return "No fine-tuning has been started yet."
147
+
148
  # Create Gradio interface
149
  with gr.Blocks(title="ZamAI-Mistral-7B-Pashto Space") as iface:
150
  gr.Markdown(f"# ZamAI-Mistral-7B-Pashto")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
+ with gr.Tabs():
153
+ with gr.TabItem("Test Model"):
154
+ gr.Markdown("""
155
+ Test the ZamAI-Mistral-7B-Pashto model with your own text.
156
+
157
+ Example input:
158
+ > سلام، څنګه یی؟
159
+ """)
160
+ with gr.Row():
161
+ with gr.Column():
162
+ input_text = gr.Textbox(label="Input", lines=3, value="سلام، څنګه یی؟")
163
+ submit_btn = gr.Button("Generate")
164
+ with gr.Column():
165
+ output_text = gr.Textbox(label="Output", lines=3)
166
+
167
+ submit_btn.click(fn=test_model, inputs=input_text, outputs=output_text)
168
+
169
+ with gr.TabItem("Fine-tune Model"):
170
+ gr.Markdown("""
171
+ Fine-tune the model on your own dataset.
172
+
173
+ The dataset should be available on Hugging Face Hub and contain a 'text' column.
174
+ """)
175
+
176
+ dataset_name = gr.Textbox(label="Dataset Name (e.g., 'username/dataset')", value="")
177
+ with gr.Row():
178
+ learning_rate = gr.Number(label="Learning Rate", value=5e-5)
179
+ num_epochs = gr.Number(label="Number of Epochs", value=3, precision=0)
180
+ batch_size = gr.Number(label="Batch Size", value=8, precision=0)
181
+
182
+ finetune_btn = gr.Button("Start Fine-tuning")
183
+ finetune_output = gr.Textbox(label="Fine-tuning Status", interactive=False)
184
+
185
+ finetune_btn.click(
186
+ fn=finetune_model,
187
+ inputs=[dataset_name, learning_rate, num_epochs, batch_size],
188
+ outputs=finetune_output
189
+ )
190
+
191
+ status_btn = gr.Button("Check Status")
192
+ status_btn.click(fn=get_finetune_status, inputs=None, outputs=finetune_output)
193
 
194
  if __name__ == "__main__":
195
  iface.launch()
requirements.txt CHANGED
@@ -4,3 +4,7 @@ gradio==4.36.1
4
  spaces
5
  torch>=2.0.0
6
  transformers==4.39.3
 
 
 
 
 
4
  spaces
5
  torch>=2.0.0
6
  transformers==4.39.3
7
+ datasets>=2.17.0
8
+ accelerate>=0.28.0
9
+ bitsandbytes>=0.42.0
10
+ peft>=0.7.0