Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web demo share #7

Closed
baiyutang opened this issue Jul 5, 2023 · 0 comments
Closed

web demo share #7

baiyutang opened this issue Jul 5, 2023 · 0 comments

Comments

@baiyutang
Copy link

baiyutang commented Jul 5, 2023

import gradio as gr
from PIL import Image

from VisCPM import VisCPMChat

# 修改你的模型地址
model_path = '/path/to/checkpoint'
viscpm_chat = VisCPMChat(model_path, image_safety_checker=False)
print("load  model  success !")


def upload_img(image,_chatbot,_app_session):
    image = Image.fromarray(image)
    _app_session['sts']=None
    _app_session['ctx']=''
    _app_session['img']=image
    _chatbot.append(('图片解析成功,可以和我对话了', ''))
    return _chatbot,_app_session


def respond( _question, _chat_bot,_app_cfg):
    _answer, _context, sts = viscpm_chat.chat(_app_cfg['img'], _question, _app_cfg['ctx'],
                                            vision_hidden_states=_app_cfg['sts'])
    _chat_bot.append((_question, _answer))
    _app_cfg['ctx']=_context
    _app_cfg['sts']=sts
    print('context', _context)
    return '',_chat_bot,_app_cfg


with gr.Blocks() as demo:
    app_session = gr.State({'sts':None,'ctx':None,'img':None})
    bt_pic = gr.Image(label="先上传一张图片")
    chat_bot = gr.Chatbot(label="聊天对话")
    txt_message = gr.Textbox(label="输入文字")

    txt_message.submit(respond, [ txt_message, chat_bot,app_session], [txt_message,chat_bot,app_session])
    bt_pic.upload(lambda: None, None, chat_bot, queue=False).then(upload_img, inputs=[bt_pic,chat_bot,app_session], outputs=[chat_bot,app_session])


demo.queue(concurrency_count=1, max_size=20).launch(share=False, debug=True, server_port=7866,
                                                    server_name="0.0.0.0")

@JamesHujy JamesHujy pinned this issue Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants