omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Shortcut error: Could not run Run Pythonista Script

    Pythonista
    3
    8
    295
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Y
      yaroslav_ya_ya last edited by

      Could not run Run
      Pythonista Script
      The operation took too long to complete. Please try again.

      Script is about to make long request to OpenAI. So it’s definitely not heavy, but long one.

      The script on what shortcuts failed.

      
      import requests
      import sys
      import json
      import shortcuts
      import certifi
      
      token = 'sk-zszs'
      
      settings = {
          'model': 'text-davinci-003',
          'temperature': 0.7,
          'max_tokens': 1000,
          'top_p': 1,
          'frequency_penalty': 0,
          'presence_penalty': 0,
      }
      
      def exec_net_request(connect):
          try:
              completion = json.loads(connect.text) 
              return completion['choices'][0]['text']
          except KeyError:
              pront("Exception: " + str(completion))
              return
      
          except Exception as ex:
              print("Exception: " + str(ex))
              return
      
      def complete(token, text, settings):
          conn = requests.session()
      
          payload = {
              "prompt": text,
              "model": settings["model"],
              "temperature": settings["temperature"],
              "max_tokens": settings["max_tokens"],
              "top_p": settings["top_p"],
              "frequency_penalty": settings["frequency_penalty"],
              "presence_penalty": settings["presence_penalty"]
          }
          
          headers = {
              'Content-Type': "application/json",
              'Authorization': 'Bearer {}'.format(token),
              'cache-control': "no-cache",
          }
      
          conn.headers.update(headers)
          resp = conn.post("https://api.openai.com/v1/completions", json=payload)
          return exec_net_request(connect=resp)
      
      
      if __name__ == '__main__':
          text = ' '.join([text for index, text in enumerate(sys.argv) if index > 0])
          answer = complete(token, text, settings)
          print(answer)
      
      
      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @yaroslav_ya_ya last edited by

        @yaroslav_ya_ya have you tried running this from pythonista? How long does it take to return?

        Y 1 Reply Last reply Reply Quote 0
        • Y
          yaroslav_ya_ya @JonB last edited by yaroslav_ya_ya

          @JonB yep, I’ve actually developed and tested it in it, in foreground everything goes well. And about time it depends, but usually up to 5 to 10 seconds, quite rare up to 15, but it’s subjectively, haven’t measured its properly. Anyway, it’s always feels like even when it succeed, it’s like made it at the last second before being killed.

          And again, some there’s a huge neural net working on the back end, it takes some time anytime. Like it never responded faster than several seconds, it’s computing that much.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @yaroslav_ya_ya last edited by

            @yaroslav_ya_ya so, I think there might be a 10 second limit for Shortcuts. (You could try a simple script which simply time.sleep's for various amount of time).

            It might be possible for you to continue running the script in the background -- kick off a thread and return to the script, then wait a few seconds, and then check back for results. I'm not sure if that actually would work.

            I wonder if you could use shortcuts to simple open a URL scheme in pythonista, then have pythonista call another Shortcut with the result. Or, set a notification with a URL scheme so pythonista opens in the foreground.

            I also wonder ... Pythonista probably takes several seconds to spin up the first time. But faster the second. So I wonder if you called a quick script just to return, then call the real worker second. Might have a few seconds off the time.

            Y 2 Replies Last reply Reply Quote 0
            • Y
              yaroslav_ya_ya @JonB last edited by

              @JonB actually all those stuff are as complex as write my own iOS app with SwiftUI and native call to the server. I mean it’s not worth it to test those workarounds in an any given app. But thanks for trying to help anyway

              1 Reply Last reply Reply Quote 0
              • Y
                yaroslav_ya_ya @JonB last edited by

                @JonB I’ve tested just now and it seems it’s not about time, but about memory footprint. The script above ( I still not changed it) fails with the same error but within 2-3 seconds and while I’m at shortcuts app. So I guess that error are not preside one. And knowing how apple are greedy about resources my shot is that sometimes Pythonista eats too much memory.

                Just for the record the very right before it crashed it succeed with quite a longer call that it was on fail.

                JonB 1 Reply Last reply Reply Quote 0
                • JonB
                  JonB @yaroslav_ya_ya last edited by

                  @yaroslav_ya_ya right. Found this:
                  https://developer.apple.com/forums/thread/655189
                  And this
                  https://developer.apple.com/videos/play/wwdc2020/10073/

                  Looks like it might be both time (10sec) and memory (30MB).

                  It seems like there is a method that @omz might be able to add to add a handler for in-app intents, and some sort of plug in system for handling them in python.

                  Y 1 Reply Last reply Reply Quote 0
                  • Y
                    yaroslav_ya_ya @JonB last edited by

                    @JonB very much looking forward to this. I’m so against to getting into js for scripting things up on my iOS devices, so the Pythonista looks like only option here.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors