Commit
·
f0dc90a
1
Parent(s):
b4f86c0
Create gradio app
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gpt4all import GPT4All
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
title = "Mistral-7B-Instruct-GGUF-Run-On-CPU-Basic"
|
| 6 |
+
|
| 7 |
+
description = """
|
| 8 |
+
🔎 mistral-7b-instruct-v0.1, 4-bit quantisation balanced quality gguf version, running on CPU. English Only. (Also support other languages but the quality's not good)
|
| 9 |
+
|
| 10 |
+
🔨 Running on CPU-Basic Hardware. Suggest duplicating this space to run without a queue. [GGUF format model files](https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF) for [Mistral AI's Mistral 7B Instruct v0.1](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1). How to insert system prompt: [Learn more](https://docs.mistral.ai/usage/guardrailing). [GitHub - gpt4all](https://github.com/nomic-ai/gpt4all) [GitHub - llama.cpp](https://github.com/ggerganov/llama.cpp)
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
"""
|
| 14 |
+
[Model From TheBloke/Mistral-7B-Instruct-v0.1-GGUF](https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF)
|
| 15 |
+
[Mistral-instruct-v0.1 System prompt](https://docs.mistral.ai/usage/guardrailing)
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
model_path = "models"
|
| 19 |
+
model_name = "mistral-7b-instruct-v0.1.Q4_K_M.gguf"
|
| 20 |
+
hf_hub_download(repo_id="TheBloke/Mistral-7B-Instruct-v0.1-GGUF", filename=model_name, local_dir=model_path, local_dir_use_symlinks=False)
|
| 21 |
+
|
| 22 |
+
print("Start the model init process")
|
| 23 |
+
model = model = GPT4All(model_name, model_path, allow_download = False, device="cpu")
|
| 24 |
+
print("Finish the model init process")
|
| 25 |
+
|
| 26 |
+
model.config["promptTemplate"] = "[INST] {0} [/INST]"
|
| 27 |
+
model.config["systemPrompt"] = ""
|
| 28 |
+
model._is_chat_session_activated = False
|
| 29 |
+
|
| 30 |
+
max_new_tokens = 2048
|
| 31 |
+
|
| 32 |
+
def generater(message, history, temperature, top_p, top_k):
|
| 33 |
+
prompt = "<s>"
|
| 34 |
+
for user_message, assistant_message in history:
|
| 35 |
+
prompt += model.config["promptTemplate"].format(user_message)
|
| 36 |
+
prompt += assistant_message + "</s>"
|
| 37 |
+
prompt += model.config["promptTemplate"].format(message)
|
| 38 |
+
outputs = []
|
| 39 |
+
for token in model.generate(prompt=prompt, temp=temperature, top_k = top_k, top_p = top_p, max_tokens = max_new_tokens, streaming=True):
|
| 40 |
+
outputs.append(token)
|
| 41 |
+
yield "".join(outputs)
|
| 42 |
+
|
| 43 |
+
def vote(data: gr.LikeData):
|
| 44 |
+
if data.liked:
|
| 45 |
+
return
|
| 46 |
+
else:
|
| 47 |
+
return
|
| 48 |
+
|
| 49 |
+
chatbot = gr.Chatbot(avatar_images=('resourse/user-icon.png', 'resourse/chatbot-icon.png'),bubble_full_width = False)
|
| 50 |
+
|
| 51 |
+
additional_inputs=[
|
| 52 |
+
gr.Slider(
|
| 53 |
+
label="temperature",
|
| 54 |
+
value=0.5,
|
| 55 |
+
minimum=0.0,
|
| 56 |
+
maximum=2.0,
|
| 57 |
+
step=0.05,
|
| 58 |
+
interactive=True,
|
| 59 |
+
info="Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.",
|
| 60 |
+
),
|
| 61 |
+
gr.Slider(
|
| 62 |
+
label="top_p",
|
| 63 |
+
value=1.0,
|
| 64 |
+
minimum=0.0,
|
| 65 |
+
maximum=1.0,
|
| 66 |
+
step=0.01,
|
| 67 |
+
interactive=True,
|
| 68 |
+
info="0.1 means only the tokens comprising the top 10% probability mass are considered. Suggest set to 1 and use temperature. 1 means 100% and will disable it",
|
| 69 |
+
),
|
| 70 |
+
gr.Slider(
|
| 71 |
+
label="top_k",
|
| 72 |
+
value=40,
|
| 73 |
+
minimum=0,
|
| 74 |
+
maximum=1000,
|
| 75 |
+
step=1,
|
| 76 |
+
interactive=True,
|
| 77 |
+
info="limits candidate tokens to a fixed number after sorting by probability. Setting it higher than the vocabulary size deactivates this limit.",
|
| 78 |
+
)
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
character = "Sherlock Holmes"
|
| 82 |
+
series = "Arthur Conan Doyle's novel"
|
| 83 |
+
|
| 84 |
+
iface = gr.ChatInterface(
|
| 85 |
+
fn = generater,
|
| 86 |
+
title=title,
|
| 87 |
+
description = description,
|
| 88 |
+
chatbot=chatbot,
|
| 89 |
+
additional_inputs=additional_inputs,
|
| 90 |
+
examples=[
|
| 91 |
+
["Hello there! How are you doing?"],
|
| 92 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
| 93 |
+
["You are a helpful and honest assistant. Always answer as helpfully as possible. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."],
|
| 94 |
+
["I want you to act as a spoken English teacher and improver. I will speak to you in English and you will reply to me in English to practice my spoken English. I want you to strictly correct my grammar mistakes, typos, and factual errors. I want you to ask me a question in your reply. Now let's start practicing, you could ask me a question first. Remember, I want you to strictly correct my grammar mistakes, typos, and factual errors."],
|
| 95 |
+
[f"I want you to act like {character} from {series}. I want you to respond and answer like {character} using the tone, manner and vocabulary {character} would use. Do not write any explanations. Only answer like {character}. You must know all of the knowledge of {character}."]
|
| 96 |
+
]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
with gr.Blocks(css="resourse/style/custom.css") as demo:
|
| 100 |
+
chatbot.like(vote, None, None)
|
| 101 |
+
iface.render()
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo.queue(max_size=3).launch()
|