Spaces:
Sleeping
Sleeping
app uppdate for mlx
Browse files
app.py
CHANGED
|
@@ -1,43 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
max_tokens=256,
|
| 26 |
temperature=0.7,
|
| 27 |
top_p=0.9,
|
| 28 |
-
stop=None,
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
return output["choices"][0]["text"].strip()
|
| 32 |
-
|
| 33 |
-
gr.Interface(
|
| 34 |
-
fn=respond,
|
| 35 |
-
inputs="text",
|
| 36 |
-
outputs="text",
|
| 37 |
-
title="Llama3.2-3B Fine-tuned Model"
|
| 38 |
-
).launch()
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from mlx_lm import load, generate
|
| 3 |
+
|
| 4 |
+
# ----------------------------------------------------
|
| 5 |
+
# 1. Load your quantized MLX model from HuggingFace
|
| 6 |
+
# ----------------------------------------------------
|
| 7 |
+
MODEL_REPO = "astegaras/my-mlx-llama3" # <-- change to your repo
|
| 8 |
+
|
| 9 |
+
print("Loading model...")
|
| 10 |
+
model, tokenizer = load(MODEL_REPO)
|
| 11 |
+
print("Model loaded!")
|
| 12 |
+
|
| 13 |
+
# ----------------------------------------------------
|
| 14 |
+
# 2. Chat / inference function
|
| 15 |
+
# ----------------------------------------------------
|
| 16 |
+
def respond(user_input, history):
|
| 17 |
+
"""
|
| 18 |
+
user_input: new user message
|
| 19 |
+
history: list of [user, assistant] messages from Gradio
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Build a conversation prompt (simple version)
|
| 23 |
+
messages = []
|
| 24 |
+
for user_msg, assistant_msg in history:
|
| 25 |
+
messages.append(f"User: {user_msg}\nAssistant: {assistant_msg}")
|
| 26 |
+
messages.append(f"User: {user_input}\nAssistant:")
|
| 27 |
+
|
| 28 |
+
prompt = "\n".join(messages)
|
| 29 |
+
|
| 30 |
+
# Generate with mlx_lm
|
| 31 |
+
output = generate(
|
| 32 |
+
model,
|
| 33 |
+
tokenizer,
|
| 34 |
+
prompt,
|
| 35 |
max_tokens=256,
|
| 36 |
temperature=0.7,
|
| 37 |
top_p=0.9,
|
|
|
|
| 38 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Extract only the assistant's new text
|
| 41 |
+
assistant_reply = output[len(prompt):].strip()
|
| 42 |
|
| 43 |
+
return assistant_reply
|
| 44 |
|
| 45 |
+
# ----------------------------------------------------
|
| 46 |
+
# 3. Launch Gradio chat interface
|
| 47 |
+
# ----------------------------------------------------
|
| 48 |
+
gr.ChatInterface(
|
| 49 |
+
fn=respond,
|
| 50 |
+
title="My MLX Llama Model",
|
| 51 |
+
description="Chat with your fine-tuned MLX model!",
|
| 52 |
+
).launch()
|