Gradio机器学习模型快速部署工具接口状态
作者:Livingbody 发布时间:2023-08-11 13:36:27
标签:Gradio,机器学习,部署,接口状态
原文: gradio.app/interface-s…
1.全局状态
例子来解释
import gradio as gr
scores = []
def track_score(score):
scores.append(score)
top_scores = sorted(scores, reverse=True)[:3]
return top_scores
demo = gr.Interface(
track_score,
gr.Number(label="Score"),
gr.JSON(label="Top Scores")
)
demo.launch()
如上所述,scores,就可以在某函数中访问。
多用户访问,每次访问的分数都保存到scores列表
并并返回前三的分数
2.会话状态
Gradio 支持的另一种数据持久化类型是会话状态,其中数据在页面会话中跨多个提交持久化。但是,数据_不会_在模型的不同用户之间共享。要在会话状态中存储数据,您需要做三件事:
将一个额外的参数传递到您的函数中,该参数表示界面的状态。
在函数结束时,返回状态的更新值作为额外的返回值。
创建时添加
'state'
输入和输出组件'state'``Interface
聊天机器人是一个您需要会话状态的示例 - 您想要访问用户以前提交的内容,但您不能将聊天历史存储在全局变量中,因为那样聊天历史会在不同用户之间混乱。
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
def user(message, history):
return "", history + [[message, None]]
# bot_message = random.choice(["Yes", "No"])
# history[-1][1] = bot_message
# time.sleep(1)
# return history
# def predict(input, history=[]):
# # tokenize the new input sentence
def bot(history):
user_message = history[-1][0]
new_user_input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt')
# append the new user input tokens to the chat history
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
# generate a response
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
# convert the tokens to text, and then split the responses into lines
response = tokenizer.decode(history[0]).split("<|endoftext|>")
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
return history
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
clear = gr.Button("Clear")
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
bot, chatbot, chatbot
)
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch()
来源:https://juejin.cn/post/7217360688263512121


猜你喜欢
- 今日一同时问我,new Date(Date(str))这段代码什么意思?我一看就晕了,一个new Date 一个Date这是什么意思?这函数
- 简介CountMinSketch是一种计数器,用来统计一个元素的计数,它能够以一个非常小的空间统计大量元素的计数,同时保证高的性能及准确性。
- 在使用python函数print()时,如下代码会出现输出无法显示的问题:分三次在一行输出 123print(1, end="&q
- 模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中。此类中的大部分函数都与对文件的操作方法类似。例:#coding=gb
- 什么是Dynamic HTML 今天我们以问答的形式来讲述什麽是Dynamic Html。问:亲爱的网猴,我经常看到讲述有关“Dynamic
- 实验环境:windows 7,anaconda 3(python 3.5),tensorflow(gpu/cpu)函数介绍:所用函数为six
- 这个是用vue-cli生成的项目下使用比如有个路由跳转时需要带两个参数:<router-link to='/tr'&g
- 在目前的工作中需要解决复制整个SqlServer数据库的问题,复制的内容包括数据库大纲、数据库中的存储过程、函数、表结构、主外键关系以及表中
- Python 数据库编程,ODBC方式实现通讯录,供大家参考,具体内容如下#-*-coding:utf-8-*-import pyodbci
- You have an error in your SQL syntax; check the manual that correspond
- -- begin auth.inc -- <?php $
- Xml_javascript分页实例:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.
- 前言Python多进程适用的场景:计算密集型(CPU密集型)任务Python多线程适用的场景:IO密集型任务计算密集型任务一般指需要做大量的
- 我就废话不多说了,大家还是直接看代码吧!一、举例tip/tip.js var react = function (
- 如下所示:# -*- coding: utf-8 -*-import numpy as npfrom PyQt5.QtCore import
- pycharm出现no module named xlwt问题首先声明,我是初学者,今天按照书上步骤,创建Excel文件,当我的xlwt安装
- python写的简单的学生管理系统,练习python语法。可以运行在windows和linux下,python 2.7。#!/usr/loc
- 引言最近再做图像处理相关的操作的时间优化,用到了OpenCV和Pillow两个库,两个库各有优缺点。各位小伙伴需要按照自己需求选用。本篇博客
- Anaconda简介Anaconda,是一个开源的Python发行版本,其包含了conda、Python以及一大堆安装好的工具包及依赖项,比
- 一、粗心导致的语法错误SyntaxError1、input输入报错age=input('请输入你的年龄:')if age&g