Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import mlx.core as mx
|
| 3 |
+
import mlx.nn as nn
|
| 4 |
+
from mlx_lm import load, generate
|
| 5 |
+
from transformers import AutoTokenizer
|
| 6 |
+
|
| 7 |
+
# モデルとトークナイザーのロード
|
| 8 |
+
model_name = "Sakalti/ultiima-78B-Q2-mlx"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = load(model_name, quantization="q2")
|
| 11 |
+
|
| 12 |
+
def chat(prompt, top_p, top_k, max_new_tokens, system_prompt):
|
| 13 |
+
messages = [
|
| 14 |
+
{"role": "system", "content": system_prompt},
|
| 15 |
+
{"role": "user", "content": prompt}
|
| 16 |
+
]
|
| 17 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 18 |
+
inputs = tokenizer([text], return_tensors="pt")
|
| 19 |
+
|
| 20 |
+
# モデルに入力を渡して生成
|
| 21 |
+
output = generate(
|
| 22 |
+
model,
|
| 23 |
+
inputs.input_ids,
|
| 24 |
+
tokenizer=tokenizer,
|
| 25 |
+
max_tokens=max_new_tokens,
|
| 26 |
+
top_p=top_p,
|
| 27 |
+
top_k=top_k
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
return output
|
| 31 |
+
|
| 32 |
+
# GradioのUI設定
|
| 33 |
+
chat_interface = gr.ChatInterface(
|
| 34 |
+
fn=chat,
|
| 35 |
+
additional_inputs=[
|
| 36 |
+
gr.Textbox(value="あなたはフレンドリーなチャットボットです。", label="System Prompt"),
|
| 37 |
+
gr.Slider(0.0, 1.0, value=0.9, label="Top-p"),
|
| 38 |
+
gr.Slider(1, 100, value=50, label="Top-k"),
|
| 39 |
+
gr.Slider(1, 1024, value=512, step=1, label="Max New Tokens")
|
| 40 |
+
]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
chat_interface.launch()
|