Gradio機(jī)器學(xué)習(xí)模型快速部署工具接口狀態(tài)
1.全局狀態(tài)
例子來(lá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ù)中訪問(wèn)。
- 多用戶訪問(wèn),每次訪問(wèn)的分?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)的示例 - 您想要訪問(wèn)用戶以前提交的內(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)文章
Biblibili視頻投稿接口分析并以Python實(shí)現(xiàn)自動(dòng)投稿功能
這篇文章主要介紹了Biblibili視頻投稿接口分析并以Python實(shí)現(xiàn)自動(dòng)投稿功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02tensorflow 實(shí)現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換
今天小編就為大家分享一篇tensorflow 實(shí)現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Python的信號(hào)庫(kù)Blinker用法詳解
在本篇文章里小編給大家整理了一篇關(guān)于Python的信號(hào)庫(kù)Blinker用法詳解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12python logging日志模塊以及多進(jìn)程日志詳解
本篇文章主要介紹了python logging日志模塊以及多進(jìn)程日志詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Python字符串函數(shù)strip()原理及用法詳解
這篇文章主要介紹了Python字符串函數(shù)strip()原理及用法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07