Utkarsh Verma commited on
Commit
834001a
·
1 Parent(s): a98d143

Initial chatbot deployment

Browse files
Files changed (3) hide show
  1. chatbot.py +32 -0
  2. index.html +32 -0
  3. requirements.txt +3 -0
chatbot.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+ from flask import Flask, request, jsonify, render_template
4
+
5
+ app = Flask(__name__)
6
+
7
+ # Set your OpenAI API key (Replace 'your-api-key' with your actual key)
8
+ openai.api_key = "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"
9
+
10
+ @app.route('/')
11
+ def home():
12
+ return render_template('index.html')
13
+
14
+ @app.route('/chat', methods=['POST'])
15
+ def chat():
16
+ user_message = request.json.get("message")
17
+
18
+ if not user_message:
19
+ return jsonify({"error": "Empty message received"})
20
+
21
+ try:
22
+ response = openai.ChatCompletion.create(
23
+ model="gpt-4",
24
+ messages=[{"role": "user", "content": user_message}]
25
+ )
26
+ reply = response["choices"][0]["message"]["content"]
27
+ return jsonify({"reply": reply})
28
+ except Exception as e:
29
+ return jsonify({"error": str(e)})
30
+
31
+ if __name__ == '__main__':
32
+ app.run(host='0.0.0.0', port=7860)
index.html ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>AI Chatbot</title>
7
+ <script>
8
+ async function sendMessage() {
9
+ let userInput = document.getElementById("user-input").value;
10
+ let chatBox = document.getElementById("chat-box");
11
+
12
+ chatBox.innerHTML += "<p><b>You:</b> " + userInput + "</p>";
13
+
14
+ let response = await fetch("/chat", {
15
+ method: "POST",
16
+ headers: { "Content-Type": "application/json" },
17
+ body: JSON.stringify({ message: userInput }),
18
+ });
19
+
20
+ let data = await response.json();
21
+ chatBox.innerHTML += "<p><b>Bot:</b> " + (data.reply || "Error in response") + "</p>";
22
+ document.getElementById("user-input").value = "";
23
+ }
24
+ </script>
25
+ </head>
26
+ <body>
27
+ <h1>AI Chatbot</h1>
28
+ <div id="chat-box" style="border:1px solid #ccc; height:300px; overflow:auto;"></div>
29
+ <input type="text" id="user-input" placeholder="Type a message...">
30
+ <button onclick="sendMessage()">Send</button>
31
+ </body>
32
+ </html>
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ openai
3
+ gunicorn