Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -74,38 +74,46 @@ def push_existing_model(model_name):
|
|
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 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
83 |
|
84 |
# Push to Hub
|
85 |
model_name = model_name.strip()
|
86 |
output += f"β¬οΈ Pushing model to Hub: {model_name}\n"
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
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 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
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:
|
@@ -113,23 +121,30 @@ with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
|
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: `
|
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="
|
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 |
|
@@ -137,9 +152,10 @@ with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
|
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(
|
@@ -166,8 +182,9 @@ with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
|
166 |
with gr.Column():
|
167 |
push_model_name = gr.Textbox(
|
168 |
label="Model Name",
|
169 |
-
placeholder="
|
170 |
-
info="Name for your model on Hugging Face Hub"
|
|
|
171 |
)
|
172 |
push_btn = gr.Button("β¬οΈ Push to Hub", variant="secondary", size="lg")
|
173 |
|
@@ -194,9 +211,10 @@ with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
|
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__":
|
|
|
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 |
+
# FIXED: Better error handling for model loading
|
79 |
+
try:
|
80 |
+
output += "π Loading trained model from ./model_output...\n"
|
81 |
+
model = AutoPeftModelForQuestionAnswering.from_pretrained("./model_output")
|
82 |
+
tokenizer = AutoTokenizer.from_pretrained("./model_output")
|
83 |
+
output += "β
Model loaded successfully!\n\n"
|
84 |
+
except Exception as e:
|
85 |
+
return output + f"β Failed to load model: {str(e)}\nMake sure the model was trained successfully."
|
86 |
|
87 |
# Push to Hub
|
88 |
model_name = model_name.strip()
|
89 |
output += f"β¬οΈ Pushing model to Hub: {model_name}\n"
|
90 |
|
91 |
+
try:
|
92 |
+
model.push_to_hub(model_name, private=False)
|
93 |
+
tokenizer.push_to_hub(model_name, private=False)
|
94 |
+
output += f"π SUCCESS! Model pushed to: https://huggingface.co/{model_name}\n"
|
95 |
+
except Exception as e:
|
96 |
+
return output + f"β Failed to push model: {str(e)}\nCheck if model name already exists or token has write permissions."
|
97 |
|
98 |
# Also push training info if it exists
|
99 |
training_info_path = "./model_output/training_info.json"
|
100 |
if os.path.exists(training_info_path):
|
101 |
+
try:
|
102 |
+
from huggingface_hub import upload_file
|
103 |
+
upload_file(
|
104 |
+
path_or_fileobj=training_info_path,
|
105 |
+
path_in_repo="training_info.json",
|
106 |
+
repo_id=model_name,
|
107 |
+
repo_type="model"
|
108 |
+
)
|
109 |
+
output += "π Training info also uploaded!\n"
|
110 |
+
except Exception as e:
|
111 |
+
output += f"β οΈ Training info upload failed: {str(e)}\n"
|
112 |
|
113 |
return output
|
114 |
|
115 |
except Exception as e:
|
116 |
+
return f"β Error: {str(e)}\n\nCommon issues:\n- Invalid token\n- Model name already exists\n- Network issues\n- Token lacks write permissions"
|
117 |
|
118 |
# Gradio Interface
|
119 |
with gr.Blocks(title="RoBERTa CUAD Trainer") as demo:
|
|
|
121 |
# π€ RoBERTa CUAD Question Answering Trainer
|
122 |
|
123 |
Train a RoBERTa model with LoRA on CUAD dataset OR push an existing trained model to Hub.
|
124 |
+
|
125 |
+
**For AvocadoMuffin:** Your models will be saved as `AvocadoMuffin/your-model-name`
|
126 |
""")
|
127 |
|
128 |
with gr.Tabs():
|
129 |
with gr.TabItem("π Train New Model"):
|
130 |
gr.Markdown("""
|
131 |
**Instructions:**
|
132 |
+
1. Enter your desired model name (format: `AvocadoMuffin/model-name`)
|
133 |
2. Click "Start Training"
|
134 |
3. Wait ~45-75 minutes for training to complete
|
135 |
+
|
136 |
+
**Example model names:**
|
137 |
+
- `AvocadoMuffin/roberta-cuad-qa`
|
138 |
+
- `AvocadoMuffin/roberta-legal-qa-v1`
|
139 |
""")
|
140 |
|
141 |
with gr.Row():
|
142 |
with gr.Column():
|
143 |
model_name_input = gr.Textbox(
|
144 |
label="Model Name",
|
145 |
+
placeholder="AvocadoMuffin/roberta-cuad-qa",
|
146 |
+
info="This will be your model's name on Hugging Face Hub",
|
147 |
+
value="AvocadoMuffin/"
|
148 |
)
|
149 |
train_btn = gr.Button("π Start Training", variant="primary", size="lg")
|
150 |
|
|
|
152 |
gr.Markdown("""
|
153 |
**Training Details:**
|
154 |
- Model: RoBERTa-base + LoRA
|
155 |
+
- Dataset: CUAD (4000 samples β ~20K after tokenization)
|
156 |
- Time: ~45-75 minutes
|
157 |
+
- GPU: T4 (free tier)
|
158 |
+
- Will auto-push to your HF profile
|
159 |
""")
|
160 |
|
161 |
train_output = gr.Textbox(
|
|
|
182 |
with gr.Column():
|
183 |
push_model_name = gr.Textbox(
|
184 |
label="Model Name",
|
185 |
+
placeholder="AvocadoMuffin/roberta-cuad-qa",
|
186 |
+
info="Name for your model on Hugging Face Hub",
|
187 |
+
value="AvocadoMuffin/"
|
188 |
)
|
189 |
push_btn = gr.Button("β¬οΈ Push to Hub", variant="secondary", size="lg")
|
190 |
|
|
|
211 |
|
212 |
gr.Markdown("""
|
213 |
---
|
214 |
+
**Setup Required for AvocadoMuffin:**
|
215 |
+
- β
Set `roberta_token` in Space Settings β Repository secrets
|
216 |
+
- β
Get your token from: https://huggingface.co/settings/tokens (with Write permissions)
|
217 |
+
- β
Your trained models will appear at: `https://huggingface.co/AvocadoMuffin/model-name`
|
218 |
""")
|
219 |
|
220 |
if __name__ == "__main__":
|