Guest User

Untitled

a guest
Jan 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. from flask import Flask
  2. import tensorflow as tf
  3. import numpy as np
  4. import os
  5. import time
  6. import datetime
  7. import data_helpers
  8. from text_cnn import TextCNN
  9. from tensorflow.contrib import learn
  10.  
  11. app = Flask(__name__)
  12.  
  13. @app.route('/check/<estring>')
  14. def hello_world(estring):
  15. return check(estring)
  16.  
  17. tf.flags.DEFINE_string("checkpoint_dir", "runs/1483816176/checkpoints/", "Checkpoint directory from training run")
  18. tf.flags.DEFINE_boolean("eval_train", True, "Evaluate on all training data")
  19. tf.flags.DEFINE_boolean("allow_soft_placement", True, "Allow device soft device placement")
  20. tf.flags.DEFINE_boolean("log_device_placement", False, "Log placement of ops on devices")
  21. FLAGS = tf.flags.FLAGS
  22. FLAGS._parse_flags()
  23. vocab_path = os.path.join(FLAGS.checkpoint_dir, "..", "vocab")
  24. vocab_processor = learn.preprocessing.VocabularyProcessor.restore(vocab_path)
  25. checkpoint_file = tf.train.latest_checkpoint(FLAGS.checkpoint_dir)
  26. graph = tf.Graph()
  27. def check(estring):
  28. x_raw = [estring]
  29. x_test = np.array(list(vocab_processor.transform(x_raw)))
  30. with graph.as_default():
  31. session_conf = tf.ConfigProto(
  32. allow_soft_placement=FLAGS.allow_soft_placement,
  33. log_device_placement=FLAGS.log_device_placement)
  34. sess = tf.Session(config=session_conf)
  35. with sess.as_default():
  36. # Load the saved meta graph and restore variables
  37. saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
  38. saver.restore(sess, checkpoint_file)
  39. # Get the placeholders from the graph by name
  40. input_x = graph.get_operation_by_name("input_x").outputs[0]
  41. # input_y = graph.get_operation_by_name("input_y").outputs[0]
  42. dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
  43. # Tensors we want to evaluate
  44. predictions = graph.get_operation_by_name("output/predictions").outputs[0]
  45. # Generate batches for one epoch
  46. batches = data_helpers.batch_iter(list(x_test), 1, 1, shuffle=False)
  47. # Collect the predictions here
  48. all_predictions = []
  49. for x_test_batch in batches:
  50. batch_predictions = sess.run(predictions, {input_x: x_test_batch, dropout_keep_prob: 1.0})
  51. all_predictions = np.concatenate([all_predictions, batch_predictions])
  52. if(all_predictions[0]==1):
  53. return "L"
  54. else:
  55. return "C"
Add Comment
Please, Sign In to add comment