|
import gradio as gr |
|
from langchain.llms import HuggingFacePipeline |
|
from langchain import LLMChain, PromptTemplate |
|
from langchain.memory import ConversationBufferMemory |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline |
|
|
|
|
|
model_name = "microsoft/DialoGPT-medium" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=1000, do_sample=True) |
|
|
|
|
|
llm = HuggingFacePipeline(pipeline=pipe) |
|
|
|
template = """You are a helpful assistant to answer user queries. |
|
{chat_history} |
|
User: {user_message} |
|
Chatbot:""" |
|
|
|
prompt = PromptTemplate( |
|
input_variables=["chat_history", "user_message"], template=template |
|
) |
|
|
|
memory = ConversationBufferMemory(memory_key="chat_history") |
|
|
|
llm_chain = LLMChain( |
|
llm=llm, |
|
prompt=prompt, |
|
verbose=True, |
|
memory=memory, |
|
) |
|
|
|
def get_text_response(user_message, history): |
|
response = llm_chain.predict(user_message=user_message) |
|
return response |
|
|
|
demo = gr.ChatInterface( |
|
get_text_response, |
|
examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.queue().launch(share=True, debug=True) |
|
|