使用Jina部署本地服务及问题解决

安装Jina

pip install -U jina

服务端python

server.py:

from jina import Flow, Document, Executor, requests,DocumentArray

class MyExecutor(Executor):

    @requests(on='/bar')

    def foo(self, docs:DocumentArray, **kwargs):
        #ret = DocumentArray()
        print("修改前:",docs.texts)
        #docs = DocumentArray([Document(text='The cake is a lie'), Document(text='Do a barrel roll!')])
        docs.append(Document(text='aha, the cake is a lie'))
        print("修改后:",docs.texts)
        return docs


f = Flow(port_expose=12345).add(name='myexec1', uses=MyExecutor)

if __name__ == '__main__':
    with f:
        # backend server forever
        f.block()

注意:必须加上if __name__ == '__main__':,否则会报RuntimeError错误:

RuntimeError:
An attempt has been made to start a new process before thecurrent process has finished its bootstrapping phase.
This probably means that you are not using fork to start yourchild processes and you have forgotten to use the proper idiomin the main module :
ifname_"Imainfreeze_support()
The "freeze_support()" line can be omitted if the programis not going to be frozen to produce an executable.

客户端

client.py:

from jina import Client, Document,DocumentArray

c = Client(port=12345)

docs = DocumentArray([Document(text='Here is a delicious cake.')])

resp = c.post(on='/bar', inputs=docs)

print("resp:",resp)
print("resp.texts:",resp.texts)

运行效果:
server端:

D:\textCompare>python serverComp.py
─────────────────────────────── 🎉 Flow is ready to serve! ────────────────────────────────
╭────────────── 🔗 Endpoint ───────────────╮
│  ⛓      Protocol                   GRPC  │
│  🏠        Local        127.0.0.1:12345  │
│  🔒      Private     172.17.29.48:12345  │
╰──────────────────────────────────────────╯
修改前: ['Here is a delicious cake.']
修改后: ['Here is a delicious cake.', 'aha, the cake is a lie']

client端:

D:\textCompare>python clientComp.py
resp: <DocumentArray (length=2) at 2071732501376>
resp.texts: ['Here is a delicious cake.', 'aha, the cake is a lie']

yaml文件加载服务配置

my.yaml:

jtype: Flow
executors:
  - name: myexec1
    uses: MyExecutor

客户端

client.py:

from jina import Flow, Document
f = Flow.load_config('my.yml')
with f:
    f.post(on='/bar', inputs=Document(text='this is a test'))

注意:客户端和服务端交换的数据,必须包装成DocumentArray类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Jina是一个开源的流数据引擎,可以帮助您对大规模文本数据进行无监督的语料标注。以下是使用Jina进行无监督语料标注的简要步骤: 1. 准备您的数据集 首先,您需要准备一个包含大量文本数据的数据集。这个数据集可以是您自己的数据集,或者是公开可用的数据集,例如维基百科。 2. 安装Jina 安装Jina非常简单,您可以使用pip命令进行安装: ``` pip install jina ``` 3. 创建一个Flow 在Jina中,Flow是一个用于构建分布式流水线的对象。要创建一个Flow,您需要定义一些Executor,并将它们组合在一起。 在这里,我们将使用Jina内置的TextEncoder和ChunkToDoc Executor。TextEncoder用于将文本数据编码为向量表示,而ChunkToDoc将编码后的数据组合成文档。 以下是一个简单的Flow示例: ```python from jina.flow import Flow from jina.executors.encoders.nlp import TextEncoder from jina.executors.metas import get_default_metas metas = get_default_metas() metas['workspace'] = 'my-workspace' encoder = TextEncoder(metas=metas) chunk_to_doc = ChunkToDoc(metas=metas) flow = Flow().add(encoder).add(chunk_to_doc) ``` 4. 运行Flow 现在,我们已经创建了一个Flow,接下来需要将数据传递给Flow并运行它。在这里,我们将使用一个简单的Python列表来模拟我们的数据集。 ```python data = ['This is the first document.', 'This is the second document.'] with flow: flow.index(data, batch_size=2) ``` 在运行此代码后,Jina将处理我们的数据集,并将其编码为向量表示。这些向量表示将被存储在Jina内置的索引中,以便以后进行检索。 5. 检索数据 一旦我们的数据被编码和索引,我们就可以使用Jina进行检索。以下是一个简单的示例: ```python query = 'This is a query.' with flow: response = flow.search(query, return_results=True) for result in response[0].data.docs: print(result.text) ``` 在这里,我们将查询传递给Flow,Jina将返回与查询最相关的文档。在此示例中,我们只打印了检索到的文档的文本内容,但您可以根据需要对其进行其他操作。 这就是使用Jina进行无监督语料标注的基本步骤。当然,这只是一个简单的示例,您可以根据自己的需求对Flow进行更复杂的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值