当前位置:   article > 正文

通义千问qwen14b app端demo_千问模型demo

千问模型demo

 这个demo模型来是基于魔搭平台搭建

可能需要安装modelscope==1.9.1左右版本

  1. import os
  2. os.system('pip install tiktoken')
  3. os.system('pip install transformers_stream_generator')
  4. import gradio as gr
  5. from modelscope.pipelines import pipeline
  6. from modelscope.utils.constant import Tasks
  7. from modelscope import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
  8. import torch
  9. # os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
  10. def clear_session():
  11. return '', None
  12. from modelscope import AutoModelForCausalLM, AutoTokenizer, snapshot_download
  13. from modelscope import GenerationConfig
  14. model_dir = snapshot_download('qwen/Qwen-14B-Chat', revision='v1.0.4')
  15. tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True, revision='v1.0.4')
  16. model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True,torch_dtype=torch.bfloat16, revision='v1.0.4').eval()
  17. model.generation_config = GenerationConfig.from_pretrained(model_dir, trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参
  18. model.generation_config.top_p = 0.8
  19. model.generation_config.repetition_penalty = 1.1
  20. def generate_chat(input: str, history = None):
  21. if input is None:
  22. input = ''
  23. if history is None:
  24. history = []
  25. history = history[-5:]
  26. gen = model.chat_stream(tokenizer, input, history=history)
  27. for x in gen:
  28. history.append((input, x))
  29. yield None, history
  30. history.pop()
  31. history.append((input, x))
  32. return None, history
  33. block = gr.Blocks()
  34. with block as demo:
  35. 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>""")
  36. gr.Markdown("""<center><font size=8>Qwen-14B-Chat Bot
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/696187
    推荐阅读