Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,12 @@
|
|
| 1 |
-
# ===============================
|
| 2 |
-
# Derma Space: Dataset + Gradio Chatbot
|
| 3 |
-
# ===============================
|
| 4 |
-
|
| 5 |
-
import json
|
| 6 |
-
import random
|
| 7 |
-
import os
|
| 8 |
-
import gradio as gr
|
| 9 |
-
from datasets import load_dataset
|
| 10 |
-
from huggingface_hub import HfApi, login, upload_file
|
| 11 |
-
|
| 12 |
-
# ------------------------
|
| 13 |
-
# 1️⃣ ورود با Secret
|
| 14 |
-
# ------------------------
|
| 15 |
-
hf_token = os.environ["HF_TOKEN"]
|
| 16 |
-
login(token=hf_token)
|
| 17 |
-
|
| 18 |
# ------------------------
|
| 19 |
-
# 2️⃣ ساخت دیتاست ترکیبی
|
| 20 |
# ------------------------
|
| 21 |
def build_dataset():
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 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 |
print("Loading Dermatology QA (Mreeb)...")
|
| 38 |
derma = load_dataset("Mreeb/Dermatology-Question-Answer-Dataset-For-Fine-Tuning")['train']
|
|
@@ -61,7 +35,7 @@ def build_dataset():
|
|
| 61 |
print(f"✅ Dataset saved locally as {output_file} ({len(all_examples)} examples)")
|
| 62 |
|
| 63 |
# آپلود به HF
|
| 64 |
-
repo_id = "username/Derma"
|
| 65 |
api = HfApi()
|
| 66 |
api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
|
| 67 |
upload_file(
|
|
@@ -72,46 +46,3 @@ def build_dataset():
|
|
| 72 |
commit_message="Initial upload of text-based chat dataset"
|
| 73 |
)
|
| 74 |
print(f"✅ Dataset uploaded: https://huggingface.co/datasets/{repo_id}")
|
| 75 |
-
|
| 76 |
-
# ------------------------
|
| 77 |
-
# 3️⃣ چت ساده با Gradio
|
| 78 |
-
# ------------------------
|
| 79 |
-
def simple_chat(user_input):
|
| 80 |
-
# جستجو در دیتاست برای پاسخ نزدیک (ساده)
|
| 81 |
-
with open("derma_chat_mix.jsonl", 'r', encoding='utf-8') as f:
|
| 82 |
-
data = [json.loads(line) for line in f]
|
| 83 |
-
|
| 84 |
-
# جستجوی پاسخ بر اساس کلمات مشترک
|
| 85 |
-
best_match = None
|
| 86 |
-
max_overlap = 0
|
| 87 |
-
for item in data:
|
| 88 |
-
overlap = len(set(user_input.lower().split()) & set(item['context'].lower().split()))
|
| 89 |
-
if overlap > max_overlap:
|
| 90 |
-
max_overlap = overlap
|
| 91 |
-
best_match = item['response']
|
| 92 |
-
|
| 93 |
-
if best_match:
|
| 94 |
-
return best_match
|
| 95 |
-
else:
|
| 96 |
-
return "Sorry, I don't have a good answer for that. Try another question!"
|
| 97 |
-
|
| 98 |
-
# ------------------------
|
| 99 |
-
# 4️⃣ راهاندازی Gradio
|
| 100 |
-
# ------------------------
|
| 101 |
-
iface = gr.Interface(
|
| 102 |
-
fn=simple_chat,
|
| 103 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask about dermatology or chat casually..."),
|
| 104 |
-
outputs=gr.Textbox(label="Derma ChatBot"),
|
| 105 |
-
title="Derma ChatBot",
|
| 106 |
-
description="A simple English chatbot combining general conversation + dermatology QA."
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
# ------------------------
|
| 110 |
-
# 5️⃣ اجرای دیتاست + رابط
|
| 111 |
-
# ------------------------
|
| 112 |
-
if __name__ == "__main__":
|
| 113 |
-
# فقط یک بار دیتاست بساز
|
| 114 |
-
if not os.path.exists("derma_chat_mix.jsonl"):
|
| 115 |
-
build_dataset()
|
| 116 |
-
|
| 117 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ------------------------
|
| 2 |
+
# 2️⃣ ساخت دیتاست ترکیبی (اصلاح شده)
|
| 3 |
# ------------------------
|
| 4 |
def build_dataset():
|
| 5 |
+
# مثال عمومی کوچک
|
| 6 |
+
general_examples = [
|
| 7 |
+
{"domain":"general", "context":"Hello, how are you?", "response":"I'm good, thank you!"},
|
| 8 |
+
{"domain":"general", "context":"What's your name?", "response":"I'm Derma ChatBot."}
|
| 9 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
print("Loading Dermatology QA (Mreeb)...")
|
| 12 |
derma = load_dataset("Mreeb/Dermatology-Question-Answer-Dataset-For-Fine-Tuning")['train']
|
|
|
|
| 35 |
print(f"✅ Dataset saved locally as {output_file} ({len(all_examples)} examples)")
|
| 36 |
|
| 37 |
# آپلود به HF
|
| 38 |
+
repo_id = "username/Derma"
|
| 39 |
api = HfApi()
|
| 40 |
api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
|
| 41 |
upload_file(
|
|
|
|
| 46 |
commit_message="Initial upload of text-based chat dataset"
|
| 47 |
)
|
| 48 |
print(f"✅ Dataset uploaded: https://huggingface.co/datasets/{repo_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|