Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,20 +3,30 @@ import subprocess
|
|
3 |
import os
|
4 |
import sys
|
5 |
from datetime import datetime
|
|
|
|
|
|
|
6 |
|
7 |
def run_training(model_name):
|
8 |
"""Execute the training script and save to Hub"""
|
9 |
if not model_name.strip():
|
10 |
return "β Please enter a model name!"
|
11 |
|
12 |
-
# Set environment
|
13 |
os.environ['MODEL_NAME'] = model_name.strip()
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
# Run training script
|
17 |
process = subprocess.Popen(
|
18 |
-
[sys.executable, 'train.py'],
|
19 |
-
stdout=subprocess.PIPE,
|
20 |
stderr=subprocess.STDOUT,
|
21 |
universal_newlines=True,
|
22 |
bufsize=1
|
@@ -32,65 +42,161 @@ def run_training(model_name):
|
|
32 |
break
|
33 |
|
34 |
if process.returncode == 0:
|
35 |
-
|
|
|
|
|
|
|
36 |
else:
|
37 |
output += f"\n\nβ Training failed with return code: {process.returncode}"
|
38 |
-
|
39 |
return output
|
40 |
|
41 |
except Exception as e:
|
42 |
return f"β Error: {str(e)}"
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Gradio Interface
|
45 |
with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
46 |
gr.Markdown("""
|
47 |
# π€ RoBERTa CUAD Question Answering Trainer
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
**Instructions:**
|
52 |
-
1. Enter your desired model name (format: `your-username/model-name`)
|
53 |
-
2. Click "Start Training"
|
54 |
-
3. Wait ~20-30 minutes for training to complete
|
55 |
-
4. Your model will be saved and publicly available!
|
56 |
""")
|
57 |
|
58 |
-
with gr.
|
59 |
-
with gr.
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
65 |
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
gr.Markdown("""
|
70 |
-
**
|
71 |
-
|
72 |
-
- Dataset: CUAD (2000 samples)
|
73 |
-
- Time: ~20-30 minutes
|
74 |
-
- GPU: T4 (free)
|
75 |
""")
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
gr.Markdown("""
|
92 |
---
|
93 |
-
**
|
|
|
|
|
94 |
""")
|
95 |
|
96 |
if __name__ == "__main__":
|
|
|
3 |
import os
|
4 |
import sys
|
5 |
from datetime import datetime
|
6 |
+
from transformers import AutoTokenizer
|
7 |
+
from peft import AutoPeftModelForQuestionAnswering
|
8 |
+
from huggingface_hub import login
|
9 |
|
10 |
def run_training(model_name):
|
11 |
"""Execute the training script and save to Hub"""
|
12 |
if not model_name.strip():
|
13 |
return "β Please enter a model name!"
|
14 |
|
15 |
+
# Set environment variables for the script
|
16 |
os.environ['MODEL_NAME'] = model_name.strip()
|
17 |
|
18 |
+
# Get HF token from environment (set as Space secret)
|
19 |
+
hf_token = os.environ.get('roberta_token')
|
20 |
+
if hf_token:
|
21 |
+
print("β
HF Token found and set")
|
22 |
+
else:
|
23 |
+
print("β οΈ No roberta_token found - model won't be pushed to Hub")
|
24 |
+
|
25 |
try:
|
26 |
# Run training script
|
27 |
process = subprocess.Popen(
|
28 |
+
[sys.executable, 'train.py'],
|
29 |
+
stdout=subprocess.PIPE,
|
30 |
stderr=subprocess.STDOUT,
|
31 |
universal_newlines=True,
|
32 |
bufsize=1
|
|
|
42 |
break
|
43 |
|
44 |
if process.returncode == 0:
|
45 |
+
if hf_token:
|
46 |
+
output += f"\n\nβ
SUCCESS! Model should be saved to: https://huggingface.co/{model_name}"
|
47 |
+
else:
|
48 |
+
output += f"\n\nβ
Training completed! Model saved locally (no HF token for Hub upload)"
|
49 |
else:
|
50 |
output += f"\n\nβ Training failed with return code: {process.returncode}"
|
51 |
+
|
52 |
return output
|
53 |
|
54 |
except Exception as e:
|
55 |
return f"β Error: {str(e)}"
|
56 |
|
57 |
+
def push_existing_model(model_name):
|
58 |
+
"""Push already trained model to Hub"""
|
59 |
+
if not model_name.strip():
|
60 |
+
return "β Please enter a model name!"
|
61 |
+
|
62 |
+
# Get token from environment
|
63 |
+
hf_token = os.environ.get('roberta_token')
|
64 |
+
|
65 |
+
if not hf_token:
|
66 |
+
return "β roberta_token not found in environment!\nMake sure roberta_token is set in your Space secrets."
|
67 |
+
|
68 |
+
try:
|
69 |
+
output = "π Logging into Hugging Face Hub...\n"
|
70 |
+
login(token=hf_token)
|
71 |
+
output += "β
Login successful!\n\n"
|
72 |
+
|
73 |
+
output += "π Checking for trained model...\n"
|
74 |
+
# Check if model exists
|
75 |
+
if not os.path.exists("./model_output"):
|
76 |
+
return output + "β ./model_output directory not found!\nMake sure you've run training first."
|
77 |
+
|
78 |
+
# Load your already-trained model
|
79 |
+
output += "π Loading trained model from ./model_output...\n"
|
80 |
+
model = AutoPeftModelForQuestionAnswering.from_pretrained("./model_output")
|
81 |
+
tokenizer = AutoTokenizer.from_pretrained("./model_output")
|
82 |
+
output += "β
Model loaded successfully!\n\n"
|
83 |
+
|
84 |
+
# Push to Hub
|
85 |
+
model_name = model_name.strip()
|
86 |
+
output += f"β¬οΈ Pushing model to Hub: {model_name}\n"
|
87 |
+
|
88 |
+
model.push_to_hub(model_name, private=False)
|
89 |
+
tokenizer.push_to_hub(model_name, private=False)
|
90 |
+
|
91 |
+
output += f"π SUCCESS! Model pushed to: https://huggingface.co/{model_name}\n"
|
92 |
+
|
93 |
+
# Also push training info if it exists
|
94 |
+
training_info_path = "./model_output/training_info.json"
|
95 |
+
if os.path.exists(training_info_path):
|
96 |
+
from huggingface_hub import upload_file
|
97 |
+
upload_file(
|
98 |
+
path_or_fileobj=training_info_path,
|
99 |
+
path_in_repo="training_info.json",
|
100 |
+
repo_id=model_name,
|
101 |
+
repo_type="model"
|
102 |
+
)
|
103 |
+
output += "π Training info also uploaded!\n"
|
104 |
+
|
105 |
+
return output
|
106 |
+
|
107 |
+
except Exception as e:
|
108 |
+
return f"β Error: {str(e)}\n\nCommon issues:\n- Invalid token\n- Model name already exists\n- Network issues"
|
109 |
+
|
110 |
# Gradio Interface
|
111 |
with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
112 |
gr.Markdown("""
|
113 |
# π€ RoBERTa CUAD Question Answering Trainer
|
114 |
|
115 |
+
Train a RoBERTa model with LoRA on CUAD dataset OR push an existing trained model to Hub.
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
""")
|
117 |
|
118 |
+
with gr.Tabs():
|
119 |
+
with gr.TabItem("π Train New Model"):
|
120 |
+
gr.Markdown("""
|
121 |
+
**Instructions:**
|
122 |
+
1. Enter your desired model name (format: `your-username/model-name`)
|
123 |
+
2. Click "Start Training"
|
124 |
+
3. Wait ~45-75 minutes for training to complete
|
125 |
+
""")
|
126 |
|
127 |
+
with gr.Row():
|
128 |
+
with gr.Column():
|
129 |
+
model_name_input = gr.Textbox(
|
130 |
+
label="Model Name",
|
131 |
+
placeholder="your-username/roberta-cuad-qa",
|
132 |
+
info="This will be your model's name on Hugging Face Hub"
|
133 |
+
)
|
134 |
+
train_btn = gr.Button("π Start Training", variant="primary", size="lg")
|
135 |
+
|
136 |
+
with gr.Column():
|
137 |
+
gr.Markdown("""
|
138 |
+
**Training Details:**
|
139 |
+
- Model: RoBERTa-base + LoRA
|
140 |
+
- Dataset: CUAD (4000 samples β 20K after tokenization)
|
141 |
+
- Time: ~45-75 minutes
|
142 |
+
- GPU: T4 (free)
|
143 |
+
""")
|
144 |
+
|
145 |
+
train_output = gr.Textbox(
|
146 |
+
label="Training Output",
|
147 |
+
lines=25,
|
148 |
+
max_lines=50,
|
149 |
+
show_copy_button=True
|
150 |
+
)
|
151 |
|
152 |
+
train_btn.click(
|
153 |
+
fn=run_training,
|
154 |
+
inputs=model_name_input,
|
155 |
+
outputs=train_output,
|
156 |
+
show_progress=True
|
157 |
+
)
|
158 |
+
|
159 |
+
with gr.TabItem("β¬οΈ Push Existing Model"):
|
160 |
gr.Markdown("""
|
161 |
+
**Push Already Trained Model:**
|
162 |
+
If you've already trained a model and it's saved locally, use this to upload it to Hub.
|
|
|
|
|
|
|
163 |
""")
|
164 |
+
|
165 |
+
with gr.Row():
|
166 |
+
with gr.Column():
|
167 |
+
push_model_name = gr.Textbox(
|
168 |
+
label="Model Name",
|
169 |
+
placeholder="your-username/roberta-cuad-qa",
|
170 |
+
info="Name for your model on Hugging Face Hub"
|
171 |
+
)
|
172 |
+
push_btn = gr.Button("β¬οΈ Push to Hub", variant="secondary", size="lg")
|
173 |
+
|
174 |
+
with gr.Column():
|
175 |
+
gr.Markdown("""
|
176 |
+
**Requirements:**
|
177 |
+
- Model must be trained and saved in ./model_output/
|
178 |
+
- roberta_token must be set in Space secrets
|
179 |
+
- Takes ~30 seconds
|
180 |
+
""")
|
181 |
+
|
182 |
+
push_output = gr.Textbox(
|
183 |
+
label="Push Output",
|
184 |
+
lines=15,
|
185 |
+
show_copy_button=True
|
186 |
+
)
|
187 |
+
|
188 |
+
push_btn.click(
|
189 |
+
fn=push_existing_model,
|
190 |
+
inputs=push_model_name,
|
191 |
+
outputs=push_output,
|
192 |
+
show_progress=True
|
193 |
+
)
|
194 |
|
195 |
gr.Markdown("""
|
196 |
---
|
197 |
+
**Setup Required:**
|
198 |
+
- Set `roberta_token` in Space Settings β Repository secrets
|
199 |
+
- Get your token from: https://huggingface.co/settings/tokens (with Write permissions)
|
200 |
""")
|
201 |
|
202 |
if __name__ == "__main__":
|