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|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
Otomatik Script Üretimi
|
| 6 |
+
-----------------------
|
| 7 |
+
Bu modül, T5 modelini kullanarak verilen bir prompt üzerine otomatik metin üretimi yapar.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import argparse
|
| 11 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, T5ForConditionalGeneration
|
| 12 |
+
import torch
|
| 13 |
+
import os
|
| 14 |
+
import sys
|
| 15 |
+
|
| 16 |
+
# SentencePiece hatası için mesaj fonksiyonu
|
| 17 |
+
def sentencepiece_error_message():
|
| 18 |
+
print("\n" + "=" * 80)
|
| 19 |
+
print("HATA: SentencePiece kurulumu gerekiyor!")
|
| 20 |
+
print("=" * 80)
|
| 21 |
+
print("T5 modeli SentencePiece kütüphanesine ihtiyaç duyar, ancak bu kütüphane kurulu değil.")
|
| 22 |
+
print("\nKurulum seçenekleri:")
|
| 23 |
+
print("1. Windows için:")
|
| 24 |
+
print(" a. Visual C++ Build Tools kurun: https://visualstudio.microsoft.com/visual-cpp-build-tools/")
|
| 25 |
+
print(" b. CMake kurun: https://cmake.org/download/")
|
| 26 |
+
print(" c. Şu komutu çalıştırın: pip install sentencepiece")
|
| 27 |
+
print("\n2. Kolay kurulum (Python 3.8):")
|
| 28 |
+
print(" pip install sentencepiece==0.1.96 --no-build-isolation")
|
| 29 |
+
print("\n3. Alternatif model:")
|
| 30 |
+
print(" Aşağıdaki alternatif modelleri kullanmayı deneyebilirsiniz:")
|
| 31 |
+
print(" - facebook/bart-base")
|
| 32 |
+
print(" - gpt2")
|
| 33 |
+
print(" - EleutherAI/gpt-neo-1.3B")
|
| 34 |
+
print("=" * 80)
|
| 35 |
+
sys.exit(1)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class ScriptGenerator:
|
| 39 |
+
"""Metin üretimi yapan sınıf"""
|
| 40 |
+
|
| 41 |
+
def __init__(self, model_name="google/t5-v1_1-base"):
|
| 42 |
+
"""
|
| 43 |
+
Model ve tokenizer'ı yükler
|
| 44 |
+
|
| 45 |
+
Args:
|
| 46 |
+
model_name (str): Kullanılacak model adı
|
| 47 |
+
"""
|
| 48 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 49 |
+
print(f"Cihaz: {self.device}")
|
| 50 |
+
|
| 51 |
+
print(f"{model_name} yükleniyor...")
|
| 52 |
+
|
| 53 |
+
try:
|
| 54 |
+
# T5 model için AutoTokenizer kullanarak hataları azaltalım
|
| 55 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 56 |
+
|
| 57 |
+
# Model tipine göre uygun modeli yükle
|
| 58 |
+
if "t5" in model_name.lower():
|
| 59 |
+
self.model = T5ForConditionalGeneration.from_pretrained(model_name).to(self.device)
|
| 60 |
+
self.model_type = "t5"
|
| 61 |
+
else:
|
| 62 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(self.device)
|
| 63 |
+
self.model_type = "seq2seq"
|
| 64 |
+
|
| 65 |
+
print("Model başarıyla yüklendi.")
|
| 66 |
+
|
| 67 |
+
except ImportError as e:
|
| 68 |
+
if "sentencepiece" in str(e).lower():
|
| 69 |
+
sentencepiece_error_message()
|
| 70 |
+
else:
|
| 71 |
+
print(f"Model yükleme hatası: {e}")
|
| 72 |
+
sys.exit(1)
|
| 73 |
+
|
| 74 |
+
def generate_text(self, prompt, max_length=200, num_return_sequences=1):
|
| 75 |
+
"""
|
| 76 |
+
Verilen prompt'a göre metin üretir
|
| 77 |
+
|
| 78 |
+
Args:
|
| 79 |
+
prompt (str): Başlangıç metni
|
| 80 |
+
max_length (int): Üretilecek metnin maksimum uzunluğu
|
| 81 |
+
num_return_sequences (int): Üretilecek farklı metin sayısı
|
| 82 |
+
|
| 83 |
+
Returns:
|
| 84 |
+
list: Üretilen metinlerin listesi
|
| 85 |
+
"""
|
| 86 |
+
# Model tipine göre uygun input formatını hazırla
|
| 87 |
+
if self.model_type == "t5":
|
| 88 |
+
input_text = f"generate text: {prompt}"
|
| 89 |
+
else:
|
| 90 |
+
input_text = prompt
|
| 91 |
+
|
| 92 |
+
# Tokenize ve encode
|
| 93 |
+
input_ids = self.tokenizer.encode(input_text, return_tensors="pt").to(self.device)
|
| 94 |
+
|
| 95 |
+
# Metni üret
|
| 96 |
+
outputs = self.model.generate(
|
| 97 |
+
input_ids=input_ids,
|
| 98 |
+
max_length=max_length,
|
| 99 |
+
num_return_sequences=num_return_sequences,
|
| 100 |
+
do_sample=True,
|
| 101 |
+
temperature=0.8, # Yaratıcılık seviyesi (0.0-1.0)
|
| 102 |
+
top_k=50,
|
| 103 |
+
top_p=0.95,
|
| 104 |
+
repetition_penalty=1.2, # Tekrarları cezalandır
|
| 105 |
+
early_stopping=True
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Decode ve sonuçları döndür
|
| 109 |
+
generated_texts = [self.tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
| 110 |
+
return generated_texts
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def main():
|
| 114 |
+
"""Ana fonksiyon"""
|
| 115 |
+
parser = argparse.ArgumentParser(description="Metin üretimi")
|
| 116 |
+
parser.add_argument("--prompt", type=str, default="5 ilginç kedi gerçeği:", help="Başlangıç metni")
|
| 117 |
+
parser.add_argument("--model", type=str, default="google/t5-v1_1-base",
|
| 118 |
+
help="Kullanılacak model adı (t5, gpt2, bart-base vb.)")
|
| 119 |
+
parser.add_argument("--max_length", type=int, default=200, help="Üretilecek metnin maksimum uzunluğu")
|
| 120 |
+
parser.add_argument("--num_sequences", type=int, default=1, help="Üretilecek farklı metin sayısı")
|
| 121 |
+
parser.add_argument("--alternative", action="store_true", help="SentencePiece sorunu varsa alternatif model kullan")
|
| 122 |
+
|
| 123 |
+
args = parser.parse_args()
|
| 124 |
+
|
| 125 |
+
# SentencePiece problemi varsa ve alternatif istendiyse, alternatif modeli kullan
|
| 126 |
+
if args.alternative:
|
| 127 |
+
print("Alternatif model (facebook/bart-base) kullanılıyor...")
|
| 128 |
+
args.model = "facebook/bart-base"
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
# Script generator'ı oluştur
|
| 132 |
+
generator = ScriptGenerator(model_name=args.model)
|
| 133 |
+
|
| 134 |
+
# Metni üret
|
| 135 |
+
generated_texts = generator.generate_text(
|
| 136 |
+
prompt=args.prompt,
|
| 137 |
+
max_length=args.max_length,
|
| 138 |
+
num_return_sequences=args.num_sequences
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
# Sonuçları yazdır
|
| 142 |
+
print("\n--- Üretilen Metinler ---")
|
| 143 |
+
for i, text in enumerate(generated_texts, 1):
|
| 144 |
+
print(f"\n[Metin {i}]\n{text}")
|
| 145 |
+
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"\nHata: {e}")
|
| 148 |
+
print("\nİpucu: SentencePiece sorunu yaşıyorsanız --alternative bayrağını kullanarak deneyin:")
|
| 149 |
+
print("python text_generator.py --alternative --prompt \"Bir film senaryosu için giriş sahnesi:\"")
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
if __name__ == "__main__":
|
| 153 |
+
main()
|