Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. from pprint import pprint
  4.  
  5. import time
  6.  
  7. import datetime
  8. from telepot.loop import MessageLoop
  9.  
  10. import telepot
  11.  
  12. API_TOKEN = 'motherfucker'
  13.  
  14. file_name = "logs/{}.txt".format(datetime.datetime.now().strftime("%m_%d_%Y %H_%M_%S"))
  15.  
  16. script_to_run = '''
  17.                 LOGFILE="{}"
  18.                 (python -u pytorch-scripts/Example.py --datapath ./data/ {}) | (tee "$LOGFILE")'''
  19.  
  20. def parseStuff(text):
  21.     all_params = ""
  22.     text = text.encode('utf-8')
  23.     if ("-aug" in text):
  24.         index_of_first = text.find("-aug") + 5
  25.         index_of_last = text.find(" ", index_of_first) + 1
  26.         if (index_of_last == 0): index_of_last = len(text)
  27.         all_params += "--aug {} ".format(text[index_of_first: index_of_last])
  28.     if ("-cuda" in text):
  29.         all_params += "--cuda "
  30.     if ("-pretrained" in text):
  31.         all_params += "--pretrained "
  32.     if ("-batch_size" in text):
  33.         index_of_first = text.find("-batch_size") + 12
  34.         index_of_last = text.find(" ", index_of_first) + 1
  35.         if (index_of_last == 0): index_of_last = len(text)
  36.         all_params += "--batch_size {} ".format(text[index_of_first: index_of_last])
  37.     if ("-model" in text):
  38.         index_of_first = text.find("-model") + 7
  39.         index_of_last = text.find(" ", index_of_first) + 1
  40.         if (index_of_last == 0): index_of_last = len(text)
  41.         all_params += "--model {} ".format(text[index_of_first: index_of_last])
  42.     if ("-lr" in text):
  43.         index_of_first = text.find("-lr") + 4
  44.         index_of_last = text.find(" ", index_of_first) + 1
  45.         if (index_of_last == 0): index_of_last = len(text)
  46.         all_params += "--lr {} ".format(text[index_of_first: index_of_last])
  47.     if ("-epochs" in text):
  48.         index_of_first = text.find("-epochs") + 8
  49.         index_of_last = text.find(" ", index_of_first) + 1
  50.         if (index_of_last == 0): index_of_last = len(text)
  51.         all_params += "--epochs {} ".format(text[index_of_first: index_of_last])
  52.     if ("-tag" in text):
  53.         index_of_first = text.find("-tag") + 5
  54.         index_of_last = text.find(" ", index_of_first) + 1
  55.         if (index_of_last == 0): index_of_last = len(text)
  56.         all_params += "--tag {} ".format(text[index_of_first: index_of_last])
  57.  
  58.     return all_params
  59.  
  60.  
  61.  
  62. def handle(msg):
  63.     content_type, chat_type, chat_id = telepot.glance(msg)
  64.     all_params = ""
  65.     try:
  66.         all_params = parseStuff(msg['text'])
  67.         bot.sendMessage(chat_id, "Running...")
  68.         os.system(script_to_run.format(file_name, all_params))
  69.         bot.sendDocument(chat_id, open(file_name, "r"))
  70.         bot.sendMessage(chat_id, "Finish!")
  71.     except Exception as e:
  72.         bot.sendMessage(chat_id, "{}".format(e))
  73.         bot.sendMessage(chat_id, "Dead!")
  74.  
  75.  
  76. if __name__== "__main__":
  77.     bot = telepot.Bot(API_TOKEN)
  78.     MessageLoop(bot, handle).run_as_thread()
  79.     print("listening...")
  80.     while 1:
  81.         time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement