-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatboyt.py
More file actions
83 lines (69 loc) · 2.3 KB
/
chatboyt.py
File metadata and controls
83 lines (69 loc) · 2.3 KB
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
import streamlit as st
import openai
import requests
from streamlit_chat import message
# Set up OpenAI API key
openai.api_key = "your-api-key-here"
# Webhook URL for n8n workflow
WEBHOOK_URL = "https://your-n8n-instance.com/webhook/chatbot"
# Streamlit App UI
st.set_page_config(page_title="Branded AI Chatbot", page_icon="🤖", layout="wide")
# Custom branding
st.markdown(
"""
<style>
.stApp {
background-color: #f5f7fa;
}
.chat-container {
background: linear-gradient(135deg, #0072ff, #00c6ff);
border-radius: 15px;
padding: 10px;
color: white;
font-weight: bold;
}
.stTextInput {
border-radius: 10px;
border: 2px solid #0072ff;
}
.stButton>button {
background: #0072ff;
color: white;
border-radius: 10px;
font-size: 16px;
}
</style>
""",
unsafe_allow_html=True
)
st.title("🚀 Branded AI Chatbot")
st.markdown("**Ask me anything! Your AI-powered assistant is here.**")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state["messages"] = []
# Display chat history
for i, msg in enumerate(st.session_state["messages"]):
message(msg["content"], is_user=msg["role"] == "user", key=str(i))
# User input
user_input = st.text_input("Type your message:", "", key="user_input")
if st.button("Send") and user_input:
# Add user message to chat history
st.session_state["messages"].append({"role": "user", "content": user_input})
# Call n8n webhook to fetch data
webhook_response = requests.post(WEBHOOK_URL, json={"message": user_input})
if webhook_response.status_code == 200:
webhook_data = webhook_response.json().get("response", "No response from webhook")
else:
webhook_data = "Error fetching data from webhook"
# Get AI response
response = openai.ChatCompletion.create(
model="gpt-4",
messages=st.session_state["messages"]
)
ai_message = response["choices"][0]["message"]["content"]
# Combine AI response with webhook data
final_response = f"{ai_message}\n\n[Webhook Data]: {webhook_data}"
# Add AI response to chat history
st.session_state["messages"].append({"role": "assistant", "content": final_response})
# Refresh the page
st.rerun()