File size: 7,488 Bytes
85c036f
26bead7
 
85c036f
 
 
26bead7
 
 
 
 
 
7a59f60
85c036f
 
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69e927c
26bead7
69e927c
26bead7
69e927c
 
 
 
 
 
 
 
 
 
 
26bead7
 
 
69e927c
26bead7
69e927c
26bead7
69e927c
 
 
 
 
 
 
 
 
 
 
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a06565a
26bead7
 
 
 
 
 
 
69e927c
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69e927c
26bead7
 
 
 
 
 
69e927c
26bead7
 
 
 
 
 
69e927c
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69e927c
26bead7
69e927c
26bead7
69e927c
26bead7
 
69e927c
26bead7
69e927c
26bead7
 
 
 
 
 
69e927c
 
26bead7
 
69e927c
 
 
 
 
 
26bead7
69e927c
26bead7
 
 
 
 
69e927c
26bead7
 
 
 
 
 
69e927c
 
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69e927c
 
26bead7
 
 
 
 
 
 
 
 
 
69e927c
a06565a
 
 
 
 
 
 
 
69e927c
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
---
title: Python C Extension Generator
app_file: app.py
sdk: gradio
sdk_version: 6.14.0
license: mit
emoji: 🐉
colorFrom: blue
colorTo: green
short_description: High-performance Python C Extension generator from Python.
pinned: true
thumbnail: >-
  https://cdn-uploads.huggingface.co/production/uploads/67caf50af30e4fe450042ac4/7DVx6daYTcVfiqhfRHCI9.png
---

## Python C Extension code generator

A Gradio app that provides an interactive interface for users to input Python code and
generate C extension code

Optionally, a compile and eval stage can be activated for local deployments to compare
its performance against the original Python code.

> [!CAUTION]
>
> **Always review the generated codes before running them, as they will be executed in
> your local environment and may contain code that could be harmful or unwanted.**
>
> AI-generated code may contain errors or unsafe practices, so it's crucial to
> thoroughly review and test on a sandboxed environment any code before using it in a
> production environment.
>
> Never run code generated by AI models without understanding its implications and
> ensuring it adheres to your security and safety standards.

> [!IMPORTANT]
>
> **Disclaimer:** This App and Notebook are provided for educational purposes only.
> Use it at your own risk.

### Installation

* Install the required Python dependencies:

  ```bash
  pip install -r requirements.txt
  ```

* Or, if you prefer Pipenv:

  ```bash
  pipenv install
  ```

### Configuration

The app reads configuration from environment variables and from a `.env` file if present.

* `MODELS`: Colon-separated list of models to expose in the dropdown.

  * Examples:

    *bash:*

    ```bash
    export MODELS="gpt-5.1-codex-mini:gpt-5.4-mini"
    ```

    *powershell:*

    ```powershell
    $env:MODELS = "gpt-5.1-codex-mini:gpt-5.4-mini"
    ```

  If not set, the app defaults to `gpt-5.1-codex-mini` and `gpt-5.4-mini`.

* `COMPILE_STAGE`: Set to `true`, `1`, or `yes` to enable the compile and test stage.

  * Examples:

    *bash:*

    ```bash
    export COMPILE_STAGE=true
    ```

    *powershell:*

    ```powershell
    $env:COMPILE_STAGE = "true"
    ```

### Running locally

* Run the app locally with:

  ```bash
  python app.py
  ```

* For autoreload during development, use the Gradio CLI:

  ```bash
  gradio app.py
  ```

### Gradio app overview

In this image, you can see the Gradio app dashboard whose main sections are
described below.

![Gradio app dashboard](images/gradio_dashboard.jpg)\
*Image: Gradio app dashboard with default example `hello world` code loaded.*
*(compile output redacted for privacy)*

Sections:

* **Dropdown selectors and input fields**:
  * **Module name input**:

    A text input field where users can specify the name of the C extension module to be
    generated.

    That name will be used to create the C extension file `<module_name>.c` and
    the `setup.py` file required to compile the extension.

    That name will also be used to import the compiled module as usual in Python:

    ```python
    import <module_name>
    ```

    Or

    ```python
    from <module_name> import <function_name>
    ```

  * **Model selector**:

    A dropdown menu to select the model used for code generation.

    The available options are taken from the `MODELS` environment variable if set.
    Otherwise the app defaults to `gpt-5.1-codex-mini` and `gpt-5.4-mini`.

  * **Platform selector**:

    A dropdown menu to select the target platform for the generated C extension.

    This affects how the app frames the prompt for the model and ensures the
    generated code targets the selected platform (`Windows` or `Linux`).

  * **Examples selector**:

    A list of ready-made Python examples to load into the input field.

    Built-in examples include `Hello world`, `Sum array`, `Fibonacci`, `Leibniz pi`,
    and `Max subarray sum`.

* **Text input areas**:

  These areas are all editable, including those filled with generated code by the model.
  This allows users to modify and experiment with the code as needed.

  * **Python code**:
    A text area where users can input their Python code.

    > [!NOTE]
    >
    > We are creating an importable module not an executable program so the code to be
    > optimized must contain only declarations such as DEF or CLASS.

  * **C extension code**:

    A text area that displays the generated C extension code.

  * **Compilation code**:

    A text area that shows the generated `setup.py` code.
    This file is required to compile the C extension.

  * **Test compare code**:

    A text area that provides example code to run the compiled C extension.

* **Output areas**:

  These are non-editable areas that display the results of various operations.

  * **C Extension result**: *(Only with Compile Stage Enabled)*

    A text area that displays the output of the C extension code build.

    > [!CAUTION]
    > Beware that this area can contain a large amount of text including warnings during
    > the compilation process and sensible information about the local environment,
    > like: paths, Python version, etc may be included.
    >
    > Redact that information if you plan to share the output.

  * **Test result**: *(Only with Compile Stage Enabled)*

    A text area that displays the output of the test code run.

* **Buttons**:
  * **Generate extension code**:

    A button that triggers the generation of the C extension code from the provided
    Python code.

    It will call the model to generate the C code, the setup.py file and the test code,
    filling the corresponding text areas automatically.

  * **Compile extension**: *(Only with Compile Stage Enabled)*

    A button that compiles the generated C extension using the provided `setup.py` file.
    It will create the extension C file, `<module_name>.c`, and the `setup.py` file in
    the local folder, then it will run the compilation command and build the C extension,
    `<module_name>.<arch_info>.pyd`.\
    *`arch_info` represents architecture info the extension has been compiled for:*

    * `hello_world.cp313-win_amd64.pyd`: CPython 3.13 + Windows + Amd64 Architecture.

    > [!CAUTION]
    >
    > **Always review the `setup.py` code before running it, as it will be executed in
    > your local environment and may contain code that could be harmful or unwanted.**
    >
    > **Also review the generated C code, as it will be compiled and executed in your
    > local environment and may contain code that could be harmful or unwanted.**

    It will display the compilation output in the "C Extension result" area.

  * **Test code**: *(Only with Compile Stage Enabled)*

    A button that executes the test code to compare the performance of the original
    Python code and the generated C extension.

    > [!CAUTION]
    >
    > **Always review the test code before running it, as it will be executed in
    > your local environment and may contain code that could be harmful or unwanted.**

    Will save the test code provided in the "Test compare code" into the
    `usage_example.py` file and execute it, showing the output in the "Test result" area.

## Notebook

The `notebooks` folder contains an example notebook demonstrating the usage and workflow
of the Python C Extension Generator app.

It provides a step-by-step, interactive overview for users who prefer a notebook-based
approach.

## TO DO

* Add an Anthropic Claude based `optimizer`