import streamlit as st import requests # Rasa chatbot's API endpoint (adjust if needed) RASA_API_URL = "http://localhost:5005/webhooks/rest/webhook" chatbot_url = "https://nlprocessor.us/projectchatbot.html#chatbot" # Embed the chatbot using an iframe iframe_code = f'' # Use Streamlit's built-in components module st.components.v1.html(iframe_code, height=600) def send_message_to_rasa(message): """Sends a message to the Rasa chatbot and returns the response.""" payload = {"sender": "user", "message": message} try: response = requests.post(RASA_API_URL, json=payload) response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx) return response.json() except requests.exceptions.RequestException as e: return [{"text": f"Error communicating with Rasa: {e}"}] def main(): st.title("My Rasa Chatbot") if "chat_history" not in st.session_state: st.session_state["chat_history"] = [] user_input = st.text_input("You:", key="user_input") if user_input: st.session_state["chat_history"].append({"sender": "user", "text": user_input}) rasa_response = send_message_to_rasa(user_input) for response in rasa_response: if "text" in response: st.session_state["chat_history"].append({"sender": "bot", "text": response["text"]}) st.session_state["user_input"] = "" # Clear the input box # Display chat history for message in st.session_state["chat_history"]: if message["sender"] == "user": st.write(f"**You:** {message['text']}") else: st.write(f"**Bot:** {message['text']}") if __name__ == "__main__": main()