Gradio機(jī)器學(xué)習(xí)模型快速部署工具接口狀態(tài)
1.全局狀態(tài)
例子來解釋
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,就可以在某函數(shù)中訪問。
- 多用戶訪問,每次訪問的分?jǐn)?shù)都保存到scores列表
- 并并返回前三的分?jǐn)?shù)
2.會(huì)話狀態(tài)
Gradio 支持的另一種數(shù)據(jù)持久化類型是會(huì)話狀態(tài),其中數(shù)據(jù)在頁(yè)面會(huì)話中跨多個(gè)提交持久化。但是,數(shù)據(jù)_不會(huì)_在模型的不同用戶之間共享。要在會(huì)話狀態(tài)中存儲(chǔ)數(shù)據(jù),您需要做三件事:
- 將一個(gè)額外的參數(shù)傳遞到您的函數(shù)中,該參數(shù)表示界面的狀態(tài)。
- 在函數(shù)結(jié)束時(shí),返回狀態(tài)的更新值作為額外的返回值。
- 創(chuàng)建時(shí)添加
'state'輸入和輸出組件'state'``Interface
聊天機(jī)器人是一個(gè)您需要會(huì)話狀態(tài)的示例 - 您想要訪問用戶以前提交的內(nèi)容,但您不能將聊天歷史存儲(chǔ)在全局變量中,因?yàn)槟菢恿奶鞖v史會(huì)在不同用戶之間混亂。
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()

以上就是Gradio機(jī)器學(xué)習(xí)模型快速部署工具接口狀態(tài)的詳細(xì)內(nèi)容,更多關(guān)于Gradio部署接口狀態(tài)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Django 如何從request中獲取前端數(shù)據(jù)
這篇文章主要介紹了Django從request中獲取前端數(shù)據(jù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
python隨機(jī)種子ranrandom seed的使用
本文介紹了在Python中設(shè)置隨機(jī)種子random seed的方法,可以使用seed()函數(shù)設(shè)置隨機(jī)種子,確保你的隨機(jī)數(shù)生成過程是可重復(fù)的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
conda與jupyter notebook kernel核環(huán)境不一致的問題解決
本文記錄在使用conda時(shí)候出現(xiàn)的問題,jupter notebook中的環(huán)境不一致導(dǎo)致的,具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05
python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法
下面小編就為大家分享一篇python生成不重復(fù)隨機(jī)數(shù)和對(duì)list亂序的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
VSCode搭建Django開發(fā)環(huán)境的圖文步驟
本篇介紹在vscode環(huán)境下搭建Django開發(fā)環(huán)境的詳細(xì)步驟,包括Python、Django、VSCode等,以及它們的安裝和配置方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Python?PyCharm無(wú)法打開終端命令行最終解決方案(實(shí)測(cè)成功)
這篇文章主要介紹了在使用PyCharm?2024版本時(shí)遇到的無(wú)法打開終端的問題,文中提供了兩種解決方案,大家可以根據(jù)自己的需求選擇對(duì)應(yīng)的解決方法,需要的朋友可以參考下2024-12-12
Django 項(xiàng)目通過加載不同env文件來區(qū)分不同環(huán)境
這篇文章主要介紹了Django 項(xiàng)目如何通過加載不同env文件來區(qū)分不同環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python2和Python3中urllib庫(kù)中urlencode的使用注意事項(xiàng)
這篇文章主要介紹了Python2和Python3中urllib庫(kù)中urlencode的使用注意事項(xiàng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11

