Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,87 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ===============================
|
| 2 |
+
# Derma Space: Dataset Prep & HF Upload
|
| 3 |
+
# ===============================
|
| 4 |
+
|
| 5 |
+
!pip install datasets huggingface_hub --quiet
|
| 6 |
+
|
| 7 |
+
import json
|
| 8 |
+
import random
|
| 9 |
+
import os
|
| 10 |
+
from datasets import load_dataset
|
| 11 |
+
from huggingface_hub import HfApi, login, upload_file
|
| 12 |
+
|
| 13 |
+
# ------------------------
|
| 14 |
+
# 1️⃣ ورود با Secret
|
| 15 |
+
# ------------------------
|
| 16 |
+
hf_token = os.environ["HF_TOKEN"] # توکن باید در Secrets Space اضافه شده باشد
|
| 17 |
+
login(token=hf_token)
|
| 18 |
+
|
| 19 |
+
# ------------------------
|
| 20 |
+
# 2️⃣ بارگذاری دیتاست عمومی
|
| 21 |
+
# ------------------------
|
| 22 |
+
print("Loading DailyDialog...")
|
| 23 |
+
dd = load_dataset("daily_dialog")['train']
|
| 24 |
+
dd_examples = [{"domain":"general","context":conv[i],"response":conv[i+1]}
|
| 25 |
+
for conv in dd['dialog'] for i in range(len(conv)-1)]
|
| 26 |
+
|
| 27 |
+
print("Loading Persona-Chat...")
|
| 28 |
+
pc = load_dataset("persona_chat", "self_original")['train']
|
| 29 |
+
pc_examples = [{"domain":"general","context":conv['utterances'][i]['text'],
|
| 30 |
+
"response":conv['utterances'][i+1]['text']}
|
| 31 |
+
for conv in pc for i in range(len(conv['utterances'])-1)]
|
| 32 |
+
|
| 33 |
+
general_examples = dd_examples + pc_examples
|
| 34 |
+
random.shuffle(general_examples)
|
| 35 |
+
general_examples = general_examples[:5000] # نمونهگیری
|
| 36 |
+
|
| 37 |
+
# ------------------------
|
| 38 |
+
# 3️⃣ دیتاست تخصصی درماتولوژی
|
| 39 |
+
# ------------------------
|
| 40 |
+
print("Loading Dermatology QA (Mreeb)...")
|
| 41 |
+
derma = load_dataset("Mreeb/Dermatology-Question-Answer-Dataset-For-Fine-Tuning")['train']
|
| 42 |
+
derma_examples = [{"domain":"dermatology","context":item['question'],"response":item['answer']}
|
| 43 |
+
for item in derma]
|
| 44 |
+
|
| 45 |
+
print("Loading MedQuAD...")
|
| 46 |
+
medquad = load_dataset("pythonafroz/MedQuAD")['train']
|
| 47 |
+
derma_keywords = ["skin", "eczema", "psoriasis", "dermatitis", "melanoma", "acne", "rash"]
|
| 48 |
+
medquad_derma = [{"domain":"dermatology","context":item['question'],"response":item['answer']}
|
| 49 |
+
for item in medquad if any(k in item['question'].lower() for k in derma_keywords)]
|
| 50 |
+
random.shuffle(medquad_derma)
|
| 51 |
+
medquad_derma = medquad_derma[:500]
|
| 52 |
+
|
| 53 |
+
dermatology_examples = derma_examples + medquad_derma
|
| 54 |
+
random.shuffle(dermatology_examples)
|
| 55 |
+
|
| 56 |
+
# ------------------------
|
| 57 |
+
# 4️⃣ ترکیب نهایی
|
| 58 |
+
# ------------------------
|
| 59 |
+
all_examples = general_examples + dermatology_examples
|
| 60 |
+
random.shuffle(all_examples)
|
| 61 |
+
|
| 62 |
+
# ------------------------
|
| 63 |
+
# 5️⃣ ذخیره به JSONL
|
| 64 |
+
# ------------------------
|
| 65 |
+
output_file = "derma_chat_mix.jsonl"
|
| 66 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 67 |
+
for ex in all_examples:
|
| 68 |
+
f.write(json.dumps(ex, ensure_ascii=False) + "\n")
|
| 69 |
+
|
| 70 |
+
print(f"✅ Dataset saved locally as {output_file} ({len(all_examples)} examples)")
|
| 71 |
+
|
| 72 |
+
# ------------------------
|
| 73 |
+
# 6️⃣ آپلود به Hugging Face Hub
|
| 74 |
+
# ------------------------
|
| 75 |
+
repo_id = "username/Derma" # نام کاربری خودت + نام Space
|
| 76 |
+
api = HfApi()
|
| 77 |
+
api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
|
| 78 |
+
|
| 79 |
+
upload_file(
|
| 80 |
+
path_or_fileobj=output_file,
|
| 81 |
+
path_in_repo=output_file,
|
| 82 |
+
repo_id=repo_id,
|
| 83 |
+
repo_type="dataset",
|
| 84 |
+
commit_message="Initial upload of text-based chat dataset"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
print(f"✅ Dataset uploaded to Hugging Face Hub: https://huggingface.co/datasets/{repo_id}")
|