Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
# ----------------------------------------------------
|
| 5 |
-
# 1.
|
| 6 |
# ----------------------------------------------------
|
| 7 |
-
MODEL_REPO = "astegaras/my-mlx-llama3" # <-- change to your repo
|
| 8 |
|
| 9 |
-
|
| 10 |
-
model
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ----------------------------------------------------
|
| 14 |
-
# 2.
|
| 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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
prompt
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
# Generate
|
| 31 |
-
output =
|
| 32 |
-
model,
|
| 33 |
-
tokenizer,
|
| 34 |
prompt,
|
| 35 |
max_tokens=256,
|
| 36 |
temperature=0.7,
|
| 37 |
top_p=0.9,
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
assistant_reply = output[len(prompt):].strip()
|
| 42 |
-
|
| 43 |
return assistant_reply
|
| 44 |
|
| 45 |
# ----------------------------------------------------
|
| 46 |
-
#
|
| 47 |
# ----------------------------------------------------
|
| 48 |
gr.ChatInterface(
|
| 49 |
fn=respond,
|
| 50 |
-
title="My
|
| 51 |
-
description="Chat with your fine-tuned
|
| 52 |
).launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
|
| 5 |
# ----------------------------------------------------
|
| 6 |
+
# 1. Download GGUF model from HuggingFace
|
| 7 |
# ----------------------------------------------------
|
|
|
|
| 8 |
|
| 9 |
+
REPO_ID = "astegaras/merged_kaggle" # your GGUF repo
|
| 10 |
+
FILENAME = "model-q4_k_m.gguf" # your GGUF file
|
| 11 |
+
|
| 12 |
+
print("Downloading GGUF model...")
|
| 13 |
+
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 14 |
+
print("Model downloaded:", model_path)
|
| 15 |
|
| 16 |
# ----------------------------------------------------
|
| 17 |
+
# 2. Load llama.cpp model
|
| 18 |
# ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
llm = Llama(
|
| 21 |
+
model_path=model_path,
|
| 22 |
+
n_ctx=4096, # context size
|
| 23 |
+
n_threads=8, # use HF Space CPU
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# ----------------------------------------------------
|
| 27 |
+
# 3. Chat / inference function
|
| 28 |
+
# ----------------------------------------------------
|
| 29 |
+
def respond(message, history):
|
| 30 |
+
prompt = ""
|
| 31 |
|
| 32 |
+
# Build prompt manually
|
| 33 |
+
for user_msg, bot_msg in history:
|
| 34 |
+
prompt += f"User: {user_msg}\nAssistant: {bot_msg}\n"
|
| 35 |
+
prompt += f"User: {message}\nAssistant:"
|
| 36 |
|
| 37 |
+
# Generate response
|
| 38 |
+
output = llm(
|
|
|
|
|
|
|
| 39 |
prompt,
|
| 40 |
max_tokens=256,
|
| 41 |
temperature=0.7,
|
| 42 |
top_p=0.9,
|
| 43 |
+
stop=["User:", "Assistant:"]
|
| 44 |
)
|
| 45 |
|
| 46 |
+
assistant_reply = output["choices"][0]["text"].strip()
|
|
|
|
|
|
|
| 47 |
return assistant_reply
|
| 48 |
|
| 49 |
# ----------------------------------------------------
|
| 50 |
+
# 4. Launch Gradio Chat Interface
|
| 51 |
# ----------------------------------------------------
|
| 52 |
gr.ChatInterface(
|
| 53 |
fn=respond,
|
| 54 |
+
title="My Llama.cpp GGUF Model",
|
| 55 |
+
description="Chat with your fine-tuned GGUF model!",
|
| 56 |
).launch()
|
| 57 |
+
|