ztime/yi-6b-chat_llamaCppPython
0
1import gradio as gr2import os 3from pathlib import Path4 5os.environ["CMAKE_ARGS"] = "-DLLAMA_CUBLAS=on"6os.system('CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install llama-cpp-python[server]')7 8import argparse9model_file = "yi-chat-6b.Q4_K_M.gguf"10if not os.path.isfile(model_file):11 os.system("wget -c https://huggingface.co/XeIaso/yi-chat-6B-GGUF/resolve/main/yi-chat-6b.Q4_K_M.gguf")12 13DEFAULT_MODEL_PATH = model_file14 15from llama_cpp import Llama16llm = Llama(model_path=model_file, model_type="mistral")17llm._token_eos = 718 19 20def predict(input, chatbot, max_length, top_p, temperature, history):21 chatbot.append((input, ""))22 response = ""23 history.append(input)24 25 for output in llm(input, stream=True, temperature=temperature, top_p=top_p, max_tokens=max_length, stop=["<|im_end|>"]):26 piece = output['choices'][0]['text']27 response += piece28 chatbot[-1] = (chatbot[-1][0], response)29 30 yield chatbot, history31 32 history.append(response)33 yield chatbot, history34 35 36def reset_user_input():37 return gr.update(value="")38 39 40def reset_state():41 return [], []42 43 44with gr.Blocks() as demo:45 gr.HTML("""<h1 align="center">Yi-6B-Chat by llama-cpp-python</h1>""")46 47 chatbot = gr.Chatbot()48 with gr.Row():49 with gr.Column(scale=4):50 user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=8)51 submitBtn = gr.Button("Submit", variant="primary")52 with gr.Column(scale=1):53 max_length = gr.Slider(0, 32048, value=2048, step=1.0, label="Maximum Length", interactive=True)54 top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)55 temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)56 emptyBtn = gr.Button("Clear History")57 58 history = gr.State([])59 60 submitBtn.click(61 predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history], show_progress=True62 )63 submitBtn.click(reset_user_input, [], [user_input])64 65 emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)66 67demo.queue().launch(share=False, inbrowser=True)