Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,52 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 3 |
import torch
|
|
|
|
| 4 |
import spaces
|
|
|
|
| 5 |
|
| 6 |
MODEL_ID = "userdotcs/gpt-oss-20b-turkish-correction-finetuned"
|
| 7 |
-
BASE_MODEL = "unsloth/gpt-oss-20b" # veya base repo adı neyse
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
|
|
|
| 13 |
device_map="auto",
|
| 14 |
-
|
| 15 |
-
|
| 16 |
)
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def generate(prompt):
|
|
|
|
|
|
|
| 20 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
demo = gr.Interface(
|
| 30 |
fn=generate,
|
| 31 |
-
inputs=gr.Textbox(lines=4, placeholder="
|
| 32 |
outputs="text",
|
| 33 |
-
title="Turkish Correction GPT-OSS-20B"
|
| 34 |
)
|
| 35 |
|
| 36 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
import spaces
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 5 |
|
| 6 |
MODEL_ID = "userdotcs/gpt-oss-20b-turkish-correction-finetuned"
|
|
|
|
| 7 |
|
| 8 |
+
# 1. Kuantizasyon ayarlarını config'den manuel çekiyoruz (Bellek yetmesi için şart)
|
| 9 |
+
bnb_config = BitsAndBytesConfig(
|
| 10 |
+
load_in_4bit=True,
|
| 11 |
+
bnb_4bit_use_double_quant=True,
|
| 12 |
+
bnb_4bit_quant_type="nf4",
|
| 13 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# 2. Tokenizer'ı yükle
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 18 |
|
| 19 |
+
# 3. Modeli yükle (Özel mimari olduğu için trust_remote_code kritik)
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
MODEL_ID,
|
| 22 |
+
quantization_config=bnb_config,
|
| 23 |
device_map="auto",
|
| 24 |
+
trust_remote_code=True, # Custom 'GptOssForCausalLM' mimarisi için zorunlu
|
| 25 |
+
torch_dtype=torch.float16
|
| 26 |
)
|
| 27 |
|
| 28 |
@spaces.GPU
|
| 29 |
def generate(prompt):
|
| 30 |
+
# Prompt formatını modelin eğitimine uygun hale getirmek gerekebilir
|
| 31 |
+
# Örn: "Düzeltme: {prompt}" gibi
|
| 32 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 33 |
+
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
out = model.generate(
|
| 36 |
+
**inputs,
|
| 37 |
+
max_new_tokens=1024,
|
| 38 |
+
pad_token_id=tokenizer.eos_token_id
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Sadece yeni üretilen metni al (input'u kes)
|
| 42 |
+
output_text = tokenizer.decode(out[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True)
|
| 43 |
+
return output_text
|
| 44 |
|
| 45 |
demo = gr.Interface(
|
| 46 |
fn=generate,
|
| 47 |
+
inputs=gr.Textbox(lines=4, placeholder="Düzeltilecek metni girin..."),
|
| 48 |
outputs="text",
|
| 49 |
+
title="Turkish Correction with GPT-OSS-20B"
|
| 50 |
)
|
| 51 |
|
| 52 |
demo.launch()
|