gradio

Форк
0
/
script_runner.py 
149 строк · 6.1 Кб
1
import ast
2
import os
3
import sys
4
import tokenize
5
import types
6
from inspect import CO_COROUTINE
7

8
from gradio.wasm_utils import app_id_context
9

10
# BSD 3-Clause License
11
#
12
# - Copyright (c) 2008-Present, IPython Development Team
13
# - Copyright (c) 2001-2007, Fernando Perez <fernando.perez@colorado.edu>
14
# - Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>
15
# - Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>
16
#
17
# All rights reserved.
18
#
19
# Redistribution and use in source and binary forms, with or without
20
# modification, are permitted provided that the following conditions are met:
21
#
22
# * Redistributions of source code must retain the above copyright notice, this
23
#   list of conditions and the following disclaimer.
24

25
# * Redistributions in binary form must reproduce the above copyright notice,
26
#   this list of conditions and the following disclaimer in the documentation
27
#   and/or other materials provided with the distribution.
28

29
# * Neither the name of the copyright holder nor the names of its
30
#   contributors may be used to endorse or promote products derived from
31
#   this software without specific prior written permission.
32

33
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
37
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43

44
# Code modified from IPython (BSD license)
45
# Source: https://github.com/ipython/ipython/blob/master/IPython/utils/syspathcontext.py#L42
46
class modified_sys_path:  # noqa: N801
47
    """A context for prepending a directory to sys.path for a second."""
48

49
    def __init__(self, script_path: str):
50
        self._script_path = script_path
51
        self._added_path = False
52

53
    def __enter__(self):
54
        if self._script_path not in sys.path:
55
            sys.path.insert(0, self._script_path)
56
            self._added_path = True
57

58
    def __exit__(self, type, value, traceback):
59
        if self._added_path:
60
            try:
61
                sys.path.remove(self._script_path)
62
            except ValueError:
63
                # It's already removed.
64
                pass
65

66
        # Returning False causes any exceptions to be re-raised.
67
        return False
68

69

70
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022)
71
# Copyright (c) Yuichiro Tachibana (2023)
72
#
73
# Licensed under the Apache License, Version 2.0 (the "License");
74
# you may not use this file except in compliance with the License.
75
# You may obtain a copy of the License at
76
#
77
#     http://www.apache.org/licenses/LICENSE-2.0
78
#
79
# Unless required by applicable law or agreed to in writing, software
80
# distributed under the License is distributed on an "AS IS" BASIS,
81
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
82
# See the License for the specific language governing permissions and
83
# limitations under the License.
84
def _new_module(name: str) -> types.ModuleType:
85
    """Create a new module with the given name."""
86
    return types.ModuleType(name)
87

88

89
def set_home_dir(home_dir: str) -> None:
90
    os.environ["HOME"] = home_dir
91
    os.chdir(home_dir)
92

93

94
async def _run_script(app_id: str, home_dir: str, script_path: str) -> None:
95
    # This function is based on the following code from Streamlit:
96
    # https://github.com/streamlit/streamlit/blob/1.24.0/lib/streamlit/runtime/scriptrunner/script_runner.py#L519-L554
97
    # with modifications to support top-level await.
98
    set_home_dir(home_dir)
99

100
    with tokenize.open(script_path) as f:
101
        filebody = f.read()
102

103
    await _run_code(app_id, home_dir, filebody, script_path)
104

105

106
async def _run_code(
107
        app_id: str,
108
        home_dir: str,
109
        filebody: str,
110
        script_path: str = '<string>'  # This default value follows the convention. Ref: https://docs.python.org/3/library/functions.html#compile
111
    ) -> None:
112
    set_home_dir(home_dir)
113

114
    # NOTE: In Streamlit, the bytecode caching mechanism has been introduced.
115
    # However, we skipped it here for simplicity and because Gradio doesn't need to rerun the script so frequently,
116
    # while we may do it in the future.
117
    bytecode = compile(  # type: ignore
118
        filebody,
119
        # Pass in the file path so it can show up in exceptions.
120
        script_path,
121
        # We're compiling entire blocks of Python, so we need "exec"
122
        # mode (as opposed to "eval" or "single").
123
        mode="exec",
124
        # Don't inherit any flags or "future" statements.
125
        flags=ast.PyCF_ALLOW_TOP_LEVEL_AWAIT, # Allow top-level await. Ref: https://github.com/whitphx/streamlit/commit/277dc580efb315a3e9296c9a0078c602a0904384
126
        dont_inherit=1,
127
        # Use the default optimization options.
128
        optimize=-1,
129
    )
130

131
    module = _new_module("__main__")
132

133
    # Install the fake module as the __main__ module. This allows
134
    # the pickle module to work inside the user's code, since it now
135
    # can know the module where the pickled objects stem from.
136
    # IMPORTANT: This means we can't use "if __name__ == '__main__'" in
137
    # our code, as it will point to the wrong module!!!
138
    sys.modules["__main__"] = module
139

140
    # Add special variables to the module's globals dict.
141
    module.__dict__["__file__"] = script_path
142

143
    with modified_sys_path(script_path), modified_sys_path(home_dir), app_id_context(app_id):
144
        # Allow top-level await. Ref: https://github.com/whitphx/streamlit/commit/277dc580efb315a3e9296c9a0078c602a0904384
145
        if bytecode.co_flags & CO_COROUTINE:
146
            # The source code includes top-level awaits, so the compiled code object is a coroutine.
147
            await eval(bytecode, module.__dict__)
148
        else:
149
            exec(bytecode, module.__dict__)
150

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.