Add another model - make modesl selectable ? - What wind.surf will do ?
Browse files
app.py
CHANGED
|
@@ -2,23 +2,52 @@ import gradio as gr
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def respond(
|
| 15 |
message,
|
| 16 |
history: list[tuple[str, str]],
|
|
|
|
| 17 |
system_message,
|
| 18 |
max_tokens,
|
| 19 |
temperature,
|
| 20 |
top_p,
|
| 21 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
messages = [{"role": "system", "content": system_message}]
|
| 23 |
|
| 24 |
for val in history:
|
|
@@ -30,7 +59,7 @@ def respond(
|
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
response = ""
|
| 33 |
-
response =
|
| 34 |
messages=messages,
|
| 35 |
stream=True,
|
| 36 |
max_tokens=max_tokens,
|
|
@@ -43,14 +72,21 @@ def respond(
|
|
| 43 |
message_repl = message_repl + \
|
| 44 |
chunk['choices'][0]["delta"]["content"]
|
| 45 |
yield message_repl
|
|
|
|
| 46 |
"""
|
| 47 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 48 |
"""
|
| 49 |
demo = gr.ChatInterface(
|
| 50 |
respond,
|
| 51 |
title="GGUF is popular format on PC in LM Studio or on Tablet/Mobile in PocketPal APPs",
|
| 52 |
-
description="Try models locclay in: 🖥️ [LM Studio AI for PC](https://lmstudio.ai) | 📱 PocketPal AI ([Android](https://play.google.com/store/apps/details?id=com.pocketpalai) & [iOS](https://play.google.com/store/apps/details?id=com.pocketpalai))",
|
|
|
|
| 53 |
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 55 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 56 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
|
|
|
| 2 |
from llama_cpp import Llama
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
# Define available models
|
| 6 |
+
MODELS = {
|
| 7 |
+
"Llama-3.2-3B": {
|
| 8 |
+
"repo_id": "lmstudio-community/Llama-3.2-3B-Instruct-GGUF",
|
| 9 |
+
"filename": "*Q4_K_M.gguf"
|
| 10 |
+
},
|
| 11 |
+
"Llama-3.2-1.5B": {
|
| 12 |
+
"repo_id": "lmstudio-community/Llama-3.2-1.5B-Instruct-GGUF",
|
| 13 |
+
"filename": "*Q4_K_M.gguf"
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# Initialize with default model
|
| 18 |
+
current_model = None
|
| 19 |
+
|
| 20 |
+
def load_model(model_name):
|
| 21 |
+
global current_model
|
| 22 |
+
model_info = MODELS[model_name]
|
| 23 |
+
current_model = Llama.from_pretrained(
|
| 24 |
+
repo_id=model_info["repo_id"],
|
| 25 |
+
filename=model_info["filename"],
|
| 26 |
+
verbose=True,
|
| 27 |
+
n_ctx=32768,
|
| 28 |
+
n_threads=2,
|
| 29 |
+
chat_format="chatml"
|
| 30 |
+
)
|
| 31 |
+
return current_model
|
| 32 |
+
|
| 33 |
+
# Initialize with first model
|
| 34 |
+
current_model = load_model(list(MODELS.keys())[0])
|
| 35 |
|
| 36 |
def respond(
|
| 37 |
message,
|
| 38 |
history: list[tuple[str, str]],
|
| 39 |
+
model_name,
|
| 40 |
system_message,
|
| 41 |
max_tokens,
|
| 42 |
temperature,
|
| 43 |
top_p,
|
| 44 |
):
|
| 45 |
+
global current_model
|
| 46 |
+
|
| 47 |
+
# Load new model if changed
|
| 48 |
+
if current_model is None or model_name != current_model.model_path:
|
| 49 |
+
current_model = load_model(model_name)
|
| 50 |
+
|
| 51 |
messages = [{"role": "system", "content": system_message}]
|
| 52 |
|
| 53 |
for val in history:
|
|
|
|
| 59 |
messages.append({"role": "user", "content": message})
|
| 60 |
|
| 61 |
response = ""
|
| 62 |
+
response = current_model.create_chat_completion(
|
| 63 |
messages=messages,
|
| 64 |
stream=True,
|
| 65 |
max_tokens=max_tokens,
|
|
|
|
| 72 |
message_repl = message_repl + \
|
| 73 |
chunk['choices'][0]["delta"]["content"]
|
| 74 |
yield message_repl
|
| 75 |
+
|
| 76 |
"""
|
| 77 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 78 |
"""
|
| 79 |
demo = gr.ChatInterface(
|
| 80 |
respond,
|
| 81 |
title="GGUF is popular format on PC in LM Studio or on Tablet/Mobile in PocketPal APPs",
|
| 82 |
+
description="Try models locclay in: 🖥️ [LM Studio AI for PC](https://lmstudio.ai) | 📱 PocketPal AI ([Android](https://play.google.com/store/apps/details?id=com.pocketpalai) & [iOS](https://play.google.com/store/apps/details?id=com.pocketpalai)) on Tablet or Mobile",
|
| 83 |
+
|
| 84 |
additional_inputs=[
|
| 85 |
+
gr.Dropdown(
|
| 86 |
+
choices=list(MODELS.keys()),
|
| 87 |
+
value=list(MODELS.keys())[0],
|
| 88 |
+
label="Select Model"
|
| 89 |
+
),
|
| 90 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 91 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 92 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|