Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,31 @@
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
llm =
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def chat(message, history):
|
|
|
|
|
|
|
| 14 |
prompt = f"""<|im_start|>user
|
| 15 |
{message}<|im_end|>
|
| 16 |
<|im_start|>assistant
|
| 17 |
"""
|
| 18 |
|
| 19 |
-
output =
|
| 20 |
prompt,
|
| 21 |
max_tokens=512,
|
| 22 |
temperature=0.7,
|
|
@@ -29,4 +38,8 @@ def chat(message, history):
|
|
| 29 |
response += chunk["choices"][0]["text"]
|
| 30 |
yield response
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from llama_cpp import Llama
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# 🔁 Lazy loading (important to avoid OOM)
|
| 5 |
+
llm = None
|
| 6 |
+
|
| 7 |
+
def load_model():
|
| 8 |
+
global llm
|
| 9 |
+
if llm is None:
|
| 10 |
+
llm = Llama.from_pretrained(
|
| 11 |
+
repo_id="bartowski/microsoft_Phi-4-mini-instruct-GGUF",
|
| 12 |
+
filename="microsoft_Phi-4-mini-instruct-Q4_K_M.gguf",
|
| 13 |
+
n_ctx=2048, # lower = less RAM
|
| 14 |
+
n_threads=2,
|
| 15 |
+
verbose=False
|
| 16 |
+
)
|
| 17 |
+
return llm
|
| 18 |
+
|
| 19 |
|
| 20 |
def chat(message, history):
|
| 21 |
+
model = load_model()
|
| 22 |
+
|
| 23 |
prompt = f"""<|im_start|>user
|
| 24 |
{message}<|im_end|>
|
| 25 |
<|im_start|>assistant
|
| 26 |
"""
|
| 27 |
|
| 28 |
+
output = model(
|
| 29 |
prompt,
|
| 30 |
max_tokens=512,
|
| 31 |
temperature=0.7,
|
|
|
|
| 38 |
response += chunk["choices"][0]["text"]
|
| 39 |
yield response
|
| 40 |
|
| 41 |
+
|
| 42 |
+
gr.ChatInterface(
|
| 43 |
+
chat,
|
| 44 |
+
title="🧠 Phi-4 Mini (CPU Stable Space)"
|
| 45 |
+
).launch()
|