-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
60 lines (40 loc) · 1.41 KB
/
app.py
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
model_path = "tiiuae/falcon-7b-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(
model_path, trust_remote_code=True, torch_dtype=torch.bfloat16, device_map="auto"
)
from instruct_pipeline import InstructionTextGenerationPipeline
pipeline = InstructionTextGenerationPipeline(
model=model,
tokenizer=tokenizer,
max_new_tokens=128,
return_full_text=True,
task="text-generation",
)
from langchain.llms import HuggingFacePipeline
local_llm = HuggingFacePipeline(pipeline=pipeline)
from langchain import ConversationChain
from langchain.memory import ConversationBufferMemory
memory=ConversationBufferMemory()
conversation_buff = ConversationChain(llm=local_llm, memory=memory)
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
def user(user_message, history):
return "", history + [[user_message, None]]
def bot(history):
history[-1][1] = conversation_buff(history[-1][0])["response"]
return history
def reset():
memory.clear()
pass
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(reset, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch()