Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,53 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
model = LlamaForCausalLM.from_pretrained("bkaplan/MRL1", device_map="auto", torch_dtype=torch.float16)
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
demo
|
36 |
-
respond,
|
37 |
-
additional_inputs=[
|
38 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
39 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
40 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
41 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
42 |
-
]
|
43 |
-
)
|
44 |
|
45 |
-
|
46 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
+
# Modeli yükleyin
|
6 |
+
model_name = "bkaplan/MRL1"
|
|
|
7 |
|
8 |
+
try:
|
9 |
+
# Tokenizer ve modeli yükleme
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
|
12 |
+
|
13 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
14 |
+
try:
|
15 |
+
# Girdiyi hazırlama
|
16 |
+
input_text = f"System: {system_message}\nUser: {message}\nAssistant:"
|
17 |
+
|
18 |
+
# Tokenize etme
|
19 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
20 |
+
|
21 |
+
# Üretim parametreleri
|
22 |
+
outputs = model.generate(
|
23 |
+
**inputs,
|
24 |
+
max_length=max_tokens,
|
25 |
+
temperature=temperature,
|
26 |
+
top_p=top_p,
|
27 |
+
num_return_sequences=1,
|
28 |
+
do_sample=True
|
29 |
+
)
|
30 |
+
|
31 |
+
# Yanıtı çözme
|
32 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
33 |
+
yield response
|
34 |
|
35 |
+
except Exception as e:
|
36 |
+
yield f"Hata oluştu: {str(e)}"
|
37 |
+
|
38 |
+
# Gradio arayüzü
|
39 |
+
demo = gr.ChatInterface(
|
40 |
+
respond,
|
41 |
+
additional_inputs=[
|
42 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
43 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
44 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
45 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
|
46 |
+
]
|
47 |
+
)
|
48 |
|
49 |
+
if __name__ == "__main__":
|
50 |
+
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
except Exception as e:
|
53 |
+
print(f"Model yüklenirken hata oluştu: {e}")
|