Upload app.py
Browse files
app.py
CHANGED
@@ -342,59 +342,67 @@ def normalize_text(text):
|
|
342 |
return text
|
343 |
|
344 |
def generate_turkish_text(prompt, max_length=300):
|
345 |
-
"""Türkçe metin üretme fonksiyonu -
|
346 |
try:
|
|
|
347 |
inputs = tokenizer(prompt, return_tensors="pt")
|
348 |
|
349 |
-
#
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
|
372 |
# Prompt'tan sonraki kısmı al
|
373 |
-
if prompt in
|
374 |
-
|
|
|
|
|
375 |
|
376 |
# Temizlik işlemleri
|
377 |
-
|
378 |
|
379 |
-
# Yanıt
|
380 |
-
|
|
|
|
|
|
|
|
|
381 |
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
if len(best_response) < 20 and best_score < 0.5:
|
388 |
-
print("Yanıt kalitesi düşük, tekrar deneniyor...")
|
389 |
-
# Farklı sıcaklık parametresi ile tekrar dene
|
390 |
-
return generate_turkish_text(prompt + " Lütfen detaylı yanıt ver.", max_length)
|
391 |
-
|
392 |
-
print(f"En iyi yanıt kalite skoru: {best_score:.2f}")
|
393 |
-
return best_response
|
394 |
|
395 |
except Exception as e:
|
396 |
-
print(f"Metin üretme hatası: {str(e)}")
|
397 |
-
|
|
|
|
|
398 |
|
399 |
def clean_response(text):
|
400 |
"""Yanıtı temizle ve kalitesini artır"""
|
|
|
342 |
return text
|
343 |
|
344 |
def generate_turkish_text(prompt, max_length=300):
|
345 |
+
"""Türkçe metin üretme fonksiyonu - daha kararlı ve optimize edilmiş versiyon"""
|
346 |
try:
|
347 |
+
print(f"Prompt: {prompt[:100]}...")
|
348 |
inputs = tokenizer(prompt, return_tensors="pt")
|
349 |
|
350 |
+
# Daha basit ve kararlı parametreler kullan
|
351 |
+
try:
|
352 |
+
output = text_model.generate(
|
353 |
+
inputs["input_ids"],
|
354 |
+
max_length=max_length,
|
355 |
+
num_return_sequences=1, # Performans için tek yanıt üret
|
356 |
+
do_sample=True,
|
357 |
+
temperature=0.7,
|
358 |
+
top_p=0.92,
|
359 |
+
top_k=50,
|
360 |
+
repetition_penalty=1.2,
|
361 |
+
no_repeat_ngram_size=2,
|
362 |
+
pad_token_id=tokenizer.eos_token_id
|
363 |
+
)
|
364 |
+
except RuntimeError as e:
|
365 |
+
print(f"Model üretme hatası: {str(e)}")
|
366 |
+
# Daha basit parametrelerle tekrar dene
|
367 |
+
output = text_model.generate(
|
368 |
+
inputs["input_ids"],
|
369 |
+
max_length=max_length,
|
370 |
+
do_sample=True,
|
371 |
+
temperature=0.7,
|
372 |
+
pad_token_id=tokenizer.eos_token_id
|
373 |
+
)
|
374 |
+
|
375 |
+
# Yanıt metni oluştur
|
376 |
+
try:
|
377 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
378 |
|
379 |
# Prompt'tan sonraki kısmı al
|
380 |
+
if prompt in response:
|
381 |
+
response = response[len(prompt):].strip()
|
382 |
+
|
383 |
+
print(f"Ham yanıt: {response[:50]}...")
|
384 |
|
385 |
# Temizlik işlemleri
|
386 |
+
response = clean_response(response)
|
387 |
|
388 |
+
# Yanıt boşsa veya çok kısaysa
|
389 |
+
if not response or len(response) < 10:
|
390 |
+
# Yedek bir yanıt oluştur
|
391 |
+
backup_response = "Bu konuda seninle konuşmak güzel. Daha fazla detay paylaşır mısın?"
|
392 |
+
print("Yanıt çok kısa, yedek yanıt kullanılıyor")
|
393 |
+
return backup_response
|
394 |
|
395 |
+
return response
|
396 |
+
|
397 |
+
except Exception as e:
|
398 |
+
print(f"Yanıt işleme hatası: {str(e)}")
|
399 |
+
return "Nasıl hissettiğini anlıyorum. Bu durumda nasıl yardımcı olabilirim?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
|
401 |
except Exception as e:
|
402 |
+
print(f"Metin üretme genel hatası: {str(e)}")
|
403 |
+
import traceback
|
404 |
+
print(f"Hata detayı: {traceback.format_exc()}")
|
405 |
+
return "Şu an düşüncelerimi toparlıyorum. Biraz daha anlatır mısın?"
|
406 |
|
407 |
def clean_response(text):
|
408 |
"""Yanıtı temizle ve kalitesini artır"""
|