code: #!/usr/bin/env python3
  
  from flask import Flask, request
  from werkzeug.utils import secure_filename
  
  from llama_index import GPTSimpleVectorIndex, download_loader
  import json
  
  import secrets
  
  
  app = Flask(__name__)
  
  
  @app.route('/index', methods = ['GET', 'POST'])
  def upload_and_index():
      if request.method == "POST":
          f = request.files['file']
          filename = f"./uploads/{secure_filename(f.filename)}"
          f.save(filename)
  
          RDFReader = download_loader('RDFReader')
          document = RDFReader().load_data(file=filename)
  
          # avoid collisions of filenames
          data_id = secrets.token_hex(15)
  
          index = GPTSimpleVectorIndex(document)
          index.save_to_disk(f"{data_id}.json")
  
          return {'id': data_id}
  
  
  @app.route('/query')
  def query():
      args = request.args
      data_id = args.get('id')
      query_str = args.get('query')
      q_index = GPTSimpleVectorIndex.load_from_disk(f"{data_id}.json")
  
      result = q_index.query(f"{query_str} - return the answer and explanation in a JSON object")
      try:
          json_start = result.response.index('{')
          answer = json.loads(result.response[json_start:])
          answer.update({'success': True})
      except (ValueError, json.JSONDecodeError):
          answer = {'success': False, 'answer': result.response, 'explanation': ''}
  
      return json.dumps(answer)
  
  
  @app.route('/')
  def hello():
      return 'Hello, World!'
  
  
  def run_app():
      app.run(host='0.0.0.0', port=5050)
  
  
  if __name__ == '__main__':
      run_app()
  
apis: llama_index.GPTSimpleVectorIndex.load_from_disk llama_index.download_loader llama_index.GPTSimpleVectorIndex
extract_api: [((199, 214), 'flask.Flask', 'Flask', (['__name__'], {}), '(__name__)\n', (204, 214), False, 'from flask import Flask, request\n'), ((893, 947), 'llama_index.GPTSimpleVectorIndex.load_from_disk', 'GPTSimpleVectorIndex.load_from_disk', (['f"""{data_id}.json"""'], {}), "(f'{data_id}.json')\n", (928, 947), False, 'from llama_index import GPTSimpleVectorIndex, download_loader\n'), ((1342, 1360), 'json.dumps', 'json.dumps', (['answer'], {}), '(answer)\n', (1352, 1360), False, 'import json\n'), ((464, 492), 'llama_index.download_loader', 'download_loader', (['"""RDFReader"""'], {}), "('RDFReader')\n", (479, 492), False, 'from llama_index import GPTSimpleVectorIndex, download_loader\n'), ((608, 629), 'secrets.token_hex', 'secrets.token_hex', (['(15)'], {}), '(15)\n', (625, 629), False, 'import secrets\n'), ((647, 677), 'llama_index.GPTSimpleVectorIndex', 'GPTSimpleVectorIndex', (['document'], {}), '(document)\n', (667, 677), False, 'from llama_index import GPTSimpleVectorIndex, download_loader\n'), ((1119, 1159), 'json.loads', 'json.loads', (['result.response[json_start:]'], {}), '(result.response[json_start:])\n', (1129, 1159), False, 'import json\n'), ((388, 415), 'werkzeug.utils.secure_filename', 'secure_filename', (['f.filename'], {}), '(f.filename)\n', (403, 415), False, 'from werkzeug.utils import secure_filename\n')]