myspace / app.py
manoj555's picture
Update app.py
5350e2e verified
raw
history blame
1.69 kB
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
from langchain_community.llms import HuggingFacePipeline
from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_core.memory import ConversationBufferMemory
# Load model and tokenizer
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# Create text-generation pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=10000, do_sample=True, truncation=True)
# Wrap with HuggingFacePipeline
llm = HuggingFacePipeline(pipeline=pipe)
# Prompt Template
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
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Chain
llm_chain = LLMChain(
llm=llm,
prompt=prompt,
memory=memory,
verbose=True
)
# Chat function
def get_text_response(user_message, history):
response = llm_chain.predict(user_message=user_message)
return response
# Gradio UI
demo = gr.ChatInterface(
fn=get_text_response,
examples=["How are you doing?", "What are your interests?", "Which places do you like to visit?"],
title="AI Chatbot",
description="A simple chatbot using LangChain + HuggingFace + Gradio",
theme="default",
chatbot=gr.Chatbot(label="Assistant", show_label=True)
)
if __name__ == "__main__":
demo.queue().launch(share=True)