Doganbilir commited on
Commit
c9316a4
·
verified ·
1 Parent(s): e6e82ed

Create app.py

Browse files

Turkish English summarizer

Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py - Minimum Uzunluk Ayarı Eklenmiş Versiyon
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ # --- 1. Hugging Face Hub'a Yüklediğimiz Modeli Belirtelim ---
7
+ model_name = "doganbilir/mt5-Turkish-English-Summarizer" # Model adınızın bu olduğundan emin olun
8
+
9
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
10
+
11
+ # --- 2. Modeli Doğrudan Hub'dan Yükle ---
12
+ try:
13
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
14
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(device)
15
+ print("Model ve tokenizer başarıyla yüklendi!")
16
+ except Exception as e:
17
+ print(f"Model yüklenirken hata oluştu: {e}")
18
+ model = None
19
+
20
+ # --- 3. Özetleme Fonksiyonunu Tanımla ---
21
+ def summarize(text, language_choice, min_length):
22
+ if model is None:
23
+ return "Model yüklenemediği için özetleme yapılamıyor. Lütfen Space loglarını kontrol edin."
24
+ if not text or not text.strip():
25
+ return "Lütfen özetlemek için bir metin girin."
26
+
27
+ if language_choice == "Türkçe Metin -> Türkçe Özet":
28
+ prompt = f"summarize: {text}"
29
+ elif language_choice == "İngilizce Metin -> İngilizce Özet":
30
+ prompt = f"summarize: {text}"
31
+ else: # TR -> EN (Deneysel)
32
+ prompt = f"summarize Turkish to English: {text}"
33
+
34
+ try:
35
+ inputs = tokenizer(prompt, return_tensors="pt", max_length=1024, truncation=True).to(device)
36
+
37
+ summary_ids = model.generate(
38
+ input_ids=inputs.input_ids,
39
+ attention_mask=inputs.attention_mask,
40
+ max_new_tokens=150,
41
+ num_beams=5,
42
+ min_new_tokens=int(min_length), # Slider'dan gelen değeri kullan
43
+ early_stopping=True,
44
+ no_repeat_ngram_size=3
45
+ )
46
+
47
+ summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]
48
+ return summary.strip()
49
+ except Exception as e:
50
+ return f"Özetleme sırasında bir hata oluştu: {str(e)}"
51
+
52
+ # --- 4. Gradio Arayüzünü Oluştur ---
53
+ iface = gr.Interface(
54
+ fn=summarize,
55
+ inputs=[
56
+ gr.Textbox(lines=15, placeholder="Özetlemek istediğiniz metni buraya yapıştırın...", label="Metin Girişi"),
57
+ gr.Radio(
58
+ ["Türkçe Metin -> Türkçe Özet", "İngilizce Metin -> İngilizce Özet", "Türkçe Metin -> İngilizce Özet (Deneysel)"],
59
+ label="Özetleme Türü",
60
+ value="Türkçe Metin -> Türkçe Özet"
61
+ ),
62
+ gr.Slider(
63
+ minimum=10,
64
+ maximum=100,
65
+ value=10, # Varsayılan değer (doğal, kısa özet için)
66
+ step=5,
67
+ label="Minimum Özet Uzunluğu (Token)",
68
+ info="Modeli daha uzun özet üretmeye zorlamak için bu değeri artırın."
69
+ )
70
+ ],
71
+ outputs=gr.Textbox(lines=5, label="Oluşturulan Özet"),
72
+ title="Çok Dilli Metin Özetleme Modeli",
73
+ description="Bu demo, Türkçe veya İngilizce metinler için başlık tarzı özetler üretir. Model, `google/mt5-small` temel alınarak LoRA tekniği ile 30,000 Türkçe-İngilizce özet verisi üzerinde fine-tune edilmiştir.",
74
+ examples=[
75
+ ["Türkiye’de yapılan yeni bir araştırma, gençlerin sosyal medyada günde ortalama üç saatten fazla zaman geçirdiğini ortaya koydu. Araştırmaya göre, en çok kullanılan platformların başında Instagram ve TikTok geliyor.", "Türkçe Metin -> Türkçe Özet", 10],
76
+ ["A new study published in the journal Nature has revealed that honeybees can understand the concept of zero.", "İngilizce Metin -> İngilizce Özet", 10]
77
+ ],
78
+ theme="gradio/soft",
79
+ allow_flagging="never"
80
+ )
81
+
82
+ if __name__ == "__main__":
83
+ iface.launch()