File size: 1,621 Bytes
03813a3
2a26a73
ccd354a
 
2a26a73
ccd354a
91a2093
 
03813a3
 
2a26a73
91a2093
 
2a26a73
 
 
8d768b7
 
ccd354a
2a26a73
 
03813a3
 
67881e0
96c1712
9af1238
f092898
 
96c1712
ccd354a
 
f092898
 
 
 
 
91a2093
f092898
e777dab
 
f092898
 
 
 
 
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
from transformers import GPT2Tokenizer, GPT2LMHeadModel
from langchain import PromptTemplate, LLMChain
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
import gradio as gr

# Load the tokenizer and model
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
model = GPT2LMHeadModel.from_pretrained("gpt2")

template = """Question: {question}
------------------
Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])

# Create a memory module with a maximum capacity of 1000 items
memory = ConversationBufferMemory()
# Callbacks support token-wise streaming
callbacks = [StreamingStdOutCallbackHandler()]
# Instantiate the LLMChain with the model and tokenizer
llm = LLMChain(model=model, tokenizer=tokenizer, callbacks=callbacks, verbose=True)

conversation = ConversationChain(llm=llm, memory=memory, callbacks=callbacks, prompt=prompt)

# Define the Gradio interface
def chatbot_interface(input_text):
    response = conversation.predict(input_text)
    memory.chat_memory.add_user_message(input_text)
    memory.chat_memory.add_ai_message(response)
    return response

# Define the Gradio app
gradio_app = gr.Interface(
    fn=chatbot_interface,
    inputs=gr.inputs.Textbox(label="Say something..."),
    outputs=gr.outputs.Textbox(),
    title="ConversationChain Chatbot",
    description="A chatbot interface powered by ConversationChain and Hugging Face.",
)

# Run the Gradio app
if __name__ == "__main__":
    gradio_app.run()