Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# ------------------------
|
| 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."}
|
|
@@ -13,7 +32,7 @@ def build_dataset():
|
|
| 13 |
derma_examples = [{"domain":"dermatology","context":item['question'],"response":item['answer']}
|
| 14 |
for item in derma]
|
| 15 |
|
| 16 |
-
print("Loading MedQuAD...")
|
| 17 |
medquad = load_dataset("pythonafroz/MedQuAD")['train']
|
| 18 |
derma_keywords = ["skin", "eczema", "psoriasis", "dermatitis", "melanoma", "acne", "rash"]
|
| 19 |
medquad_derma = [{"domain":"dermatology","context":item['question'],"response":item['answer']}
|
|
@@ -35,7 +54,7 @@ def build_dataset():
|
|
| 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,3 +65,43 @@ def build_dataset():
|
|
| 46 |
commit_message="Initial upload of text-based chat dataset"
|
| 47 |
)
|
| 48 |
print(f"✅ Dataset uploaded: https://huggingface.co/datasets/{repo_id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.get("HF_TOKEN", None)
|
| 16 |
+
if hf_token is None:
|
| 17 |
+
raise ValueError("HF_TOKEN not found in Secrets. Please add it in Space settings.")
|
| 18 |
+
login(token=hf_token)
|
| 19 |
+
|
| 20 |
+
# ------------------------
|
| 21 |
+
# 2️⃣ ساخت دیتاست ترکیبی
|
| 22 |
# ------------------------
|
| 23 |
def build_dataset():
|
| 24 |
+
print("Creating a small general dataset...")
|
| 25 |
general_examples = [
|
| 26 |
{"domain":"general", "context":"Hello, how are you?", "response":"I'm good, thank you!"},
|
| 27 |
{"domain":"general", "context":"What's your name?", "response":"I'm Derma ChatBot."}
|
|
|
|
| 32 |
derma_examples = [{"domain":"dermatology","context":item['question'],"response":item['answer']}
|
| 33 |
for item in derma]
|
| 34 |
|
| 35 |
+
print("Loading MedQuAD subset...")
|
| 36 |
medquad = load_dataset("pythonafroz/MedQuAD")['train']
|
| 37 |
derma_keywords = ["skin", "eczema", "psoriasis", "dermatitis", "melanoma", "acne", "rash"]
|
| 38 |
medquad_derma = [{"domain":"dermatology","context":item['question'],"response":item['answer']}
|
|
|
|
| 54 |
print(f"✅ Dataset saved locally as {output_file} ({len(all_examples)} examples)")
|
| 55 |
|
| 56 |
# آپلود به HF
|
| 57 |
+
repo_id = "username/Derma" # تغییر بده به نام کاربری خودت
|
| 58 |
api = HfApi()
|
| 59 |
api.create_repo(repo_id=repo_id, repo_type="dataset", exist_ok=True)
|
| 60 |
upload_file(
|
|
|
|
| 65 |
commit_message="Initial upload of text-based chat dataset"
|
| 66 |
)
|
| 67 |
print(f"✅ Dataset uploaded: https://huggingface.co/datasets/{repo_id}")
|
| 68 |
+
|
| 69 |
+
# ------------------------
|
| 70 |
+
# 3️⃣ چت ساده با Gradio
|
| 71 |
+
# ------------------------
|
| 72 |
+
def simple_chat(user_input):
|
| 73 |
+
# جستجو در دیتاست برای پاسخ نزدیک (ساده)
|
| 74 |
+
with open("derma_chat_mix.jsonl", 'r', encoding='utf-8') as f:
|
| 75 |
+
data = [json.loads(line) for line in f]
|
| 76 |
+
|
| 77 |
+
best_match = None
|
| 78 |
+
max_overlap = 0
|
| 79 |
+
for item in data:
|
| 80 |
+
overlap = len(set(user_input.lower().split()) & set(item['context'].lower().split()))
|
| 81 |
+
if overlap > max_overlap:
|
| 82 |
+
max_overlap = overlap
|
| 83 |
+
best_match = item['response']
|
| 84 |
+
|
| 85 |
+
if best_match:
|
| 86 |
+
return best_match
|
| 87 |
+
else:
|
| 88 |
+
return "Sorry, I don't have a good answer for that. Try another question!"
|
| 89 |
+
|
| 90 |
+
# ------------------------
|
| 91 |
+
# 4️⃣ راهاندازی Gradio
|
| 92 |
+
# ------------------------
|
| 93 |
+
iface = gr.Interface(
|
| 94 |
+
fn=simple_chat,
|
| 95 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask about dermatology or chat casually..."),
|
| 96 |
+
outputs=gr.Textbox(label="Derma ChatBot"),
|
| 97 |
+
title="Derma ChatBot",
|
| 98 |
+
description="A simple English chatbot combining general conversation + dermatology QA."
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
# ------------------------
|
| 102 |
+
# 5️⃣ اجرای دیتاست + رابط
|
| 103 |
+
# ------------------------
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
if not os.path.exists("derma_chat_mix.jsonl"):
|
| 106 |
+
build_dataset()
|
| 107 |
+
iface.launch()
|