Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,128 +50,171 @@ def chatbot(user_input, history=[]):
|
|
| 50 |
history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}"))
|
| 51 |
return history, saved_chats
|
| 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 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
}
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
}
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
}
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
history.append((f"({topic}) You: {user_input}", f"Motivator Bot: {bot_response}"))
|
| 51 |
return history, saved_chats
|
| 52 |
|
| 53 |
+
# Custom HTML, CSS, and JavaScript
|
| 54 |
+
custom_html = """
|
| 55 |
+
<!DOCTYPE html>
|
| 56 |
+
<html lang="en">
|
| 57 |
+
<head>
|
| 58 |
+
<meta charset="UTF-8">
|
| 59 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 60 |
+
<title>Motivational Chatbot</title>
|
| 61 |
+
<style>
|
| 62 |
+
body {
|
| 63 |
+
font-family: 'Poppins', sans-serif;
|
| 64 |
+
background: #121212;
|
| 65 |
+
color: #f3f3f3;
|
| 66 |
+
margin: 0;
|
| 67 |
+
padding: 0;
|
| 68 |
+
display: flex;
|
| 69 |
+
justify-content: center;
|
| 70 |
+
align-items: center;
|
| 71 |
+
height: 100vh;
|
| 72 |
+
}
|
| 73 |
+
.container {
|
| 74 |
+
width: 90%;
|
| 75 |
+
max-width: 800px;
|
| 76 |
+
background: #1e1e1e;
|
| 77 |
+
border-radius: 15px;
|
| 78 |
+
box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.5);
|
| 79 |
+
overflow: hidden;
|
| 80 |
+
display: flex;
|
| 81 |
+
flex-direction: column;
|
| 82 |
+
}
|
| 83 |
+
header {
|
| 84 |
+
background: #282828;
|
| 85 |
+
padding: 20px;
|
| 86 |
+
text-align: center;
|
| 87 |
+
color: #ffffff;
|
| 88 |
+
border-bottom: 2px solid #ff6a95;
|
| 89 |
+
}
|
| 90 |
+
header h1 {
|
| 91 |
+
margin: 0;
|
| 92 |
+
font-size: 1.8rem;
|
| 93 |
+
}
|
| 94 |
+
header p {
|
| 95 |
+
margin: 5px 0 0;
|
| 96 |
+
font-size: 1rem;
|
| 97 |
+
color: #cccccc;
|
| 98 |
+
}
|
| 99 |
+
main {
|
| 100 |
+
flex: 1;
|
| 101 |
+
padding: 20px;
|
| 102 |
+
display: flex;
|
| 103 |
+
flex-direction: column;
|
| 104 |
+
}
|
| 105 |
+
.chat-container {
|
| 106 |
+
display: flex;
|
| 107 |
+
flex-direction: column;
|
| 108 |
+
height: 100%;
|
| 109 |
+
}
|
| 110 |
+
#chat-output {
|
| 111 |
+
flex: 1;
|
| 112 |
+
overflow-y: auto;
|
| 113 |
+
background: #212121;
|
| 114 |
+
border-radius: 10px;
|
| 115 |
+
padding: 10px;
|
| 116 |
+
margin-bottom: 10px;
|
| 117 |
+
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
|
| 118 |
+
}
|
| 119 |
+
.chat-input {
|
| 120 |
+
display: flex;
|
| 121 |
+
gap: 10px;
|
| 122 |
+
}
|
| 123 |
+
textarea {
|
| 124 |
+
flex: 1;
|
| 125 |
+
padding: 10px;
|
| 126 |
+
border-radius: 5px;
|
| 127 |
+
border: none;
|
| 128 |
+
background: #333;
|
| 129 |
+
color: #fff;
|
| 130 |
+
resize: none;
|
| 131 |
+
font-size: 1rem;
|
| 132 |
+
}
|
| 133 |
+
textarea:focus {
|
| 134 |
+
outline: none;
|
| 135 |
+
box-shadow: 0 0 5px #ff6a95;
|
| 136 |
+
}
|
| 137 |
+
button {
|
| 138 |
+
padding: 10px 20px;
|
| 139 |
+
background: linear-gradient(45deg, #ff6a95, #ff4b81);
|
| 140 |
+
border: none;
|
| 141 |
+
border-radius: 5px;
|
| 142 |
+
color: #fff;
|
| 143 |
+
font-weight: bold;
|
| 144 |
+
cursor: pointer;
|
| 145 |
+
transition: background 0.3s, transform 0.3s;
|
| 146 |
+
}
|
| 147 |
+
button:hover {
|
| 148 |
+
background: linear-gradient(45deg, #ff4b81, #ff6a95);
|
| 149 |
+
transform: scale(1.05);
|
| 150 |
+
}
|
| 151 |
+
footer {
|
| 152 |
+
background: #282828;
|
| 153 |
+
text-align: center;
|
| 154 |
+
padding: 10px;
|
| 155 |
+
color: #999;
|
| 156 |
+
font-size: 0.9rem;
|
| 157 |
+
border-top: 2px solid #ff6a95;
|
| 158 |
+
}
|
| 159 |
+
</style>
|
| 160 |
+
</head>
|
| 161 |
+
<body>
|
| 162 |
+
<div class="container">
|
| 163 |
+
<header>
|
| 164 |
+
<h1>✨ Motivational Chatbot ✨</h1>
|
| 165 |
+
<p>Your personal motivational speaker!</p>
|
| 166 |
+
</header>
|
| 167 |
+
<main>
|
| 168 |
+
<div class="chat-container">
|
| 169 |
+
<div id="chat-output"></div>
|
| 170 |
+
<div class="chat-input">
|
| 171 |
+
<textarea id="user-input" placeholder="Type your message here..."></textarea>
|
| 172 |
+
<button id="send-btn">Send</button>
|
| 173 |
+
</div>
|
| 174 |
+
</div>
|
| 175 |
+
</main>
|
| 176 |
+
<footer>
|
| 177 |
+
<p>Developed with ❤️ using OpenAI APIs</p>
|
| 178 |
+
</footer>
|
| 179 |
+
</div>
|
| 180 |
+
<script>
|
| 181 |
+
document.getElementById("send-btn").addEventListener("click", async () => {
|
| 182 |
+
const userInput = document.getElementById("user-input").value.trim();
|
| 183 |
+
if (!userInput) return;
|
| 184 |
+
|
| 185 |
+
// Display user input
|
| 186 |
+
const chatOutput = document.getElementById("chat-output");
|
| 187 |
+
const userMessage = `<div class="user-message"><strong>You:</strong> ${userInput}</div>`;
|
| 188 |
+
chatOutput.innerHTML += userMessage;
|
| 189 |
+
|
| 190 |
+
// Call backend
|
| 191 |
+
const response = await fetch("/chat", {
|
| 192 |
+
method: "POST",
|
| 193 |
+
headers: { "Content-Type": "application/json" },
|
| 194 |
+
body: JSON.stringify({ user_input: userInput })
|
| 195 |
+
});
|
| 196 |
+
const botResponse = await response.json();
|
| 197 |
+
|
| 198 |
+
// Display bot response
|
| 199 |
+
const botMessage = `<div class="bot-message"><strong>Bot:</strong> ${botResponse}</div>`;
|
| 200 |
+
chatOutput.innerHTML += botMessage;
|
| 201 |
+
|
| 202 |
+
// Clear input
|
| 203 |
+
document.getElementById("user-input").value = "";
|
| 204 |
+
chatOutput.scrollTop = chatOutput.scrollHeight;
|
| 205 |
+
});
|
| 206 |
+
</script>
|
| 207 |
+
</body>
|
| 208 |
+
</html>
|
| 209 |
+
"""
|
| 210 |
+
|
| 211 |
+
# Launch Gradio interface with custom frontend
|
| 212 |
+
interface = gr.Interface(
|
| 213 |
+
fn=chatbot,
|
| 214 |
+
inputs=[gr.Textbox(lines=2, label="Your Message"), gr.State()],
|
| 215 |
+
outputs=[gr.JSON(), gr.State()],
|
| 216 |
+
live=True
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
app = gr.HTML(custom_html, live=True)
|
| 220 |
+
interface.launch(share=True)
|