title: Flask context error when trying to serve a tensorflow model
body: <p>I have a simple flask app to serve a tensorflow model. As far as I am aware, I have ironed out all of the other errors. But when it comes time to run the app I get this error:</p>
  
  <pre><code>RuntimeError: Working outside of request context.
  
  Traceback (most recent call last):
    File "app.py", line 61, in &lt;module&gt;
      if request.args:
    File "/home/m0oN/.local/lib/python3.5/site-packages/werkzeug/local.py", 
  line 347, in __getattr__
      return getattr(self._get_current_object(), name)
    File "/home/m0oN/.local/lib/python3.5/site-packages/werkzeug/local.py", 
  line 306, in _get_current_object
      return self.__local()
    File "/home/m0oN/.local/lib/python3.5/site-packages/flask/globals.py", 
  line 37, in _lookup_req_object
      raise RuntimeError(_request_ctx_err_msg)
  RuntimeError: Working outside of request context.
  
  This typically means that you attempted to use functionality that needed
  an active HTTP request.  Consult the documentation on testing for
  information about how to avoid this problem.
  </code></pre>
  
  <p>I think the main problem lies in here:</p>
  
  <pre><code>if __name__ == '__main__':
      file_name = 'steph.jpg'
      model_file = 'thotornot_graph.pb'
      lable_file = 'retrained_labels.txt'
      input_height = 299
      input_width = 299
      input_mean = 128
      input_std = 128
      input_layer = "Mul"
      output_layer = "final_result"
  
      if request.args:
          file_name = request.args
  
      graph = load_graph(model_file)
      t = read_tensor_from_image_file(file_name,
                                  input_height=input_height,
                                  input_width=input_width,
                                  input_mean=input_mean,
                                  input_std=input_std)
  
      input_name = 'import/' + input_layer
      output_name = 'import/' + output_layer
      input_operation = graph.get_operation_by_name(input_name);
      output_operation = graph.get_operation_by_name(output_name);
  
      with tf.Session(graph=graph) as sess:
          start = time.time()
          results = sess.run(output_operation.outputs[0],
                          {input_operation.outputs[0]: t})
          end = time.time()
      results = np.squeeze(results)
  
      top_k = results.argsort()[-5:][::-1]
      lables = load_lables(lable_file)
  
      print('\nEvaluation time (1-image): {:.3f}s\n'.format(end-start))
  
      for i in top_k:
          print(lables[i], results[i])
  
  app.run(localhost, 8081)
  </code></pre>
  
  <p>A link to my code full is here <a href="https://paste.ee/p/ztYKg" rel="nofollow noreferrer">https://paste.ee/p/ztYKg</a></p>
  
  <p>Does anyone have a solution?</p>