"Login" failure fix
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
from functools import lru_cache
|
| 3 |
-
from typing import Iterator
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import hf_hub_download
|
|
@@ -23,11 +23,17 @@ DEFAULT_SYSTEM_PROMPT = (
|
|
| 23 |
|
| 24 |
@lru_cache(maxsize=1)
|
| 25 |
def get_llm() -> Llama:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
model_path = hf_hub_download(
|
| 27 |
repo_id=MODEL_REPO,
|
| 28 |
filename=MODEL_FILE,
|
| 29 |
local_dir=os.getenv("MODEL_DIR") or None,
|
| 30 |
)
|
|
|
|
| 31 |
|
| 32 |
return Llama(
|
| 33 |
model_path=model_path,
|
|
@@ -42,7 +48,7 @@ def get_llm() -> Llama:
|
|
| 42 |
)
|
| 43 |
|
| 44 |
|
| 45 |
-
def trim_history(history: list[
|
| 46 |
if not history:
|
| 47 |
return []
|
| 48 |
|
|
@@ -52,16 +58,23 @@ def trim_history(history: list[dict[str, str]]) -> list[dict[str, str]]:
|
|
| 52 |
|
| 53 |
def build_messages(
|
| 54 |
message: str,
|
| 55 |
-
history: list[
|
| 56 |
system_message: str,
|
| 57 |
) -> list[dict[str, str]]:
|
| 58 |
messages = [{"role": "system", "content": system_message.strip()}]
|
| 59 |
|
| 60 |
for item in trim_history(history):
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
messages.append({"role": "user", "content": message})
|
| 67 |
return messages
|
|
@@ -69,7 +82,7 @@ def build_messages(
|
|
| 69 |
|
| 70 |
def respond(
|
| 71 |
message: str,
|
| 72 |
-
history: list[
|
| 73 |
system_message: str,
|
| 74 |
max_tokens: int,
|
| 75 |
temperature: float,
|
|
@@ -125,4 +138,4 @@ chatbot = gr.ChatInterface(
|
|
| 125 |
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
-
chatbot.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
from functools import lru_cache
|
| 3 |
+
from typing import Any, Iterator
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 23 |
|
| 24 |
@lru_cache(maxsize=1)
|
| 25 |
def get_llm() -> Llama:
|
| 26 |
+
print(
|
| 27 |
+
f"Loading GGUF model {MODEL_REPO}/{MODEL_FILE} "
|
| 28 |
+
f"with n_ctx={N_CTX}, n_batch={N_BATCH}, n_threads={N_THREADS}",
|
| 29 |
+
flush=True,
|
| 30 |
+
)
|
| 31 |
model_path = hf_hub_download(
|
| 32 |
repo_id=MODEL_REPO,
|
| 33 |
filename=MODEL_FILE,
|
| 34 |
local_dir=os.getenv("MODEL_DIR") or None,
|
| 35 |
)
|
| 36 |
+
print(f"Model file ready: {model_path}", flush=True)
|
| 37 |
|
| 38 |
return Llama(
|
| 39 |
model_path=model_path,
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
|
| 51 |
+
def trim_history(history: list[Any]) -> list[Any]:
|
| 52 |
if not history:
|
| 53 |
return []
|
| 54 |
|
|
|
|
| 58 |
|
| 59 |
def build_messages(
|
| 60 |
message: str,
|
| 61 |
+
history: list[Any],
|
| 62 |
system_message: str,
|
| 63 |
) -> list[dict[str, str]]:
|
| 64 |
messages = [{"role": "system", "content": system_message.strip()}]
|
| 65 |
|
| 66 |
for item in trim_history(history):
|
| 67 |
+
if isinstance(item, dict):
|
| 68 |
+
role = item.get("role")
|
| 69 |
+
content = item.get("content")
|
| 70 |
+
if role in {"user", "assistant"} and content:
|
| 71 |
+
messages.append({"role": role, "content": content})
|
| 72 |
+
elif isinstance(item, (list, tuple)) and len(item) >= 2:
|
| 73 |
+
user_text, assistant_text = item[:2]
|
| 74 |
+
if user_text:
|
| 75 |
+
messages.append({"role": "user", "content": str(user_text)})
|
| 76 |
+
if assistant_text:
|
| 77 |
+
messages.append({"role": "assistant", "content": str(assistant_text)})
|
| 78 |
|
| 79 |
messages.append({"role": "user", "content": message})
|
| 80 |
return messages
|
|
|
|
| 82 |
|
| 83 |
def respond(
|
| 84 |
message: str,
|
| 85 |
+
history: list[Any],
|
| 86 |
system_message: str,
|
| 87 |
max_tokens: int,
|
| 88 |
temperature: float,
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
+
chatbot.queue(max_size=8).launch()
|