PodzSphereChat / app.py
Ablepodz's picture
Update app.py
ebb4774 verified
from flask import Flask, render_template_string, request, jsonify
import random
app = Flask(__name__)
class ChatBot:
def __init__(self, name="Sophia"):
self.name = name
self.responses = {
"greeting": [
"Hey there! 👋",
"Hello, how’s your day going?",
"Hi! What’s on your mind?"
],
"farewell": [
"Goodbye 👋",
"Catch you later!",
"See you soon!"
],
"default": [
"Interesting… tell me more.",
"Hmm, I see. Go on.",
"That’s something to think about."
]
}
def understand(self, user_input):
text = user_input.lower()
if any(word in text for word in ["hi", "hello", "hey"]):
return "greeting"
elif any(word in text for word in ["bye", "goodnight", "see you"]):
return "farewell"
else:
return "default"
def respond(self, user_input):
intent = self.understand(user_input)
return random.choice(self.responses[intent])
bot = ChatBot("Sophia")
# ---------------- HTML Template ----------------
page = """
<!DOCTYPE html>
<html>
<head>
<title>{{ bot_name }} ChatBot</title>
<style>
body { font-family: Arial, sans-serif; background: #f4f4f9; }
#chatbox { width: 100%; max-width: 600px; margin: 30px auto; padding: 20px;
background: white; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.2); }
#messages { height: 400px; overflow-y: auto; border: 1px solid #ddd; padding: 10px; border-radius: 5px; }
.msg { margin: 5px 0; }
.user { color: blue; }
.bot { color: green; }
#inputArea { margin-top: 10px; display: flex; }
#userInput { flex: 1; padding: 10px; }
#sendBtn { padding: 10px 20px; }
</style>
</head>
<body>
<div id="chatbox">
<h2>{{ bot_name }} 🤖</h2>
<div id="messages">
<div class="msg bot">{{ bot_name }}: Hi! I'm {{ bot_name }}. Type 'quit' to end.</div>
</div>
<div id="inputArea">
<input type="text" id="userInput" placeholder="Type your message..." autofocus>
<button id="sendBtn">Send</button>
</div>
</div>
<script>
const sendBtn = document.getElementById("sendBtn");
const userInput = document.getElementById("userInput");
const messages = document.getElementById("messages");
function addMessage(sender, text) {
const div = document.createElement("div");
div.classList.add("msg", sender);
div.textContent = sender === "user" ? "You: " + text : "{{ bot_name }}: " + text;
messages.appendChild(div);
messages.scrollTop = messages.scrollHeight;
}
async function sendMessage() {
const text = userInput.value.trim();
if (!text) return;
addMessage("user", text);
userInput.value = "";
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: text })
});
const data = await response.json();
addMessage("bot", data.reply);
}
sendBtn.addEventListener("click", sendMessage);
userInput.addEventListener("keypress", function(e) {
if (e.key === "Enter") sendMessage();
});
</script>
</body>
</html>
"""
# ---------------- Routes ----------------
@app.route("/")
def home():
return render_template_string(page, bot_name=bot.name)
@app.route("/chat", methods=["POST"])
def chat():
user_msg = request.json.get("message", "")
if user_msg.lower() in ["quit", "exit"]:
reply = "It was nice chatting with you. Bye!"
else:
reply = bot.respond(user_msg)
return jsonify({"reply": reply})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)