Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 718c4f2

Browse files
authored
models: support building against PythonKit (#347)
Enable use of PythonKit and Python, preferring PythonKit over Python.
1 parent cf54036 commit 718c4f2

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

FastStyleTransfer/Demo/ColabDemo.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@
7676
"\n",
7777
"// Imaging capabilities\n",
7878
"%include \"EnableIPythonDisplay.swift\"\n",
79-
"import Python\n",
79+
"#if canImport(PythonKit)\n",
80+
" import PythonKit\n",
81+
"#else",
82+
" import Python\n",
83+
"#endif",
8084
"let plt = Python.import(\"matplotlib.pyplot\")\n",
8185
"IPythonDisplay.shell.enable_matplotlib(\"inline\")\n",
8286
"\n",
@@ -357,4 +361,4 @@
357361
},
358362
"nbformat": 4,
359363
"nbformat_minor": 1
360-
}
364+
}

Gym/Blackjack/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import Python
15+
#if canImport(PythonKit)
16+
import PythonKit
17+
#else
18+
import Python
19+
#endif
1620
import TensorFlow
1721

1822
let iterationCount = 10000

Gym/CartPole/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import Python
15+
#if canImport(PythonKit)
16+
import PythonKit
17+
#else
18+
import Python
19+
#endif
1620
import TensorFlow
1721

1822
// Force unwrapping with `!` does not provide source location when unwrapping `nil`, so we instead

Gym/FrozenLake/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import Python
15+
#if canImport(PythonKit)
16+
import PythonKit
17+
#else
18+
import Python
19+
#endif
1620
import TensorFlow
1721

1822
// Force unwrapping with `!` does not provide source location when unwrapping `nil`, so we instead

Transformer/Model.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
// limitations under the License.
1414

1515
import TensorFlow
16-
import Python
16+
#if canImport(PythonKit)
17+
import PythonKit
18+
#else
19+
import Python
20+
#endif
1721

1822
struct TimeDistributed: Layer {
1923
var dense: Dense<Float>

Transformer/main.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
import Foundation
1616
import ModelSupport
17-
import Python
17+
#if canImport(PythonKit)
18+
import PythonKit
19+
#else
20+
import Python
21+
#endif
1822
import TensorFlow
1923
import Transformer
2024

0 commit comments

Comments
 (0)