erhanmeydan commited on
Commit
7732a85
·
verified ·
1 Parent(s): 3219932

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -4
app.py CHANGED
@@ -1,7 +1,116 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ import torch
4
+ import os
5
 
6
+ # Model adını belirtin
7
+ model_name = "MiniMaxAI/MiniMax-M1-40k"
8
 
9
+ # Tokenizer'ı yükleyin
10
+ print(f"Tokenizer yükleniyor: {model_name}...")
11
+ try:
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ print("Tokenizer yüklendi.")
14
+ except Exception as e:
15
+ print(f"Tokenizer yüklenirken hata oluştu: {e}")
16
+ # Hata durumunda boş bir tokenizer veya hata mesajı
17
+ tokenizer = None
18
+
19
+ # Modeli yükleyin
20
+ print(f"Model yükleniyor: {model_name}...")
21
+ # Dikkat: Bu model çok büyük olabilir. device_map="auto" ve torch_dtype=torch.float16
22
+ # bellek kullanımını azaltmaya yardımcı olabilir, ancak yeterli değilse CPU'da yüklemek gerekebilir.
23
+ try:
24
+ model = AutoModelForCausalLM.from_pretrained(
25
+ model_name,
26
+ device_map="auto",
27
+ torch_dtype=torch.float16 # Bellek kullanımını azaltmak ve hesaplamayı hızlandırmak için
28
+ # load_in_8bit=True # 8-bit kuantizasyon kullanmayı deneyebilirsiniz (ek kurulum gerektirir)
29
+ )
30
+ print("Model yüklendi.")
31
+ except Exception as e:
32
+ print(f"Model yüklenirken hata oluştu: {e}")
33
+ print("CPU'da yüklemeyi deniyorum...")
34
+ try:
35
+ model = AutoModelForCausalLM.from_pretrained(
36
+ model_name,
37
+ device_map="cpu" # Eğer GPU yoksa CPU'da yükle
38
+ )
39
+ print("Model CPU'da yüklendi.")
40
+ except Exception as cpu_e:
41
+ print(f"Model CPU'da yüklenirken de hata oluştu: {cpu_e}")
42
+ model = None
43
+
44
+ # Modeli değerlendirme moduna (evaluation) alın
45
+ if model:
46
+ model.eval()
47
+
48
+ # Metin üretme fonksiyonu
49
+ def generate_text(prompt, max_length=500, temperature=0.7, top_p=0.9, top_k=50):
50
+ """
51
+ Verilen prompt'a göre modelden metin üretir.
52
+ """
53
+ if not model or not tokenizer:
54
+ return "Hata: Model veya tokenizer yüklenemedi."
55
+
56
+ # Prompt'u token'lara dönüştür
57
+ try:
58
+ inputs = tokenizer(prompt, return_tensors="pt")
59
+
60
+ # Eğer model GPU'daysa inputs'ları da GPU'ya taşı
61
+ if model.device != torch.device("cpu"):
62
+ inputs = inputs.to(model.device)
63
+ except Exception as e:
64
+ return f"Prompt tokenleştirilirken hata oluştu: {e}"
65
+
66
+ # Metin üretimi
67
+ with torch.no_grad():
68
+ try:
69
+ outputs = model.generate(
70
+ **inputs,
71
+ max_length=max_length,
72
+ do_sample=True,
73
+ temperature=temperature,
74
+ top_p=top_p,
75
+ top_k=top_k,
76
+ pad_token_id=tokenizer.eos_token_id # Padding için EOS token'ını kullan
77
+ )
78
+ except Exception as e:
79
+ return f"Metin üretimi sırasında hata oluştu: {e}"
80
+
81
+ # Üretilen token'ları metne dönüştür ve oku
82
+ try:
83
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
84
+ except Exception as e:
85
+ return f"Metin çözülürken hata oluştu: {e}"
86
+
87
+ return generated_text
88
+
89
+ # Gradio arayüzünü tanımlayın
90
+ with gr.Blocks() as demo:
91
+ gr.Markdown("# MiniMax-M1-40k Demo")
92
+ gr.Markdown("MiniMaxAI/MiniMax-M1-40k modelini kullanarak metin üretin.")
93
+
94
+ with gr.Row():
95
+ with gr.Column(scale=3):
96
+ prompt_input = gr.Textbox(label="Prompt", lines=5, placeholder="Buraya metninizi girin...")
97
+ generate_button = gr.Button("Metin Üret")
98
+ with gr.Column(scale=1):
99
+ max_length = gr.Slider(minimum=50, maximum=2048, value=500, step=10, label="Max Uzunluk")
100
+ temperature = gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Sıcaklık")
101
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top P")
102
+ top_k = gr.Slider(minimum=1, maximum=100, value=50, step=1, label="Top K")
103
+
104
+ output_text = gr.Textbox(label="Üretilen Metin", lines=10, interactive=False)
105
+
106
+ # Butona tıklandığında fonksiyonu çağır
107
+ generate_button.click(
108
+ fn=generate_text,
109
+ inputs=[prompt_input, max_length, temperature, top_p, top_k],
110
+ outputs=output_text
111
+ )
112
+
113
+ # Gradio arayüzünü başlatın
114
+ if __name__ == "__main__":
115
+ print("Gradio arayüzü başlatılıyor...")
116
+ demo.launch()