-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
51 lines (41 loc) · 1.52 KB
/
application.py
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
import signal
import sys
import logging
from run import shlok_ai_web
from flask import Flask, jsonify, request, send_from_directory
from flask_cors import CORS
import os
import json
app = Flask(__name__, static_folder="build")
port = int(os.environ.get("PORT", 5001))
CORS(app)
@app.route("/api/shloka-prompt", methods=["POST"])
def shloka_sage_ai():
try:
data = request.get_json()
prompt = data.get("prompt")
print("Prompt: ", prompt)
result = shlok_ai_web.shlokAI(prompt)
# If the result is an error, return the error message
if "Error" in result:
print("Error:", result)
logging.error("Error!!:", result)
return jsonify(Error=result)
# If the result is not an error, return the result
else:
# result_json = json.dumps(result)
print("Result:", result)
logging.info("Result is:", result)
return jsonify(Result=result)
except Exception as e:
print("Error:", e)
logging.error("Error!!:", e)
error_message = "Shlok-AI is currently unable to process your request. Please try again later."
return jsonify(Error=error_message)
def signal_handler(signal, frame):
print("You have requested to exit the program using Ctrl+C. Exiting.")
logging.info("You have requested to exit the program using Ctrl+C. Exiting.")
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
app.run(debug=True, host="0.0.0.0", port=port)