Spaces:
Running
Running
Fix gradio launch and mistral prompt formatting
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ from ctransformers import AutoModelForCausalLM
|
|
| 4 |
MODEL_REPO = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
|
| 5 |
MODEL_FILE = "mistral-7b-instruct-v0.2.Q4_K_M.gguf"
|
| 6 |
|
| 7 |
-
print("Loading model...")
|
| 8 |
llm = AutoModelForCausalLM.from_pretrained(
|
| 9 |
MODEL_REPO,
|
| 10 |
model_file=MODEL_FILE,
|
|
@@ -13,23 +12,25 @@ llm = AutoModelForCausalLM.from_pretrained(
|
|
| 13 |
context_length=2048,
|
| 14 |
)
|
| 15 |
|
| 16 |
-
def respond(message: str, history: list[
|
| 17 |
prompt = ""
|
| 18 |
for user_msg, bot_msg in history:
|
| 19 |
-
prompt += f"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
prompt += f"<|im_start|>user\n{message}\n<|im_end|>\n<|im_start|>assistant\n"
|
| 23 |
|
| 24 |
out = llm(
|
| 25 |
prompt,
|
| 26 |
max_new_tokens=256,
|
| 27 |
temperature=0.7,
|
| 28 |
top_p=0.9,
|
| 29 |
-
stop=["<
|
| 30 |
)
|
| 31 |
-
return out["text"]
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
-
|
|
|
|
| 4 |
MODEL_REPO = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF"
|
| 5 |
MODEL_FILE = "mistral-7b-instruct-v0.2.Q4_K_M.gguf"
|
| 6 |
|
|
|
|
| 7 |
llm = AutoModelForCausalLM.from_pretrained(
|
| 8 |
MODEL_REPO,
|
| 9 |
model_file=MODEL_FILE,
|
|
|
|
| 12 |
context_length=2048,
|
| 13 |
)
|
| 14 |
|
| 15 |
+
def respond(message: str, history: list[tuple[str, str]]):
|
| 16 |
prompt = ""
|
| 17 |
for user_msg, bot_msg in history:
|
| 18 |
+
prompt += f"[INST] {user_msg} [/INST] {bot_msg}</s>"
|
| 19 |
+
prompt += f"[INST] {message} [/INST]"
|
|
|
|
|
|
|
| 20 |
|
| 21 |
out = llm(
|
| 22 |
prompt,
|
| 23 |
max_new_tokens=256,
|
| 24 |
temperature=0.7,
|
| 25 |
top_p=0.9,
|
| 26 |
+
stop=["</s>"],
|
| 27 |
)
|
|
|
|
| 28 |
|
| 29 |
+
if isinstance(out, dict) and "text" in out:
|
| 30 |
+
return out["text"]
|
| 31 |
+
return str(out)
|
| 32 |
+
|
| 33 |
+
demo = gr.ChatInterface(respond)
|
| 34 |
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|