AhmedBenElsayedBenAhmed commited on
Commit
faa6d61
·
1 Parent(s): 89a35b8

Merge branch 'اسم-الفرع-الجديد' into main

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -203,14 +203,20 @@ def train():
203
  epochs = 1
204
  batch_size = 1
205
 
206
- # 1️⃣ JSON request
207
  if request.is_json:
208
  body = request.get_json()
209
- data = body.get("data", [])
210
- epochs = int(body.get("epochs", 1))
211
- batch_size = int(body.get("batch_size", 1))
 
 
 
 
 
 
212
 
213
- # 2️⃣ form-data file upload
214
  else:
215
  file = request.files.get("file")
216
  if file:
@@ -220,7 +226,7 @@ def train():
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:
@@ -231,25 +237,22 @@ def train():
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:
@@ -298,6 +301,7 @@ def train():
298
  except Exception as e:
299
  return jsonify({"error": str(e)})
300
 
 
301
  # ===============================
302
  # 8️⃣ تشغيل Flask
303
  # ===============================
 
203
  epochs = 1
204
  batch_size = 1
205
 
206
+ # 1️⃣ JSON مباشر
207
  if request.is_json:
208
  body = request.get_json()
209
+ if isinstance(body, list):
210
+ data = body
211
+ elif isinstance(body, dict) and "data" in body:
212
+ data = body["data"]
213
+ else:
214
+ return jsonify({"error": "JSON format not recognized"}), 400
215
+
216
+ epochs = int(body.get("epochs", 1)) if isinstance(body, dict) else 1
217
+ batch_size = int(body.get("batch_size", 1)) if isinstance(body, dict) else 1
218
 
219
+ # 2️⃣ form-data ملف
220
  else:
221
  file = request.files.get("file")
222
  if file:
 
226
  else:
227
  return jsonify({"error": "No file uploaded or JSON provided"}), 400
228
 
229
+ # دمج وإزالة التكرارات حسب محتوى الرسائل
230
  old_data = []
231
  if os.path.exists(DATASET_FILE):
232
  with open(DATASET_FILE, "r", encoding="utf-8") as f:
 
237
 
238
  combined = old_data + data
239
  unique_data, seen_texts = [], set()
 
240
  for item in combined:
 
241
  if "messages" in item:
242
  text_key = "\n".join([f"{m['role'].strip().lower()}:{m['content'].strip()}" for m in item["messages"]])
243
  elif "text" in item:
244
  text_key = item["text"].strip()
245
  else:
246
+ continue
 
247
  if text_key not in seen_texts:
248
  seen_texts.add(text_key)
249
  unique_data.append(item)
250
 
251
+ # الحفظ
252
  with open(DATASET_FILE, "w", encoding="utf-8") as f:
253
  json.dump(unique_data, f, ensure_ascii=False, indent=2)
254
 
255
+ # تجهيز Dataset للتدريب
256
  formatted = []
257
  for item in unique_data:
258
  if "messages" in item:
 
301
  except Exception as e:
302
  return jsonify({"error": str(e)})
303
 
304
+
305
  # ===============================
306
  # 8️⃣ تشغيل Flask
307
  # ===============================