File size: 1,405 Bytes
f4ec833 7dda322 aaa37fd 49e706c f4ec833 aaa37fd 49e706c f4ec833 aaa37fd f4ec833 aaa37fd 49e706c f4ec833 49e706c f4ec833 49e706c aaa37fd 49e706c f4ec833 aaa37fd f4ec833 49e706c aaa37fd 49e706c f4ec833 49e706c f4ec833 aaa37fd f4ec833 aaa37fd 5350e2e aaa37fd |
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 |
import gradio as gr
from langchain_community.llms import HuggingFacePipeline
from langchain import LLMChain, PromptTemplate
from langchain.memory import ConversationBufferMemory
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
# Load a free model from Hugging Face
model_name = "microsoft/DialoGPT-medium" # Or try "tiiuae/falcon-rw-1b" or "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Create pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=1000, do_sample=True)
# Wrap with HuggingFacePipeline
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)
|