Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,20 +5,23 @@ warnings.filterwarnings("ignore")
|
|
5 |
import json
|
6 |
import subprocess
|
7 |
import sys
|
|
|
8 |
from llama_cpp import Llama
|
9 |
from llama_cpp_agent import LlamaCppAgent
|
10 |
-
from llama_cpp_agent import MessagesFormatterType
|
11 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
12 |
from llama_cpp_agent.chat_history import BasicChatHistory
|
13 |
from llama_cpp_agent.chat_history.messages import Roles
|
14 |
-
import
|
15 |
from huggingface_hub import hf_hub_download
|
16 |
-
|
17 |
from logger import logging
|
18 |
from exception import CustomExceptionHandling
|
19 |
|
20 |
|
21 |
# Download gguf model files
|
|
|
|
|
|
|
22 |
hf_hub_download(
|
23 |
repo_id="bartowski/SmolLM2-135M-Instruct-GGUF",
|
24 |
filename="SmolLM2-135M-Instruct-Q6_K.gguf",
|
@@ -42,13 +45,13 @@ llm_model = None
|
|
42 |
def respond(
|
43 |
message: str,
|
44 |
history: List[Tuple[str, str]],
|
45 |
-
model: str,
|
46 |
-
system_message: str,
|
47 |
-
max_tokens: int,
|
48 |
-
temperature: float,
|
49 |
-
top_p: float,
|
50 |
-
top_k: int,
|
51 |
-
repeat_penalty: float,
|
52 |
):
|
53 |
"""
|
54 |
Respond to a message using the SmolLM2 model via Llama.cpp.
|
@@ -72,8 +75,18 @@ def respond(
|
|
72 |
global llm
|
73 |
global llm_model
|
74 |
|
|
|
|
|
|
|
|
|
75 |
# Load the model
|
76 |
if llm is None or llm_model != model:
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
llm = Llama(
|
78 |
model_path=f"models/{model}",
|
79 |
flash_attn=False,
|
@@ -205,11 +218,18 @@ demo = gr.ChatInterface(
|
|
205 |
stop_btn="Stop",
|
206 |
title=title,
|
207 |
description=description,
|
208 |
-
chatbot=gr.Chatbot(scale=1, show_copy_button=True),
|
209 |
flagging_mode="never",
|
|
|
|
|
210 |
)
|
211 |
|
212 |
|
213 |
# Launch the chat interface
|
214 |
if __name__ == "__main__":
|
215 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import json
|
6 |
import subprocess
|
7 |
import sys
|
8 |
+
from typing import List, Tuple
|
9 |
from llama_cpp import Llama
|
10 |
from llama_cpp_agent import LlamaCppAgent
|
|
|
11 |
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
12 |
from llama_cpp_agent.chat_history import BasicChatHistory
|
13 |
from llama_cpp_agent.chat_history.messages import Roles
|
14 |
+
from llama_cpp_agent.messages_formatter import MessagesFormatter, PromptMarkers
|
15 |
from huggingface_hub import hf_hub_download
|
16 |
+
import gradio as gr
|
17 |
from logger import logging
|
18 |
from exception import CustomExceptionHandling
|
19 |
|
20 |
|
21 |
# Download gguf model files
|
22 |
+
if not os.path.exists("./models"):
|
23 |
+
os.makedirs("./models")
|
24 |
+
|
25 |
hf_hub_download(
|
26 |
repo_id="bartowski/SmolLM2-135M-Instruct-GGUF",
|
27 |
filename="SmolLM2-135M-Instruct-Q6_K.gguf",
|
|
|
45 |
def respond(
|
46 |
message: str,
|
47 |
history: List[Tuple[str, str]],
|
48 |
+
model: str = "SmolLM2-135M-Instruct-Q6_K.gguf", # Set default model
|
49 |
+
system_message: str = "You are a helpful assistant.",
|
50 |
+
max_tokens: int = 1024,
|
51 |
+
temperature: float = 0.7,
|
52 |
+
top_p: float = 0.95,
|
53 |
+
top_k: int = 40,
|
54 |
+
repeat_penalty: float = 1.1,
|
55 |
):
|
56 |
"""
|
57 |
Respond to a message using the SmolLM2 model via Llama.cpp.
|
|
|
75 |
global llm
|
76 |
global llm_model
|
77 |
|
78 |
+
# Ensure model is not None
|
79 |
+
if model is None:
|
80 |
+
model = "SmolLM2-135M-Instruct-Q6_K.gguf"
|
81 |
+
|
82 |
# Load the model
|
83 |
if llm is None or llm_model != model:
|
84 |
+
# Check if model file exists
|
85 |
+
model_path = f"models/{model}"
|
86 |
+
if not os.path.exists(model_path):
|
87 |
+
yield f"Error: Model file not found at {model_path}. Please check your model path."
|
88 |
+
return
|
89 |
+
|
90 |
llm = Llama(
|
91 |
model_path=f"models/{model}",
|
92 |
flash_attn=False,
|
|
|
218 |
stop_btn="Stop",
|
219 |
title=title,
|
220 |
description=description,
|
221 |
+
chatbot=gr.Chatbot(scale=1, show_copy_button=True, resizable=True),
|
222 |
flagging_mode="never",
|
223 |
+
editable=True,
|
224 |
+
cache_examples=False,
|
225 |
)
|
226 |
|
227 |
|
228 |
# Launch the chat interface
|
229 |
if __name__ == "__main__":
|
230 |
+
demo.launch(
|
231 |
+
share=False,
|
232 |
+
server_name="0.0.0.0",
|
233 |
+
server_port=7860,
|
234 |
+
show_api=False,
|
235 |
+
)
|