| | import streamlit as st |
| | import requests |
| |
|
| | |
| | RASA_API_URL = "http://localhost:5005/webhooks/rest/webhook" |
| | chatbot_url = "https://nlprocessor.us/projectchatbot.html#chatbot" |
| |
|
| | |
| | iframe_code = f'<iframe src="{chatbot_url}" width="100%" height="600px" style="border:none;"></iframe>' |
| |
|
| | |
| | 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() |
| | 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"] = "" |
| |
|
| | |
| | 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() |
| |
|