Spaces:
Running
Running
from flask import Flask, request, jsonify | |
import bingChat | |
import api_info | |
import json | |
app = Flask(__name__) | |
def initial(): | |
return '<pre>' + api_info.default_info + '</pre>' | |
def available_models(): | |
return jsonify(api_info.available_models) | |
def endpoints(): | |
return jsonify(api_info.endpoint) | |
def developer_info(): | |
return jsonify(api_info.developer_info) | |
def generate(): | |
try: | |
if request.method == 'POST': | |
data = request.get_json(force=True) | |
conversation_history = data.get('conversation_history') | |
realtime = data.get('realtime', False) | |
md_format = data.get('md_format', True) | |
convo_tone = data.get('convo_tone', 'Balanced') | |
else: # GET request | |
conversation_history = request.args.get('conversation_history') | |
realtime = request.args.get('realtime', 'false').lower() == 'true' | |
md_format = request.args.get('md_format', 'true').lower() == 'true' | |
convo_tone = request.args.get('convo_tone', 'Balanced') | |
if conversation_history: | |
# Parse the conversation_history as JSON if it's a string | |
if isinstance(conversation_history, str): | |
try: | |
conversation_history = json.loads(conversation_history) | |
except json.JSONDecodeError: | |
conversation_history = [{"role": "user", "content": conversation_history}] | |
response = bingChat.ddc_bing_converse( | |
conv_history=conversation_history, | |
realtime=realtime, | |
md_format=md_format, | |
convo_tone=convo_tone | |
) | |
return jsonify(response), 200 | |
else: | |
return jsonify(api_info.error_message), 400 | |
except Exception as e: | |
return jsonify({"error": str(e)}), 400 | |
if __name__ == '__main__': | |
app.run(debug=True) | |