Spaces:
Sleeping
Sleeping
File size: 2,408 Bytes
d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 17be8c2 97830bc 17be8c2 d74fb71 17be8c2 d74fb71 17be8c2 4e18b30 17be8c2 97830bc 17be8c2 ae714e8 17be8c2 a619426 d74fb71 a619426 d74fb71 3ae4433 e9ba98e 97830bc 9caaf6e 76de1fe e9ba98e d74fb71 a619426 d74fb71 2b4da4f d74fb71 a619426 35a2a1f d74fb71 a619426 d74fb71 a619426 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import gradio as gr
import os
import time
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
from langchain_community.llms import LlamaCpp
from langchain_experimental.chat_models import Llama2Chat
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain.schema import SystemMessage
import urllib
urllib.request.urlretrieve(
"https://huggingface.co/hfl/chinese-alpaca-2-1.3b-gguf/resolve/main/ggml-model-q8_0.gguf?download=true",
"ggml-model-q8_0.gguf"
)
template_messages = [
SystemMessage(content="你是一名软件工程师,你的名字叫做贺英旭。请你以这个身份回答以下问题!"),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{text}"),
]
prompt_template = ChatPromptTemplate.from_messages(template_messages)
llm = LlamaCpp(
model_path="ggml-model-q8_0.gguf",
temperature=0.75,
max_tokens=64
)
model = Llama2Chat(llm=llm)
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
chain = LLMChain(llm=model, prompt=prompt_template, memory=memory)
def add_text(history, text):
history = history + [(text, None)]
return history, ""
def bot(history):
# session based memory
if len(history) == 1:
chain.memory.clear()
history[-1][1] = ""
user_message = history[-1][0]
for chunk in chain.stream({"text": user_message}):
history[-1][1] += chunk["text"]
yield history
css = """
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
"""
title = """
<div style="text-align: center;max-width: 700px;">
<h1>Chat with Yingxu</h1>
<p style="text-align: center;">Free feel to talk about anything :)</p>
</div>
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.HTML(title)
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=300)
question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
submit_btn = gr.Button("Send Message")
question.submit(add_text, [chatbot, question], [chatbot, question]).then(
bot, chatbot, chatbot
)
submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
bot, chatbot, chatbot)
demo.launch()
|