Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,28 @@ os.environ["HF_HOME"] = HUGGINGFACE_API_KEY # Set the Hugging Face API key
|
|
9 |
# Initialize the text generation pipeline (without passing api_key)
|
10 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
11 |
|
|
|
|
|
|
|
|
|
12 |
# Function to get response from the model
|
13 |
def get_chatbot_response(user_input):
|
14 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Generate response from the model
|
16 |
-
response = chatbot(
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
except Exception as e:
|
19 |
return f"Error: {str(e)}"
|
20 |
|
@@ -66,9 +82,6 @@ st.markdown("""
|
|
66 |
st.markdown('<div class="chat-header">Gemini Chatbot-Your AI Companion 💻</div>', unsafe_allow_html=True)
|
67 |
st.write("Powered by Hugging Face’s DialoGPT model for smart, engaging conversations. 🤖")
|
68 |
|
69 |
-
if "history" not in st.session_state:
|
70 |
-
st.session_state["history"] = []
|
71 |
-
|
72 |
with st.form(key="chat_form", clear_on_submit=True):
|
73 |
user_input = st.text_input("Your message here... ✍️", max_chars=2000, label_visibility="collapsed")
|
74 |
submit_button = st.form_submit_button("Send 🚀")
|
|
|
9 |
# Initialize the text generation pipeline (without passing api_key)
|
10 |
chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
11 |
|
12 |
+
# Initialize the conversation history
|
13 |
+
if "history" not in st.session_state:
|
14 |
+
st.session_state["history"] = []
|
15 |
+
|
16 |
# Function to get response from the model
|
17 |
def get_chatbot_response(user_input):
|
18 |
try:
|
19 |
+
# Prepare the conversation history for the model
|
20 |
+
conversation_history = ""
|
21 |
+
for user_input, response in st.session_state["history"]:
|
22 |
+
conversation_history += f"User: {user_input}\nBot: {response}\n"
|
23 |
+
|
24 |
+
# Add the current user input to the conversation
|
25 |
+
conversation_history += f"User: {user_input}\n"
|
26 |
+
|
27 |
# Generate response from the model
|
28 |
+
response = chatbot(conversation_history, max_length=1000, pad_token_id=50256)[0]["generated_text"]
|
29 |
+
|
30 |
+
# Remove the user input from the generated response (optional)
|
31 |
+
response = response[len(conversation_history):].strip()
|
32 |
+
|
33 |
+
return response
|
34 |
except Exception as e:
|
35 |
return f"Error: {str(e)}"
|
36 |
|
|
|
82 |
st.markdown('<div class="chat-header">Gemini Chatbot-Your AI Companion 💻</div>', unsafe_allow_html=True)
|
83 |
st.write("Powered by Hugging Face’s DialoGPT model for smart, engaging conversations. 🤖")
|
84 |
|
|
|
|
|
|
|
85 |
with st.form(key="chat_form", clear_on_submit=True):
|
86 |
user_input = st.text_input("Your message here... ✍️", max_chars=2000, label_visibility="collapsed")
|
87 |
submit_button = st.form_submit_button("Send 🚀")
|