File size: 3,848 Bytes
99abf93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b6e6dd
a8acb9b
 
 
 
 
99abf93
a8acb9b
 
 
 
 
 
99abf93
a8acb9b
99abf93
a8acb9b
99abf93
 
 
a8acb9b
99abf93
 
a8acb9b
 
99abf93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8acb9b
99abf93
a8acb9b
 
99abf93
 
 
 
 
 
 
 
 
 
 
a8acb9b
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# import streamlit as st
# from transformers import AutoModelForCausalLM, AutoTokenizer

# # Load the model and tokenizer
# @st.cache_resource
# def load_model_and_tokenizer():
#     model_name = "microsoft/DialoGPT-medium"  # Replace with your chosen model
#     tokenizer = AutoTokenizer.from_pretrained(model_name)
#     model = AutoModelForCausalLM.from_pretrained(model_name)
#     return tokenizer, model

# tokenizer, model = load_model_and_tokenizer()

# # Streamlit App
# st.title("General Chatbot")
# st.write("A chatbot powered by an open-source model from Hugging Face.")

# # Initialize the conversation
# if "conversation_history" not in st.session_state:
#     st.session_state["conversation_history"] = []

# # Input box for user query
# user_input = st.text_input("You:", placeholder="Ask me anything...", key="user_input")

# if st.button("Send") and user_input:
#     # Append user input to history
#     st.session_state["conversation_history"].append({"role": "user", "content": user_input})
    
#     # Tokenize and generate response
#     input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
#     chat_history_ids = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
#     response = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)

#     # Append model response to history
#     st.session_state["conversation_history"].append({"role": "assistant", "content": response})

# # Display the conversation
# for message in st.session_state["conversation_history"]:
#     if message["role"] == "user":
#         st.write(f"**You:** {message['content']}")
#     else:
#         st.write(f"**Bot:** {message['content']}")

import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load the model and tokenizer
@st.cache_resource
def load_model_and_tokenizer():
    model_name = "microsoft/DialoGPT-medium"  # You can replace with any Hugging Face conversational model
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForCausalLM.from_pretrained(model_name)
    return tokenizer, model

tokenizer, model = load_model_and_tokenizer()

# Streamlit App Title
st.title("General Chatbot")
st.markdown("This chatbot is powered by an open-source model from Hugging Face. Feel free to ask me anything!")

# Initialize the session state for conversation history
if "chat_history" not in st.session_state:
    st.session_state["chat_history"] = ""

# User Input Section
user_input = st.text_input("You:", placeholder="Type your message here...", key="user_input")

if st.button("Send") and user_input:
    # Add user input to the conversation history
    st.session_state["chat_history"] += f"User: {user_input}\n"

    # Tokenize the input with conversation history
    input_ids = tokenizer.encode(st.session_state["chat_history"], return_tensors="pt")

    # Generate a response
    chat_history_ids = model.generate(
        input_ids,
        max_length=1500,  # Allow long responses
        min_length=200,   # Ensure responses are not too short
        temperature=1.0,  # Adjust for creativity
        top_p=0.9,        # Nucleus sampling for focused responses
        repetition_penalty=1.2,  # Penalize repeated phrases
        pad_token_id=tokenizer.eos_token_id
    )
    
    # Decode the model's response
    response = tokenizer.decode(chat_history_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)

    # Add the response to the conversation history
    st.session_state["chat_history"] += f"Bot: {response}\n"

    # Display the conversation
    st.markdown(f"**You:** {user_input}")
    st.markdown(f"**Bot:** {response}")

# Display Full Conversation History
st.divider()
st.subheader("Conversation History:")
st.text(st.session_state["chat_history"])