import mdtex2html
from flask import Flask, request, jsonify
from chat import converse
import json
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def home():
try:
par = request.get_json()
if not par:
return jsonify({"error": "Invalid JSON input"}), 400
conversation = par.get('conversation')
provider = par.get('provider')
model = par.get('model')
api = par.get('api')
if not all([conversation, provider, model, api]):
return jsonify({"error": "Missing required parameters"}), 400
load = json.loads(converse(conversation, provider, model, api))
load['DirectResult'] = load['content']
try:
load['content'] = mdtex2html.convert(load['content'])
except Exception as e:
return jsonify({"error": f"Error converting content: {str(e)}"}), 500
toreturn = json.dumps(load, indent=4)
# print(toreturn)
return toreturn, 200
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True, port=1777)