Spaces:
Sleeping
Sleeping
File size: 2,478 Bytes
d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 4e18b30 c9f0a44 ae714e8 c9f0a44 a619426 d74fb71 a619426 d74fb71 064bcd7 c9f0a44 d74fb71 c9f0a44 d74fb71 c9f0a44 d74fb71 42ad7dc d74fb71 a619426 d74fb71 2b4da4f d74fb71 a619426 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 90 91 92 93 94 |
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-7b-rlhf-gguf/resolve/main/ggml-model-q6_k.gguf?download=true",
"ggml-model-q6_k.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-q6_k.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):
print(history)
response = infer(history[-1][0])
history[-1][1] = ""
for character in response:
history[-1][1] += character
time.sleep(0.05)
yield history
def infer(question):
result = chain.run(text=question).strip()
#print(result)
return result+ "123"
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=350)
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()
|