userdotcs commited on
Commit
306ae96
·
verified ·
1 Parent(s): e1719ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -21
app.py CHANGED
@@ -1,54 +1,53 @@
1
  import torch
2
  import gradio as gr
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from peft import PeftModel
5
 
6
- # --- Model ve Tokenizer Yükleme ---
7
  base_model_name = "unsloth/gpt-oss-20b"
8
  adapter_model_name = "userdotcs/gpt-oss-20b-turkish-correction-adapter"
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- print(device)
11
 
12
- print("Model yükleniyor...")
13
  tokenizer = AutoTokenizer.from_pretrained(base_model_name)
 
 
14
  base_model = AutoModelForCausalLM.from_pretrained(
15
  base_model_name,
16
- device_map="auto",
17
- torch_dtype=torch.float16
18
  )
 
19
  model = PeftModel.from_pretrained(base_model, adapter_model_name)
20
  model.eval()
21
 
22
- # --- Düzeltme Fonksiyonu (API Uyumlu) ---
 
23
  def fix_text(input_text):
24
  if not input_text or input_text.strip() == "":
25
  return ""
26
 
27
- # Senin istediğin komut formatı
 
 
28
  formatted_prompt = f"Fix typos in the text:\n{input_text}"
29
-
30
- messages = [
31
- {"role": "user", "content": formatted_prompt},
32
- ]
33
 
34
- # Tokenize
35
  inputs = tokenizer.apply_chat_template(
36
  messages,
37
  add_generation_prompt=True,
38
  return_tensors="pt",
39
  return_dict=True,
40
  reasoning_effort="medium"
41
- ).to(device)
42
 
43
- # Üretim (Streamer yok, doğrudan çıktı alıyoruz)
44
  with torch.no_grad():
45
  outputs = model.generate(
46
  **inputs,
47
- max_new_tokens=2048,
48
  pad_token_id=tokenizer.eos_token_id
49
  )
50
 
51
- # Sadece yeni üretilen kısmı decode et (input tokenlarını atla)
52
  input_length = inputs.input_ids.shape[1]
53
  response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
54
 
@@ -57,9 +56,11 @@ def fix_text(input_text):
57
  # --- Gradio Arayüzü ---
58
  demo = gr.Interface(
59
  fn=fix_text,
60
- inputs=gr.Textbox(label="Girdi", placeholder="Metni buraya yazın..."),
61
- outputs=gr.Textbox(label="Sonuç"),
62
- allow_flagging="never"
 
63
  )
64
 
65
- demo.launch()
 
 
1
  import torch
2
  import gradio as gr
3
+ import spaces # ZeroGPU için gerekli
4
  from transformers import AutoModelForCausalLM, AutoTokenizer
5
  from peft import PeftModel
6
 
7
+ # --- Model Ayarları ---
8
  base_model_name = "unsloth/gpt-oss-20b"
9
  adapter_model_name = "userdotcs/gpt-oss-20b-turkish-correction-adapter"
 
 
10
 
11
+ print("Model ve Tokenizer CPU üzerinde hazırlanıyor...")
12
  tokenizer = AutoTokenizer.from_pretrained(base_model_name)
13
+
14
+ # ZeroGPU'da bellek yönetimi için torch_dtype=torch.float16 önemli
15
  base_model = AutoModelForCausalLM.from_pretrained(
16
  base_model_name,
17
+ torch_dtype=torch.float16,
18
+ device_map="cpu" # Başlangıçta CPU'da tutuyoruz
19
  )
20
+
21
  model = PeftModel.from_pretrained(base_model, adapter_model_name)
22
  model.eval()
23
 
24
+ # --- GPU Fonksiyonu ---
25
+ @spaces.GPU # Maksimum 60 saniyelik GPU izni
26
  def fix_text(input_text):
27
  if not input_text or input_text.strip() == "":
28
  return ""
29
 
30
+ # Modeli GPU'ya taşı (ZeroGPU bunu dekoratör sayesinde yönetir)
31
+ model.to("cuda")
32
+
33
  formatted_prompt = f"Fix typos in the text:\n{input_text}"
34
+ messages = [{"role": "user", "content": formatted_prompt}]
 
 
 
35
 
 
36
  inputs = tokenizer.apply_chat_template(
37
  messages,
38
  add_generation_prompt=True,
39
  return_tensors="pt",
40
  return_dict=True,
41
  reasoning_effort="medium"
42
+ ).to("cuda")
43
 
 
44
  with torch.no_grad():
45
  outputs = model.generate(
46
  **inputs,
47
+ max_new_tokens=2048, # ZeroGPU zaman kısıtlı olduğu için çok yüksek tutmamak iyidir
48
  pad_token_id=tokenizer.eos_token_id
49
  )
50
 
 
51
  input_length = inputs.input_ids.shape[1]
52
  response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
53
 
 
56
  # --- Gradio Arayüzü ---
57
  demo = gr.Interface(
58
  fn=fix_text,
59
+ inputs=gr.Textbox(label="Düzeltilecek Metin", lines=3),
60
+ outputs=gr.Textbox(label="Düzeltilmiş Sonuç", lines=3),
61
+ title="ZeroGPU Turkish Correction",
62
+ description="Metni girin ve modelin düzeltmesini bekleyin."
63
  )
64
 
65
+ if __name__ == "__main__":
66
+ demo.launch()