Created
June 29, 2024 04:31
-
-
Save rajivmehtaflex/cc24caf4db6b0052fe93f1e787b2cc2c to your computer and use it in GitHub Desktop.
Quick Chatbot Deployment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python server.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install huggingface-hub gradio llama-cpp-python \\n--extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cpu | |
huggingface-cli login | |
mkdir models | |
cd models | |
huggingface-cli download Qwen/Qwen2-0.5B-Instruct-GGUF \\nqwen2-0_5b-instruct-q5_k_m.gguf \\n--local-dir . --local-dir-use-symlinks False | |
cd .. | |
touch server.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gradio as gr | |
from llama_cpp import Llama | |
llm = Llama( | |
model_path="./models/qwen2-0_5b-instruct-q5_k_m.gguf", | |
verbose=True, | |
) | |
def predict(message, history): | |
messages = [{"role": "system", "content": "You are a helpful assistant."}] | |
for user_message, bot_message in history: | |
if user_message: | |
messages.append({"role": "user", "content": user_message}) | |
if bot_message: | |
messages.append({"role": "assistant", "content": bot_message}) | |
messages.append({"role": "user", "content": message}) | |
response = "" | |
for chunk in llm.create_chat_completion( | |
stream=True, | |
messages=messages, | |
): | |
part = chunk["choices"][0]["delta"].get("content", None) | |
if part: | |
response += part | |
yield response | |
demo = gr.ChatInterface(predict) | |
if __name__ == "__main__": | |
demo.launch(share=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment