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|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
TASK_MODELS = {
|
| 6 |
+
"Text Classification": [
|
| 7 |
+
"savasy/bert-base-turkish-text-classification",
|
| 8 |
+
"GosamaIKU/bert-topic-classification-turkish",
|
| 9 |
+
"pamu-tar/bert-turkish-news"
|
| 10 |
+
],
|
| 11 |
+
"Translation": [
|
| 12 |
+
"Helsinki-NLP/opus-mt-tr-en",
|
| 13 |
+
"asafaya/kanarya-2b",
|
| 14 |
+
"boun-tabi-LMG/TURNA"
|
| 15 |
+
],
|
| 16 |
+
"Summarization": [
|
| 17 |
+
"Ali-Akbar/summarizer-tr",
|
| 18 |
+
"ozcangundes/mt5-small-turkish-summarization",
|
| 19 |
+
"RegenAI/umt5-small-turkish-summary"
|
| 20 |
+
],
|
| 21 |
+
"Question Answering": [
|
| 22 |
+
"savasy/bert-base-turkish-squad",
|
| 23 |
+
"Rustamshry/Qwen2.5-3B-Self-Instruct-Turkish",
|
| 24 |
+
"cuneytkaya/fine-tuned-t5-small-turkish-mmlu"
|
| 25 |
+
],
|
| 26 |
+
"Text Generation": [
|
| 27 |
+
"dbmdz/gpt2-turkish",
|
| 28 |
+
"WiroAI/wiroai-turkish-llm-9b",
|
| 29 |
+
"ytu-ce-cosmos/Turkish-Gemma-9b-v0.1"
|
| 30 |
+
],
|
| 31 |
+
"Conversational": [
|
| 32 |
+
"merttuerk/CyberYapayZeka-V1",
|
| 33 |
+
"asafaya/kanarya-2b",
|
| 34 |
+
"mertaydin/phi-turkish"
|
| 35 |
+
],
|
| 36 |
+
"Named Entity Recognition": [
|
| 37 |
+
"savasy/bert-base-turkish-ner-cased",
|
| 38 |
+
"akdeniz27/bert-base-turkish-cased-ner",
|
| 39 |
+
"dbmdz/bert-base-turkish-128k-ner"
|
| 40 |
+
],
|
| 41 |
+
"Sentiment Analysis": [
|
| 42 |
+
"savasy/bert-base-turkish-sentiment-cased",
|
| 43 |
+
"pamu-tar/bert-turkish-news",
|
| 44 |
+
"GosamaIKU/bert-topic-classification-turkish"
|
| 45 |
+
],
|
| 46 |
+
"Sentence Similarity": [
|
| 47 |
+
"sentence-transformers/LaBSE",
|
| 48 |
+
"paraphrase-multilingual-mpnet-base-v2",
|
| 49 |
+
"microsoft/Multilingual-MiniLM-L12-H384"
|
| 50 |
+
],
|
| 51 |
+
"Topic Classification": [
|
| 52 |
+
"GosamaIKU/bert-topic-classification-turkish",
|
| 53 |
+
"savasy/bert-base-turkish-text-classification",
|
| 54 |
+
"pamu-tar/bert-turkish-news"
|
| 55 |
+
],
|
| 56 |
+
"Grammar Correction": [
|
| 57 |
+
"metatextgmbh/t5-base-grammar-correction",
|
| 58 |
+
"microsoft/trocr-base-handwritten",
|
| 59 |
+
"wietsedv/grammar_correction"
|
| 60 |
+
],
|
| 61 |
+
"Intent Detection": [
|
| 62 |
+
"muratkavlak/bert-base-turkish-uncased-intent-detection",
|
| 63 |
+
"dbmdz/bert-base-turkish-cased",
|
| 64 |
+
"savasy/bert-base-turkish-sentiment-cased"
|
| 65 |
+
],
|
| 66 |
+
"Token Classification": [
|
| 67 |
+
"savasy/bert-base-turkish-ner-cased",
|
| 68 |
+
"akdeniz27/bert-base-turkish-cased-ner",
|
| 69 |
+
"dbmdz/bert-base-turkish-128k-ner"
|
| 70 |
+
]
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
def get_random_model(task):
|
| 74 |
+
models = TASK_MODELS.get(task, None)
|
| 75 |
+
if not models:
|
| 76 |
+
return None
|
| 77 |
+
return random.choice(models)
|
| 78 |
+
|
| 79 |
+
def run_task(task, text):
|
| 80 |
+
model_name = get_random_model(task)
|
| 81 |
+
if model_name is None:
|
| 82 |
+
return "Bu görev için model bulunamadı."
|
| 83 |
+
|
| 84 |
+
if task in ["Text Classification", "Sentiment Analysis", "Topic Classification", "Intent Detection"]:
|
| 85 |
+
task_name = "text-classification"
|
| 86 |
+
elif task == "Translation":
|
| 87 |
+
task_name = "translation"
|
| 88 |
+
elif task in ["Summarization", "Grammar Correction"]:
|
| 89 |
+
task_name = "summarization"
|
| 90 |
+
elif task == "Question Answering":
|
| 91 |
+
task_name = "question-answering"
|
| 92 |
+
elif task == "Text Generation":
|
| 93 |
+
task_name = "text-generation"
|
| 94 |
+
elif task == "Conversational":
|
| 95 |
+
task_name = "conversational"
|
| 96 |
+
elif task in ["Named Entity Recognition", "Token Classification"]:
|
| 97 |
+
task_name = "token-classification"
|
| 98 |
+
elif task == "Sentence Similarity":
|
| 99 |
+
task_name = "feature-extraction"
|
| 100 |
+
else:
|
| 101 |
+
task_name = "text-generation"
|
| 102 |
+
|
| 103 |
+
try:
|
| 104 |
+
pipe = pipeline(task_name, model=model_name)
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return f"Model yüklenirken hata: {e}"
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
if task_name == "question-answering":
|
| 110 |
+
context = text
|
| 111 |
+
result = pipe(question=text, context=context)
|
| 112 |
+
return f"📌 Cevap: {result['answer']} (Model: {model_name})"
|
| 113 |
+
elif task_name == "translation":
|
| 114 |
+
result = pipe(text)
|
| 115 |
+
return f"🌐 Çeviri: {result[0]['translation_text']} (Model: {model_name})"
|
| 116 |
+
elif task_name == "summarization":
|
| 117 |
+
result = pipe(text)
|
| 118 |
+
return f"📝 Özet: {result[0]['summary_text']} (Model: {model_name})"
|
| 119 |
+
elif task_name == "text-classification":
|
| 120 |
+
result = pipe(text)
|
| 121 |
+
labels = ", ".join([f"{r['label']} (%{round(r['score']*100, 2)})" for r in result])
|
| 122 |
+
return f"🧠 Sınıflandırma: {labels} (Model: {model_name})"
|
| 123 |
+
elif task_name == "token-classification":
|
| 124 |
+
result = pipe(text)
|
| 125 |
+
entities = ", ".join([f"{r['entity']}:{r['word']}" for r in result])
|
| 126 |
+
return f"🏷️ Varlık Tanıma: {entities} (Model: {model_name})"
|
| 127 |
+
elif task_name == "text-generation":
|
| 128 |
+
result = pipe(text, max_new_tokens=50)
|
| 129 |
+
return f"✍️ Üretim: {result[0]['generated_text']} (Model: {model_name})"
|
| 130 |
+
elif task_name == "conversational":
|
| 131 |
+
result = pipe(text)
|
| 132 |
+
return f"💬 Konuşma: {result} (Model: {model_name})"
|
| 133 |
+
elif task_name == "feature-extraction":
|
| 134 |
+
vec = pipe(text)
|
| 135 |
+
return f"🔍 Özellik vektörü uzunluğu: {len(vec[0][0])} (Model: {model_name})"
|
| 136 |
+
else:
|
| 137 |
+
return "Bu görev için işlem tanımlı değil."
|
| 138 |
+
except Exception as e:
|
| 139 |
+
return f"İşlem sırasında hata: {e}"
|
| 140 |
+
|
| 141 |
+
def gradio_chat(text, task):
|
| 142 |
+
return run_task(task, text)
|
| 143 |
+
|
| 144 |
+
tasks = list(TASK_MODELS.keys())
|
| 145 |
+
|
| 146 |
+
interface = gr.Interface(
|
| 147 |
+
fn=gradio_chat,
|
| 148 |
+
inputs=[gr.Textbox(lines=4, placeholder="Buraya Türkçe metin yazın..."), gr.Dropdown(choices=tasks, label="Görev Seçin")],
|
| 149 |
+
outputs="text",
|
| 150 |
+
title="Türkçe Çok Görevli Süper Chatbot",
|
| 151 |
+
description="Hugging Face üzerindeki en iyi Türkçe modellerden seçmeli çoklu görevli chatbot."
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
interface.launch()
|