Spaces:
Sleeping
Sleeping
Commit ·
89a35b8
1
Parent(s): 3839595
Merge branch 'اسم-الفرع-الجديد' into main
Browse files
app.py
CHANGED
|
@@ -218,10 +218,9 @@ def train():
|
|
| 218 |
epochs = int(request.form.get("epochs", 1))
|
| 219 |
batch_size = int(request.form.get("batch_size", 1))
|
| 220 |
else:
|
| 221 |
-
# لو مفيش file ولا JSON → خطأ
|
| 222 |
return jsonify({"error": "No file uploaded or JSON provided"}), 400
|
| 223 |
|
| 224 |
-
#
|
| 225 |
old_data = []
|
| 226 |
if os.path.exists(DATASET_FILE):
|
| 227 |
with open(DATASET_FILE, "r", encoding="utf-8") as f:
|
|
@@ -231,18 +230,26 @@ def train():
|
|
| 231 |
old_data = []
|
| 232 |
|
| 233 |
combined = old_data + data
|
| 234 |
-
unique_data,
|
|
|
|
| 235 |
for item in combined:
|
| 236 |
-
|
| 237 |
-
if
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
unique_data.append(item)
|
| 240 |
|
| 241 |
-
#
|
| 242 |
with open(DATASET_FILE, "w", encoding="utf-8") as f:
|
| 243 |
json.dump(unique_data, f, ensure_ascii=False, indent=2)
|
| 244 |
|
| 245 |
-
#
|
| 246 |
formatted = []
|
| 247 |
for item in unique_data:
|
| 248 |
if "messages" in item:
|
|
@@ -272,6 +279,7 @@ def train():
|
|
| 272 |
|
| 273 |
trainer = Trainer(model=model, args=training_args, train_dataset=tokenized_dataset)
|
| 274 |
|
|
|
|
| 275 |
def run_training():
|
| 276 |
print("🔄 Training started...")
|
| 277 |
trainer.train()
|
|
@@ -280,7 +288,12 @@ def train():
|
|
| 280 |
|
| 281 |
threading.Thread(target=run_training, daemon=True).start()
|
| 282 |
|
| 283 |
-
return jsonify({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
except Exception as e:
|
| 286 |
return jsonify({"error": str(e)})
|
|
|
|
| 218 |
epochs = int(request.form.get("epochs", 1))
|
| 219 |
batch_size = int(request.form.get("batch_size", 1))
|
| 220 |
else:
|
|
|
|
| 221 |
return jsonify({"error": "No file uploaded or JSON provided"}), 400
|
| 222 |
|
| 223 |
+
# دمج وإزالة التكرارات بناءً على محتوى الرسائل فقط
|
| 224 |
old_data = []
|
| 225 |
if os.path.exists(DATASET_FILE):
|
| 226 |
with open(DATASET_FILE, "r", encoding="utf-8") as f:
|
|
|
|
| 230 |
old_data = []
|
| 231 |
|
| 232 |
combined = old_data + data
|
| 233 |
+
unique_data, seen_texts = [], set()
|
| 234 |
+
|
| 235 |
for item in combined:
|
| 236 |
+
# نحول محتوى الرسائل فقط لسلسلة نصية لتحديد التكرار
|
| 237 |
+
if "messages" in item:
|
| 238 |
+
text_key = "\n".join([f"{m['role'].strip().lower()}:{m['content'].strip()}" for m in item["messages"]])
|
| 239 |
+
elif "text" in item:
|
| 240 |
+
text_key = item["text"].strip()
|
| 241 |
+
else:
|
| 242 |
+
continue # تجاهل أي item فارغ أو غير صالح
|
| 243 |
+
|
| 244 |
+
if text_key not in seen_texts:
|
| 245 |
+
seen_texts.add(text_key)
|
| 246 |
unique_data.append(item)
|
| 247 |
|
| 248 |
+
# الحفظ بعد إزالة التكرارات
|
| 249 |
with open(DATASET_FILE, "w", encoding="utf-8") as f:
|
| 250 |
json.dump(unique_data, f, ensure_ascii=False, indent=2)
|
| 251 |
|
| 252 |
+
# تجهيز dataset للتدريب
|
| 253 |
formatted = []
|
| 254 |
for item in unique_data:
|
| 255 |
if "messages" in item:
|
|
|
|
| 279 |
|
| 280 |
trainer = Trainer(model=model, args=training_args, train_dataset=tokenized_dataset)
|
| 281 |
|
| 282 |
+
# تشغيل التدريب في Thread منفصل
|
| 283 |
def run_training():
|
| 284 |
print("🔄 Training started...")
|
| 285 |
trainer.train()
|
|
|
|
| 288 |
|
| 289 |
threading.Thread(target=run_training, daemon=True).start()
|
| 290 |
|
| 291 |
+
return jsonify({
|
| 292 |
+
"status": "training started",
|
| 293 |
+
"epochs": epochs,
|
| 294 |
+
"batch_size": batch_size,
|
| 295 |
+
"records": len(unique_data)
|
| 296 |
+
})
|
| 297 |
|
| 298 |
except Exception as e:
|
| 299 |
return jsonify({"error": str(e)})
|