File size: 2,886 Bytes
64c6e7a
 
 
 
a85678b
a2142e5
a85678b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64c6e7a
a85678b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6fd5a3c
a85678b
 
 
 
6fd5a3c
a85678b
 
 
 
 
 
 
 
 
bdedfa4
64c6e7a
a85678b
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import streamlit as st

from transformers import GPT2Tokenizer, GPT2LMHeadModel

# Function to generate a response
def generate_response(input_text):
    # Adjusted input to include the [Bot] marker
    #adjusted_input = f"{input_text} [Bot]"
    
    # Encode the adjusted input
    inputs = tokenizer(input_text, return_tensors="pt")

    # Generate a sequence of text with a slightly increased max_length to account for the prompt length
    output_sequences = model.generate(
        input_ids=inputs['input_ids'],
        attention_mask=inputs['attention_mask'],
        max_length=100,  # Adjusted max_length
        temperature=0.7,
        top_k=50,
        top_p=0.95,
        no_repeat_ngram_size=2,
        pad_token_id=tokenizer.eos_token_id,
        #early_stopping=True,
        do_sample=True
    )

    # Decode the generated sequence
    full_generated_text = tokenizer.decode(output_sequences[0], skip_special_tokens=True)

    # Extract the generated response after the [Bot] marker
    bot_response_start = full_generated_text.find('[Bot]') + len('[Bot]')
    bot_response = full_generated_text[bot_response_start:]
    
    # Trim the response to end at the last period within the specified max_length
    last_period_index = bot_response.rfind('.')
    if last_period_index != -1:
        bot_response = bot_response[:last_period_index + 1]

    return bot_response.strip()

# Load pre-trained model tokenizer (vocabulary) and model
model_name = 'KhantKyaw/Chat_GPT-2'
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

# Chat loop
print("Chatbot is ready. Type 'quit' to exit.")
while True:
    user_input = input("You: ")
    if user_input.lower() == "quit":
        break
    response = generate_response(user_input)
    print("Chatbot:", response)



st.title("Simple Streamlit Chatbot")

    # User input text box
    user_input = st.text_input("You: ", key="user_input")

    # Button to send the message
    if st.button("Send"):
        # Generating a response
        response = get_response(user_input)
        
        # Displaying the conversation
        # Here, we use st.session_state to keep track of the conversation
        if 'conversation' not in st.session_state:
            st.session_state.conversation = []
        
        # Append the user input and bot response to the conversation
        st.session_state.conversation.append("You: " + user_input)
        st.session_state.conversation.append("Bot: " + response)

        # Display each line in the conversation
        for line in st.session_state.conversation:
            st.text(line)




#prompt = st.chat_input(placeholder="Say Something!",key=None, max_chars=None, disabled=False, on_submit=None, args=None, kwargs=None)
#if prompt:
  # with st.chat_message(name="AI",avatar=None):
   # st.write(generate_response(prompt))