赞
踩
这个demo模型来是基于魔搭平台搭建
可能需要安装modelscope==1.9.1左右版本
- import os
- os.system('pip install tiktoken')
- os.system('pip install transformers_stream_generator')
-
- import gradio as gr
- from modelscope.pipelines import pipeline
- from modelscope.utils.constant import Tasks
- from modelscope import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
- import torch
- # os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
-
- def clear_session():
- return '', None
-
-
- from modelscope import AutoModelForCausalLM, AutoTokenizer, snapshot_download
- from modelscope import GenerationConfig
-
- model_dir = snapshot_download('qwen/Qwen-14B-Chat', revision='v1.0.4')
-
- tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True, revision='v1.0.4')
-
- model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True,torch_dtype=torch.bfloat16, revision='v1.0.4').eval()
-
- model.generation_config = GenerationConfig.from_pretrained(model_dir, trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
- model.generation_config.top_p = 0.8
- model.generation_config.repetition_penalty = 1.1
-
-
- def generate_chat(input: str, history = None):
- if input is None:
- input = ''
- if history is None:
- history = []
- history = history[-5:]
- gen = model.chat_stream(tokenizer, input, history=history)
- for x in gen:
- history.append((input, x))
- yield None, history
- history.pop()
- history.append((input, x))
- return None, history
-
- block = gr.Blocks()
- with block as demo:
- gr.Markdown("""<p align="center"><img src="https://modelscope.cn/api/v1/models/qwen/Qwen-VL-Chat/repo?Revision=master&FilePath=assets/logo.jpg&View=true" style="height: 80px"/><p>""")
- gr.Markdown("""<center><font size=8>Qwen-14B-Chat Bot声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/696187推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。