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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
重要说明:
|
| 3 |
+
1. 需要确认import的包是否是requirements.txt中的版本,否则会出现不兼容的情况。
|
| 4 |
+
2. 在LAN中如何访问Gradio app:
|
| 5 |
+
1. 在Gradio中制定demo.launch(height='1500px', server_name="0.0.0.0", server_port=7860, auth=auth_list, auth_message="欢迎使用中交建财务共享中心企业大语言模型平台", share=False).queue()
|
| 6 |
+
2. 在另外一台电脑上,通过http://http://192.168.1.104:7860/ (其中http://192.168.1.104是Gradio的IP地址+端口来访问。
|
| 7 |
+
|
| 8 |
+
产品说明:
|
| 9 |
+
1. 采用大模型的核心功能 + 专属定制化功能的方式,来实现一个专属的大语言模型解决方案。
|
| 10 |
+
2. 在专属定制化功能中包含了报告模块、商业分析模块、培训资料模块。配置了专属的prompt,以及对应的模型参数。【话术:定制化的大模型接口 + 结构化的提示词工程 + 业务流】
|
| 11 |
+
3. 此版本可以支持多轮对话【注:目前强行限制在3轮以内】,但是会收到大模型基座的影响,目前是ChatGLM3-6B中的4096个token的限制。
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
Theme默认设置为Dark:
|
| 15 |
+
1. css = """
|
| 16 |
+
#mybutton {background-color: #CEFAFE; color: #06B6D4;}
|
| 17 |
+
#textarea {-webkit-text-fill-color:black; -webkit-opacity: 1;}
|
| 18 |
+
.message {font: 16px Arial, sans-serif, 'ui-sans-serif', Montserrat, 'system-ui';}
|
| 19 |
+
"""
|
| 20 |
+
2. with gr.Blocks(theme=gr.themes.Base(primary_hue='sky', text_size='md'), css=css, title="大语言模型专项模块", js=js_func) as demo:
|
| 21 |
+
|
| 22 |
+
'''
|
| 23 |
+
|
| 24 |
+
##TODO 1. 将LLM改成公网Qwen API版本。
|
| 25 |
+
|
| 26 |
+
import gradio as gr
|
| 27 |
+
import requests
|
| 28 |
+
import os
|
| 29 |
+
from rich import print
|
| 30 |
+
import os
|
| 31 |
+
import sys
|
| 32 |
+
import time
|
| 33 |
+
import pandas as pd
|
| 34 |
+
import numpy as np
|
| 35 |
+
import sys
|
| 36 |
+
import time
|
| 37 |
+
import PyPDF2
|
| 38 |
+
from PyPDF2 import PdfReader
|
| 39 |
+
import docx ## 需要安装python-docx,否则会报错。
|
| 40 |
+
from typing import Any
|
| 41 |
+
import requests
|
| 42 |
+
import csv
|
| 43 |
+
import os
|
| 44 |
+
from rich import print
|
| 45 |
+
import pandas
|
| 46 |
+
import io
|
| 47 |
+
from io import StringIO
|
| 48 |
+
from transformers import AutoModel, AutoTokenizer
|
| 49 |
+
import mdtex2html
|
| 50 |
+
import qwen_response
|
| 51 |
+
import dashscope
|
| 52 |
+
from dotenv import load_dotenv
|
| 53 |
+
from http import HTTPStatus
|
| 54 |
+
from dashscope import Generation
|
| 55 |
+
|
| 56 |
+
load_dotenv()
|
| 57 |
+
### 设置openai的API key
|
| 58 |
+
dashscope.api_key = os.environ['dashscope_api_key']
|
| 59 |
+
|
| 60 |
+
### Gradio设置为默认Dark
|
| 61 |
+
js_func = """
|
| 62 |
+
function refresh() {
|
| 63 |
+
const url = new URL(window.location);
|
| 64 |
+
|
| 65 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
| 66 |
+
url.searchParams.set('__theme', 'dark');
|
| 67 |
+
window.location.href = url.href;
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
# ''' Start: Environment settings. '''
|
| 74 |
+
# os.environ['SENTENCE_TRANSFORMERS_HOME'] = '/Users/yunshi/Downloads/chatGLM/My_LocalKB_Project/'
|
| 75 |
+
# os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
|
| 76 |
+
# # import PyPDF4
|
| 77 |
+
# # from PyPDF4 import PdfFileReader
|
| 78 |
+
# import torch
|
| 79 |
+
# mps_device = torch.device("mps") ## 在mac机器上需要加上这句。
|
| 80 |
+
|
| 81 |
+
### 在langchain中定义chatGLM作为LLM。
|
| 82 |
+
from typing import Any, List, Mapping, Optional
|
| 83 |
+
from langchain.callbacks.manager import CallbackManagerForLLMRun
|
| 84 |
+
from langchain.llms.base import LLM
|
| 85 |
+
from transformers import AutoTokenizer, AutoModel
|
| 86 |
+
# llm_filepath = str("/Users/yunshi/Downloads/chatGLM/ChatGLM3-6B/6B") ## 第三代chatGLM 6B W/ code-interpreter
|
| 87 |
+
|
| 88 |
+
## 在ChatGLM3-6B中成功部署。
|
| 89 |
+
# class chatGLM():
|
| 90 |
+
# def __init__(self, model_name) -> None:
|
| 91 |
+
# self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 92 |
+
# # self.model = AutoModel.from_pretrained(model_name, trust_remote_code=True).half().cuda().eval() ##PC CUDA机器上用。
|
| 93 |
+
# self.model = AutoModel.from_pretrained(model_name, trust_remote_code=True).half().to('mps') ##Mac机器上用。
|
| 94 |
+
|
| 95 |
+
# def __call__(self, prompt) -> Any:
|
| 96 |
+
# # response, _ = self.model.chat(self.tokenizer , prompt) # 这里演示未使用流式接口. stream_chat()
|
| 97 |
+
# response, _ = self.model.stream_chat(self.tokenizer, prompt) # 尝试用stream_chat()
|
| 98 |
+
# return response
|
| 99 |
+
|
| 100 |
+
# chatglm = chatGLM(model_name=llm_filepath)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
''' End: Environment settings. '''
|
| 105 |
+
|
| 106 |
+
# ''' Start: 以下加载本地知识的核心内容。'''
|
| 107 |
+
# from langchain.document_loaders import UnstructuredFileLoader
|
| 108 |
+
# from langchain.text_splitter import CharacterTextSplitter
|
| 109 |
+
# # from langchain.embeddings.openai import OpenAIEmbeddings
|
| 110 |
+
# from langchain.embeddings.huggingface import HuggingFaceEmbeddings
|
| 111 |
+
# from langchain.vectorstores import FAISS
|
| 112 |
+
|
| 113 |
+
# ## 加载文件
|
| 114 |
+
# filepath = "/Users/yunshi/Downloads/txt_dir/Sparks_of_AGI.pdf"
|
| 115 |
+
# # filepath = "/Users/yunshi/Downloads/txt_dir/浙江省院前急救质控统计指标.pdf"
|
| 116 |
+
# loader = UnstructuredFileLoader(filepath)
|
| 117 |
+
# docs = loader.load()
|
| 118 |
+
|
| 119 |
+
# ## 文本分割
|
| 120 |
+
# # text_splitter = CharacterTextSplitter(chunk_size=5000, chunk_overlap=200)
|
| 121 |
+
# docs = CharacterTextSplitter(chunk_size=5000, chunk_overlap=200).split_documents(docs)
|
| 122 |
+
|
| 123 |
+
# ## 创建向量数据库
|
| 124 |
+
# # embeddings = OpenAIEmbeddings(disallowed_special=())
|
| 125 |
+
|
| 126 |
+
# embedding_model_name = 'GanymedeNil/text2vec-large-chinese'
|
| 127 |
+
# # embeddings = HuggingFaceEmbeddings(model_name=embedding_model_name) ## 这里是联网情况下连接huggingface后使用。
|
| 128 |
+
# embeddings = HuggingFaceEmbeddings(model_name='/Users/yunshi/Downloads/chatGLM/My_LocalKB_Project/GanymedeNil_text2vec-large-chinese/') ## 这里会有个“No sentence-transformers model found with name“的warning,但不是error,不影响使用。
|
| 129 |
+
|
| 130 |
+
# ### 比较的中文embeddding之一。
|
| 131 |
+
# # from text2vec import SentenceModel
|
| 132 |
+
# # embeddings = SentenceModel('shibing624/text2vec-base-chinese-sentence', device=mps_device)
|
| 133 |
+
# # embeddings = HuggingFaceEmbeddings()
|
| 134 |
+
# vector_store = FAISS.from_documents(docs, embeddings)
|
| 135 |
+
|
| 136 |
+
# '''End: 至此本地加载知识库过程完成。'''
|
| 137 |
+
|
| 138 |
+
# ### 读取教材资料,docx文档
|
| 139 |
+
# doc = docx.Document("./sample.docx")
|
| 140 |
+
# mytext = ""
|
| 141 |
+
# for paragraph in doc.paragraphs:
|
| 142 |
+
# mytext += paragraph.text
|
| 143 |
+
|
| 144 |
+
# ### 在工作台展示一部分内容
|
| 145 |
+
# showtext = ""
|
| 146 |
+
# for paragraph in doc.paragraphs[:12]:
|
| 147 |
+
# showtext += paragraph.text
|
| 148 |
+
# print(showtext)
|
| 149 |
+
|
| 150 |
+
## 一键生成培训资料的环境设定
|
| 151 |
+
# training_content = mytext.replace("['", "").replace("']", "")
|
| 152 |
+
# generate_message = gr.Textbox(f"""你是一个专业的培训材料制作者。我需要你根据提供的内容生成培训材料,内容如下:{training_content}""", visible=False) #! 注意这里的type,不能是简单的str。
|
| 153 |
+
# system_prompt_message = """你是一个专业的培训材料制作者。我需要你根据提供的内容生成培训材料,内容如下:"""
|
| 154 |
+
# system_prompt = [{"role": "system", "content": f"""{system_prompt_message} {training_content}"""}]
|
| 155 |
+
# print('system_prompt:', system_prompt)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# prompt_templates = {"Default ChatGPT": ""}
|
| 159 |
+
|
| 160 |
+
### set an empty state in Gradio.
|
| 161 |
+
def get_empty_state():
|
| 162 |
+
return {"total_tokens": 0, "messages": []}
|
| 163 |
+
|
| 164 |
+
## 重新生成对话,regenerate功能的核心,主要是将最后一次chatbot的回答清空。
|
| 165 |
+
# def reload(chatbot):
|
| 166 |
+
def regenerate(chatbot):
|
| 167 |
+
chatbot[-1][1] = "" #! 清空最后一次的回答。
|
| 168 |
+
return chatbot
|
| 169 |
+
|
| 170 |
+
## system prompt for LLM
|
| 171 |
+
system_prompt = [{"role": "system", "content": '你是一个专业和友好的AI助手。除非被要求,否则你默认的回答语言是中文。'}]
|
| 172 |
+
|
| 173 |
+
### 手动添加prompt templates。
|
| 174 |
+
# prompt_templates = {
|
| 175 |
+
# '默认角色': "你是一个专业的人工智能助手。",
|
| 176 |
+
# '周报写作': "使用下面提供的文本作为中文周报的基础,生成一个简洁的摘要,突出最重要的内容。该报告应以 markdown 格式编写,并应易于阅读和理解,以满足一般受众的需要。特别是要注重提供对利益相关者和决策者有用的见解和分析。你也可以根据需要使用任何额外的信息或来源。",
|
| 177 |
+
# '写作建议': "我希望你能充当一名人工智能写作导师。我将为你提供一个需要帮助提高写作水平的学生,你的任务是使用人工智能工具,如自然语言处理,给学生反馈如何提高他们的写作水平。你还应该利用你的修辞学知识和关于有效写作技巧的经验,以建议该学生如何以书面形式更好地表达他们的思想和观点。我的第一个要求是 [修改文本]",
|
| 178 |
+
# }
|
| 179 |
+
|
| 180 |
+
### 导入收集到的有用prompts。
|
| 181 |
+
raw_prompts = pd.read_excel("./raw_prompts.xlsx", usecols=['category','prompt'], index_col='category')
|
| 182 |
+
prompt_templates = raw_prompts.to_dict()['prompt']
|
| 183 |
+
|
| 184 |
+
def on_prompt_template_change(prompt_template):
|
| 185 |
+
if not isinstance(prompt_template, str): return
|
| 186 |
+
# print(prompt_template)
|
| 187 |
+
return prompt_templates[prompt_template]
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
chat_hist = []
|
| 191 |
+
def submit_message(radio, chatbot_history, temperature, max_tokens,top_p,presence_penalty): ## working.
|
| 192 |
+
# input_prompt = chatbot_history ## 单轮对话 working。
|
| 193 |
+
input_prompt = chatbot_history ## working。支持多轮对话。
|
| 194 |
+
print("input_prompt", input_prompt)
|
| 195 |
+
|
| 196 |
+
###NOTE: 保留3次历史记录,原生ChatGPT的上下文也只能到这里了。
|
| 197 |
+
### 只把三次历史记录放入prompt中,这样可以保留三轮对话的历史。
|
| 198 |
+
prompt = []
|
| 199 |
+
if len(input_prompt) > 3:
|
| 200 |
+
for x in input_prompt[-3:]:
|
| 201 |
+
prompt.append(x)
|
| 202 |
+
else:
|
| 203 |
+
prompt = input_prompt
|
| 204 |
+
|
| 205 |
+
prompt = [item for sublist in prompt for item in sublist if item is not None]
|
| 206 |
+
prompt = '。'.join(prompt)
|
| 207 |
+
|
| 208 |
+
print('prompt now is:', prompt)
|
| 209 |
+
print('start the default version of Qwen')
|
| 210 |
+
|
| 211 |
+
## Role的范式。
|
| 212 |
+
# prompt_msg = {"role": "user", "content": prompt}
|
| 213 |
+
# system_prompt = [{"role": "system", "content": '你是一个专业和友好的AI助手。'}]
|
| 214 |
+
|
| 215 |
+
try:
|
| 216 |
+
## no stream version.
|
| 217 |
+
# completion_1 = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=system_prompt + [prompt_msg], temperature=0.7, max_tokens=1024)
|
| 218 |
+
# history.append(prompt_msg)
|
| 219 |
+
# history.append(completion_1.choices[0].message.to_dict())
|
| 220 |
+
# print('completion_1:',completion_1.choices[0].message.content)
|
| 221 |
+
# # state['total_tokens'] += completion_1['usage']['total_tokens']
|
| 222 |
+
|
| 223 |
+
# messages = system_prompt + [prompt_msg]
|
| 224 |
+
# input_prompt[-1][1] = ""
|
| 225 |
+
# openai_client = OpenAI()
|
| 226 |
+
# for resp in openai_client.chat.completions.create(model=model_choice, messages=messages, stream=True, temperature=temperature, max_tokens=max_tokens,top_p=top_p,presence_penalty=presence_penalty):
|
| 227 |
+
# answer = str(resp.choices[0].delta.content)
|
| 228 |
+
# if answer != "None":
|
| 229 |
+
# ##NOTE: 这里是单论聊天的版本。
|
| 230 |
+
# # resp_history.append(answer) #* working!
|
| 231 |
+
# # result = "".join(resp_history).strip() #* working!
|
| 232 |
+
# # yield [[prompt, result]] #* 记得这个格式。这只能单论聊天。
|
| 233 |
+
|
| 234 |
+
# ##* 多轮聊天的版本。
|
| 235 |
+
# input_prompt[-1][1] += answer
|
| 236 |
+
# yield input_prompt
|
| 237 |
+
##* 多轮会话,带有历史记忆。因为history参数的原因?
|
| 238 |
+
# for response, history in chatglm.model.stream_chat(chatglm.tokenizer, query=input_prompt[-1][0], history=history): ## 一轮对话。working.
|
| 239 |
+
# for response, history in chatglm.model.stream_chat(chatglm.tokenizer, query=str(input_prompt)): ## 这里保留了所有的chat history在input_prompt中。
|
| 240 |
+
# for response, history in chatglm.model.stream_chat(chatglm.tokenizer, query=str(input_prompt[-1][0])): ## 从用langchain的自定义方式来做。
|
| 241 |
+
# for response, history in chatglm.model.stream_chat(chatglm.tokenizer, query=prompt): ## 这里保留了所有的chat history在input_prompt中。
|
| 242 |
+
# for response, history in chatglm.model.stream_chat(chatglm.tokenizer, query=prompt): ### Working. original code here. 这里保留了所有的chat history在input_prompt中。
|
| 243 |
+
input_prompt[-1][1] = ""
|
| 244 |
+
messages = [
|
| 245 |
+
{'role': 'user', 'content': prompt}]
|
| 246 |
+
responses = Generation.call(model="qwen-turbo",
|
| 247 |
+
messages=messages,
|
| 248 |
+
result_format='message', # 设置输出为'message'格式
|
| 249 |
+
stream=True, # 设置输出方式为流式输出
|
| 250 |
+
incremental_output=True # 增量式流式输出
|
| 251 |
+
)
|
| 252 |
+
for response in responses:
|
| 253 |
+
if response.status_code == HTTPStatus.OK and len(response.output.choices[0]['message']['content']) > 0:
|
| 254 |
+
# if response != "<br>":
|
| 255 |
+
# print('response of model:', response)
|
| 256 |
+
# input_prompt[-1][1] = response ## working. ChatGLM的输出。
|
| 257 |
+
|
| 258 |
+
# response.output.choices[0]['message']['content'] ### 标准的Qwen输出格式。
|
| 259 |
+
# input_prompt[-1][1] = response.output.choices[0]['message']['content'] ## working.
|
| 260 |
+
input_prompt[-1][1] += response.output.choices[0]['message']['content'] ## working.
|
| 261 |
+
yield input_prompt
|
| 262 |
+
|
| 263 |
+
except Exception as e:
|
| 264 |
+
print(e)
|
| 265 |
+
error = str(e)
|
| 266 |
+
# messages = [{"role": "system", "content": "你是一个专业和友好的AI助手。"},]
|
| 267 |
+
# messages.append({"role": "user", "content": ""})
|
| 268 |
+
# input_prompt[-1][1] += error
|
| 269 |
+
input_prompt[-1][1] = error
|
| 270 |
+
yield input_prompt ## 将错误打印到output的textbox里面。
|
| 271 |
+
|
| 272 |
+
# print(input_prompt)
|
| 273 |
+
return input_prompt
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
## 插入chatbot的user问题。 原始代码参考: https://www.gradio.app/guides/creating-a-custom-chatbot-with-blocks
|
| 277 |
+
def user(user_message, chat_history):
|
| 278 |
+
# print('chat_history:', chat_history)
|
| 279 |
+
# return "", chat_history + [[user_message, None]] ## possiblly orginal code?
|
| 280 |
+
return "", chat_history + [[user_message, None]]
|
| 281 |
+
|
| 282 |
+
## 报告模块的内容合并。Gradio不接受直接的str相加,所以要通过函数来实现,并且在button click中实现。
|
| 283 |
+
def report_prompt(system_prompt, report_method, user_prompt, report_requirement):
|
| 284 |
+
total_prompt = system_prompt + "\n\n" + "你撰写报告时采用的分析方法是:" + report_method + "\n\n 以下是报告的基础信息:\n" + user_prompt + "\n\n 其他报告写作的要求如下:\n\n" + report_requirement
|
| 285 |
+
return total_prompt
|
| 286 |
+
|
| 287 |
+
## 数据分析模块的内容合并。Gradio不接受直接的str相加,所以要通过函数来实现,并且在button click中实现。
|
| 288 |
+
def data_prompt(system_prompt, method_prompt, user_prompt, report_requirement):
|
| 289 |
+
total_prompt = system_prompt + "\n" + user_prompt.to_markdown() + "\n\n" +"分析方法采用:"+ method_prompt + "\n\n"+ report_requirement
|
| 290 |
+
return total_prompt
|
| 291 |
+
|
| 292 |
+
### 培训模块的内容合并。Gradio不接受直接的str相加,所以要通过函数来实现,并且在button click中实现。
|
| 293 |
+
def train_combine_prompt(train_doc, user_prompt, train_requirement):
|
| 294 |
+
total_prompt = train_doc + "\n\n" + user_prompt + "\n\n" + train_requirement
|
| 295 |
+
return total_prompt
|
| 296 |
+
|
| 297 |
+
def clear_conversation():
|
| 298 |
+
return gr.update(value=None, visible=True), None, "", get_empty_state()
|
| 299 |
+
# return "", "", []
|
| 300 |
+
|
| 301 |
+
css = """
|
| 302 |
+
#mybutton {background-color: #CEFAFE; color: #06B6D4;}
|
| 303 |
+
#textarea {-webkit-text-fill-color:black; -webkit-opacity: 1;}
|
| 304 |
+
.message {font: 16px Arial, sans-serif, 'ui-sans-serif', Montserrat, 'system-ui';}
|
| 305 |
+
"""
|
| 306 |
+
# css = None
|
| 307 |
+
|
| 308 |
+
with gr.Blocks(theme=gr.themes.Base(primary_hue='sky', text_size='md'), css=css, title="大语言模型专项模块", js=js_func) as demo:
|
| 309 |
+
# with gr.Blocks(theme=gr.themes.Monochrome(text_size='md'), css=css, title="培训资料专项大语言模型") as demo:
|
| 310 |
+
state = gr.State(get_empty_state())
|
| 311 |
+
|
| 312 |
+
with gr.Row():
|
| 313 |
+
# with gr.Column(elem_id="col-container",scale=1):
|
| 314 |
+
# # with gr.Tab('参考提示词'):
|
| 315 |
+
# prompt_template = gr.Dropdown(label="选择提示词类型:", value="教案策划",choices=list(prompt_templates.keys()))
|
| 316 |
+
# default_prompt_value = "我希望您充当教育内容创建者。您需要为教科书、在线课程和讲义等学习材料创建引人入胜且信息丰富的内容。我的第一个建议请求是“我需要帮助制定针对高中生的可再生能源课程计划。”"
|
| 317 |
+
# prompt_template_preview = gr.Textbox(label="提示词预设内容:", value=default_prompt_value, show_label=True, lines=5, show_copy_button=True)
|
| 318 |
+
|
| 319 |
+
with gr.Column(elem_id="col-container",scale=4):
|
| 320 |
+
gr.Markdown("""# **大语言模型企业智能化管理中心** """, elem_id="header")
|
| 321 |
+
gr.Markdown(""" 说明: 本大模型为企业专项定制。提供高度可控、可调节、可迭代的大语言模型工具箱。用户可以根据自己的需求,灵活配置工具箱的功能和参数。可以满足不同用户的个性化需求,让用户能够充分发挥大语言模型的潜力。例如,对于常规业务人员,可以选择使用功能简单、参数较少的工具箱,以便快速上手。对于经验丰富的用户来说,可以选择使用功能复杂、参数较多的工具箱,以便进行更深入的研究。适用于各种业务场景的开发和使用。""")
|
| 322 |
+
|
| 323 |
+
with gr.Row():
|
| 324 |
+
with gr.Column():
|
| 325 |
+
# gr.Markdown("""### 企业级大语言模型 """)
|
| 326 |
+
chatbot = gr.Chatbot(elem_id="message", height=600, label="大模型对话区", show_label=True, show_copy_button=True, layout='panel', likeable=True) ## style来设置对话框高度。
|
| 327 |
+
# chatbot = gr.Chatbot(elem_id="message", height=600, label="中交建大模型对话区") ## style来设置对话框高度。
|
| 328 |
+
# output_message = gr.Textbox(label='大语言模型的回答',lines=10).style(show_copy_button=True) ## textbox version。style来设置对话框高度。
|
| 329 |
+
# radio = gr.Radio(['培训资料生成模式', '资料优化模型','配图生成模式'], value='培训资料生成模式',label="大语言模型运行模式")
|
| 330 |
+
|
| 331 |
+
## 根据要求选择不同的按键类型,button或者icon。
|
| 332 |
+
with gr.Row():
|
| 333 |
+
with gr.Column(min_width=837):
|
| 334 |
+
# with gr.Column(scale=8):
|
| 335 |
+
input_message = gr.Textbox(lines=1, label="输入您的要求", show_label=True, placeholder="在这里输入您的要求按Enter提交", visible=True, show_copy_button=True)
|
| 336 |
+
|
| 337 |
+
with gr.Row():
|
| 338 |
+
# with gr.Column(min_width=15):
|
| 339 |
+
with gr.Column(scale=1):
|
| 340 |
+
# btn_clear_conversation = gr.Button("\u2716", variant="primary", visible=True).style(full_width=False, size="lg")
|
| 341 |
+
btn_clear_conversation = gr.Button("开启新对话", variant="secondary", visible=True, size='lg')
|
| 342 |
+
# with gr.Column(scale=1):
|
| 343 |
+
# # btn_stop = gr.Button("\u25FD", variant="primary", visible=True).style(full_width=False, size="lg")
|
| 344 |
+
# btn_stop = gr.Button("终止当前问题/任务", variant="secondary", visible=True, size='lg')
|
| 345 |
+
# with gr.Column(scale=1):
|
| 346 |
+
# # btn_stop = gr.Button("\u25FD", variant="primary", visible=True).style(full_width=False, size="lg")
|
| 347 |
+
# btn_generate = gr.Button("一键生成分析报告", variant="primary", visible=True, size='lg')
|
| 348 |
+
with gr.Column(min_width=100):
|
| 349 |
+
# with gr.Column(scale=1):
|
| 350 |
+
btn_regenerate = gr.Button("重新生成答案", variant="secondary", visible=True, size='lg')
|
| 351 |
+
# with gr.Column(scale=1):
|
| 352 |
+
# # btn_stop = gr.Button("\u25FD", variant="primary", visible=True).style(full_width=False, size="lg")
|
| 353 |
+
# btn_exam = gr.Button("一键生成培训考题", variant="primary", visible=True, size='lg')
|
| 354 |
+
with gr.Column(scale=1):
|
| 355 |
+
# btn_submit = gr.Button("\u2714", variant="primary", visible=True).style(full_width=False, size="lg")
|
| 356 |
+
btn_submit = gr.Button("提交您的要求", variant="primary", visible=True, size='lg')
|
| 357 |
+
|
| 358 |
+
with gr.Column(scale=2): ### 原先设置为2.
|
| 359 |
+
# gr.Markdown("## 工作区")
|
| 360 |
+
# with gr.Tab('专项业务板块'):
|
| 361 |
+
'''以下为报告撰写模块'''
|
| 362 |
+
with gr.Tab('报告撰写模块'):
|
| 363 |
+
# with gr.Accordion('报告撰写模块', open=True):
|
| 364 |
+
# report_input = gr.Textbox(lines=5, label="输入报告基础内容", show_label=True, placeholder="报告基础内容", visible=True, show_copy_button=True)
|
| 365 |
+
report_input = gr.Textbox(lines=5, label="输入基本信息", show_label=True, placeholder="中小企业加快数字化转型,龙头骨干企业更要发挥引领示范作用。走进位于两江新区的集团自动化的“黑灯工厂”,1000多台机器人高速运转,机械手臂有序协作。在这里,冲压完成一套汽车部件仅需5秒钟,2分钟便可下线一辆新车。", visible=True, show_copy_button=True)
|
| 366 |
+
# train_sys_prompt = gr.Textbox("你是一个专业的咨询顾问。你写作的风格专业且详实,使用的语言为中文。我需要你根据如下内容撰写一份报告:", label="报告要求设定",visible=True, interactive=True, show_copy_button=True)
|
| 367 |
+
report_sys_prompt = gr.Textbox("你是一个专业的咨询顾问。我需要你撰写报告,你的写作风格专业且尽可能的详细,除非特别说明否则你使用的语言为中文。", label="报告要求设定", visible=True, interactive=True, show_copy_button=True)
|
| 368 |
+
report_method = gr.Dropdown(choices=['扩写内容','润色文案','重写材料'], interactive=True, label="报告撰写方法", value="扩写内容", multiselect=False, show_label=True)
|
| 369 |
+
report_requirement = gr.Textbox("""标题:简明扼要地概括汇报内容。
|
| 370 |
+
正文:分为[基本情况]、[主要做法]、[下一步计划]三个部分。
|
| 371 |
+
结尾:总结全文,提出希望。
|
| 372 |
+
""", label="报告格式设定",show_label=True, show_copy_button=True,visible=True, interactive=True)
|
| 373 |
+
# train_text = gr.Textbox(lines=3, label='输出内容风格', show_label=True, show_copy_button=True, value=report_requirement)
|
| 374 |
+
btn_report = gr.Button("一键生成汇报材料", variant="primary", visible=True, size='lg')
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
'''以下为简易分析模块'''
|
| 378 |
+
with gr.Tab('商业分析模块'):
|
| 379 |
+
# with gr.Accordion('报告撰写模块', open=True):
|
| 380 |
+
biz_analysis_input = gr.Textbox(lines=5, label="输入基本信息", show_label=True, placeholder="9月份我部门共成单19件(注:8月份为21件);收到既有客户投诉246起(注:8月份为199起);潜在客户咨询152起(注:与8月份124起)", visible=True, show_copy_button=True)
|
| 381 |
+
# train_sys_prompt = gr.Textbox("你是一个专业的咨询顾问。你写作的风格专业且详实,使用的语言为中文。我需要你根据如下内容撰写一份报告:", label="报告要求设定",visible=True, interactive=True, show_copy_button=True)
|
| 382 |
+
biz_analysis_sys_prompt = gr.Textbox(lines=3, value="你是一个专业的商业分析顾问。我需要你撰写商业分析材料,你的写作风格专业且尽可能的详细,除非特别说明否则你使用的语言为中文。", label="报告要求设定", visible=True, interactive=True, show_copy_button=True)
|
| 383 |
+
biz_analysis_method = gr.Dropdown(choices=['头脑风暴', '整体分析','趋势判断','SMART分析','SWOT分析','精益分析','5W1H分析','PEST分析','金字塔原理','六顶思考帽'], interactive=True, label="商业分析方法", value="头脑风暴", multiselect=False, show_label=True)
|
| 384 |
+
biz_analysis_requirement = gr.Textbox(lines=4, value="""1. 将数据整理成Markdown表格的形式。
|
| 385 |
+
2. 必须有标题。
|
| 386 |
+
3. 使用一、二、三格式的序号。
|
| 387 |
+
4. 最后有总结。
|
| 388 |
+
""", label="报告格式设定",show_label=True, show_copy_button=True,visible=True, interactive=True)
|
| 389 |
+
# train_text = gr.Textbox(lines=3, label='输出内容风格', show_label=True, show_copy_button=True, value=biz_analysis_requirement)
|
| 390 |
+
btn_biz_analysis = gr.Button("一键生成分析报告", variant="primary", visible=True, size='lg')
|
| 391 |
+
|
| 392 |
+
###NOTE: 以下模块为数据分析模块,国产模型不稳定,暂时不开放!
|
| 393 |
+
# with gr.Tab('高级分析模块'):
|
| 394 |
+
# ## 上传功能
|
| 395 |
+
# with gr.Row():
|
| 396 |
+
# # file_output = gr.UploadButton(label='上传数据文件区', file_count="single", file_types=[".csv"])
|
| 397 |
+
# file_output = gr.File(label='上传数据文件区', file_count="single", file_types=[".csv"])
|
| 398 |
+
# ##NOTE: convert the uploaded file into dataframe, so that the Gradio can process it.
|
| 399 |
+
# def csv_file(file_output):
|
| 400 |
+
# res = pd.read_csv(file_output)
|
| 401 |
+
# return res
|
| 402 |
+
# with gr.Row():
|
| 403 |
+
# md_table = gr.DataFrame()
|
| 404 |
+
# file_output.upload(fn=csv_file, inputs=file_output, outputs=md_table, queue=False) #! Gradio中需要通过函数来获得结果,否则都是Gradio内部定义的数据结构,如File,无法被直接访问或使用。
|
| 405 |
+
# # with gr.Row():
|
| 406 |
+
# # gr.DataFrame("/Users/yunshi/Downloads/360Data/Data Center/Working-On Task/演讲与培训/2023ChatGPT/Coding/code_interpreter/rawdata/iris.csv")
|
| 407 |
+
# data_sys_prompt = gr.Textbox("""你是一个专业的数据科学家。你需要保持专业的风格且分析内容详实,使用的语言为中文,回复时使用markdown格式。我需要你对如下数据中的[X, Y]进行分析并给出计算结果:\n\n""", lines=3, label="分析要求设定",visible=True, interactive=True, show_copy_button=True)
|
| 408 |
+
# data_method = gr.Dropdown(choices=['数据整体描述','描述性统计:汇总值、平均值、标准差、分位数', '相关性分析', '分类汇总'], label="数据分析方法", value="描述性统计:平均值、汇总值、标准差、中位数、极数", multiselect=False, interactive=True,show_label=True)
|
| 409 |
+
# # data_input = gr.Textbox(lines=5, label="输入数据内容", show_label=True, placeholder="数据基础内容", visible=True, show_copy_button=True, interactive=True)
|
| 410 |
+
# data_requirement = gr.Textbox("""目标:发现数据中的问题和关键点。
|
| 411 |
+
# 分析方法:解释上述分析方法的基本原理。
|
| 412 |
+
# 分析结论:提出结论,揭示潜在可能原因和对应的措施。
|
| 413 |
+
# """, lines=5, label="分析输出设定",show_label=True, show_copy_button=True,visible=True, interactive=True)
|
| 414 |
+
# # data_analysis_requirement = gr.Textbox(lines=3, label='输出内容风格', show_label=True, show_copy_button=True, value=data_requirement)
|
| 415 |
+
# btn_data = gr.Button("一键生成数据分析", variant="primary", visible=True, size='lg')
|
| 416 |
+
|
| 417 |
+
with gr.Tab('培训资料模块'):
|
| 418 |
+
# with gr.Accordion('培训资料模块', open=False):
|
| 419 |
+
with gr.Row():
|
| 420 |
+
# file_output = gr.UploadButton(label='上传数据文件区', file_count="single", file_types=[".csv"])
|
| 421 |
+
uploaded_training_file = gr.File(label='上传培训资料区(注:文档内容限制在3000个汉字以内)', file_count="single", file_types=[".docx"])
|
| 422 |
+
##NOTE: convert the uploaded file into dataframe, so that the Gradio can process it.
|
| 423 |
+
def convert_file(upload_file):
|
| 424 |
+
# res = pd.read_csv(file_output) ## read csv file.
|
| 425 |
+
### 读取教材资料,docx文档
|
| 426 |
+
doc = docx.Document(upload_file)
|
| 427 |
+
my_text = ""
|
| 428 |
+
for paragraph in doc.paragraphs:
|
| 429 |
+
if not paragraph.text.isspace(): ## 判断是否有空行。
|
| 430 |
+
my_text += paragraph.text
|
| 431 |
+
|
| 432 |
+
### 在工作台展示一部分内容
|
| 433 |
+
show_text = ""
|
| 434 |
+
for paragraph in doc.paragraphs[:5]:
|
| 435 |
+
if not paragraph.text.isspace(): ## 判断是否有空行。
|
| 436 |
+
show_text += paragraph.text
|
| 437 |
+
# print(show_text)
|
| 438 |
+
|
| 439 |
+
return my_text, show_text
|
| 440 |
+
|
| 441 |
+
# my_text, show_text = convert_file(uploaded_training_file) ## 单独运行一次这个函数,为了后面直接拼接字符串用。
|
| 442 |
+
|
| 443 |
+
with gr.Row():
|
| 444 |
+
my_doc = gr.TextArea(visible=False)
|
| 445 |
+
show_doc = gr.TextArea(label="培训资料展示与确认",visible=True, lines=5)
|
| 446 |
+
uploaded_training_file.upload(fn=convert_file, inputs=uploaded_training_file, outputs=[my_doc, show_doc],queue=False) #! Gradio中需要通过函数来获得结果,否则都是Gradio内部定义的数据结构,如File,无法被直接访问或使用。
|
| 447 |
+
|
| 448 |
+
## 一键生成培训资料的环境设定
|
| 449 |
+
# training_content = show_doc.replace("['", "").replace("']", "")
|
| 450 |
+
# system_prompt_message = """你是一个专业的培训材料制作者。我需要你根据提供的内容生成培训材料,内容如下:"""
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
|
| 454 |
+
train_prompt = gr.Textbox(value=("""你是一个专业的培训材料制作者。我需要你根据提供的内容生成培训材料,内容如下:"""), visible=False) #! 注意这里的type,不能是简单的str。
|
| 455 |
+
# system_prompt = [{"role": "system", "content": f"""{system_prompt_message} {training_content}"""}] ### userful only in OpenAI API.
|
| 456 |
+
|
| 457 |
+
# train_requirement_text = """标题:[培训主题]
|
| 458 |
+
# 培训目标:[培训目标]
|
| 459 |
+
# 模块内容:涵盖目标和关键领域的详细内容
|
| 460 |
+
# 培训方式:引人入胜的活动,以强化所讨论的概念
|
| 461 |
+
# 评估方法:评估学习者理解和知识保留的方法。
|
| 462 |
+
# """
|
| 463 |
+
# train_requirement = gr.Textbox(lines=5, label='报告风格', show_label=True, show_copy_button=True, value=train_requirement_text)
|
| 464 |
+
train_requirement = gr.Textbox(lines=5, label='培训材料风格', show_label=True, show_copy_button=True, value="""标题:[培训主题]
|
| 465 |
+
培训目标:[培训目标]
|
| 466 |
+
模块内容:涵盖目标和关键领域的详细内容
|
| 467 |
+
培训方式:引人入胜的活动,以强化所讨论的概念
|
| 468 |
+
评估方法:评估学习者理解和知识保留的方法。
|
| 469 |
+
""")
|
| 470 |
+
total_prompt = gr.TextArea(visible=False)
|
| 471 |
+
|
| 472 |
+
# uploaded_training_file.change(train_combine_prompt, [train_prompt, my_doc, train_requirement], total_prompt)
|
| 473 |
+
|
| 474 |
+
# total_prompt = (f"""你是一个专业的培训材料制作者。我需要你根据提供的内容生成培训材料,内容如下: \n {my_text}""") + "\n 培训内容设计需要参考如下要求:" + train_requirement
|
| 475 |
+
btn_training = gr.Button("一键生成培训资料", variant="primary", visible=True, size='lg')
|
| 476 |
+
|
| 477 |
+
traing_exam_prompt = gr.Textbox(lines=2, value="""你是一个专业的培训材料制作者。我需要你根据上述内容,设计3道单选题和2道问答题。你需要给出题目内容和对应的参考答案。""", label="培训考题要求", show_copy_button=True, visible=True) #! 注意这里的type,不能是简单的str。
|
| 478 |
+
btn_training_exam = gr.Button("一键生成培训考试题目", variant="primary", visible=True, size='lg')
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
with gr.Tab(label='高级设置', elem_id='tab'):
|
| 482 |
+
with gr.Accordion('模型设置', open=True):
|
| 483 |
+
radio = gr.Radio(['核心模式', '优化模式','其他模式'], value='核心模式', label="大语言模型运行模式")
|
| 484 |
+
claim_value = str("大语言具有多种高级设置选项来调整其模型。1. Temperature:温度调整文本的多样性。温度值越高,生成的文本越随机。2. Token:控制生成文本的长度。3. 'top_p':0.0到1.0 (默认 1.0) ,类似Temperature,也叫核采样。4.presence_penalty:惩罚原始文本中已经出现过的单词/短语,从而鼓励生成无重复的输出。"
|
| 485 |
+
)
|
| 486 |
+
claim = gr.Textbox(value=claim_value, label="大模型参数简介",type="text", show_label=True, lines=5)
|
| 487 |
+
temperature = gr.Slider(minimum=0, maximum=2.0, value=0.7, step=0.1, label="Temperature参数",info="数值越高语句越灵活")
|
| 488 |
+
max_tokens = gr.Slider(minimum=100, maximum=14096, value=3000, step=100,
|
| 489 |
+
label="单次聊天最多Token数", info="平均1.12个token约等于1个汉字")
|
| 490 |
+
top_p = gr.Slider(minimum=0, maximum=1, value=1, step=0.1, label="top_p参数",info="数值越低语句越固定")
|
| 491 |
+
presence_penalty = gr.Slider(minimum=0, maximum=1, value=0.5, step=0.1, label="penalty参数",info="0没有惩罚,1完全禁止输出复制的单词")
|
| 492 |
+
|
| 493 |
+
# with gr.Tab('参考提示词'):
|
| 494 |
+
with gr.Accordion('参考提示词', open=True):
|
| 495 |
+
prompt_template = gr.Dropdown(label="选择提示词类型:", value="教案策划",choices=list(prompt_templates.keys()))
|
| 496 |
+
default_prompt_value = "我希望您充当教育内容创建者。您需要为教科书、在线课程和讲义等学习材料创建引人入胜且信息丰富的内容。我的第一个建议请求是“我需要帮助制定针对高中生的可再生能源课程计划。”"
|
| 497 |
+
prompt_template_preview = gr.Textbox(label="提示词预设内容:", value=default_prompt_value, show_label=True, lines=5, show_copy_button=True)
|
| 498 |
+
|
| 499 |
+
### 上传功能模块
|
| 500 |
+
# with gr.Tab('上传文件'):
|
| 501 |
+
# with gr.Row():
|
| 502 |
+
# file_output = gr.File(label='上传文件区', file_count="single", file_types=[".pdf", ".docx",".csv"])
|
| 503 |
+
# with gr.Column(scale=1):
|
| 504 |
+
# btn_process = gr.Button('解析文件', variant="primary", visible=True, size='lg')
|
| 505 |
+
|
| 506 |
+
## click + submit. 注意,这里的input_message必须是类似gr.Textbox这样的模块type。普通的str不行。
|
| 507 |
+
btn_submit_event = btn_submit.click(user, [input_message, chatbot], [input_message, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 508 |
+
input_message.submit(user, [input_message, chatbot], [input_message, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 509 |
+
btn_clear_conversation.click(clear_conversation, [], [input_message, chatbot])
|
| 510 |
+
|
| 511 |
+
### 报告撰写专项模块按键
|
| 512 |
+
total_prompt = gr.Textbox(visible=False) ## 用于汇总所有的prompt,然后输入chatbot。
|
| 513 |
+
btn_report.click(report_prompt, [report_sys_prompt, report_method, report_input, report_requirement], [total_prompt], queue=True).then(user, [total_prompt, chatbot], [total_prompt, chatbot], queue=True).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 514 |
+
|
| 515 |
+
### 商业分析撰写专项模块按键
|
| 516 |
+
total_prompt = gr.Textbox(visible=False) ## 用于汇总所有的prompt,然后输入chatbot。
|
| 517 |
+
btn_biz_analysis.click(report_prompt, [biz_analysis_sys_prompt, biz_analysis_method, biz_analysis_input, biz_analysis_requirement], [total_prompt], queue=False).then(user, [total_prompt, chatbot], [total_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 518 |
+
|
| 519 |
+
# ### 数据分析专项模块按键
|
| 520 |
+
##TODO 尝试用ChatGLM3中的code-interpreter来完成。
|
| 521 |
+
# # report_prompt = gr.Textbox(value=f"你是一个专业的咨询顾问。你写作的风格专业且详实,使用的语言为中文。我需要你根据如下内容撰写一份报告:", visible=False)
|
| 522 |
+
# # train_sys_prompt = gr.Textbox("你是一个专业的咨询顾问。你写作的风格专业且详实,使用的语言为中文。我需要你根据如下内容撰写一份报告:")
|
| 523 |
+
# total_prompt = gr.Textbox(visible=False) ## 用于汇总所有的prompt,然后输入chatbot。
|
| 524 |
+
# # btn_report.click(user, [report_prompt, chatbot], [report_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 525 |
+
# # btn_data.click(data_prompt, [data_sys_prompt, data_method, file_output, data_requirement], [total_prompt], queue=False).then(user, [total_prompt, chatbot], [total_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot) ## 可以看见这个file_output的完整链接。
|
| 526 |
+
# btn_data.click(data_prompt, [data_sys_prompt, data_method, md_table, data_requirement], [total_prompt], queue=False).then(user, [total_prompt, chatbot], [total_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot) ##TODO:尝试把数据导入。
|
| 527 |
+
|
| 528 |
+
|
| 529 |
+
### 培训资料专项模块按键
|
| 530 |
+
btn_training.click(train_combine_prompt, [train_prompt, my_doc, train_requirement], [total_prompt], queue=False).then(user, [total_prompt, chatbot], [total_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 531 |
+
|
| 532 |
+
## 把total_prompt作为中间变量。否则,textbox中的内容用user()函数提交后会被清空。
|
| 533 |
+
def exam_prompt(traing_exam_prompt):
|
| 534 |
+
total_prompt = traing_exam_prompt
|
| 535 |
+
return total_prompt
|
| 536 |
+
|
| 537 |
+
btn_training_exam.click(exam_prompt, traing_exam_prompt, total_prompt).then(user, [total_prompt, chatbot], [total_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot)
|
| 538 |
+
|
| 539 |
+
|
| 540 |
+
# null_message = gr.Textbox(value=None, visible=False)
|
| 541 |
+
# btn_regenerate.click(user, [null_message, chatbot], [null_message, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot) ## working.
|
| 542 |
+
|
| 543 |
+
# exam_prompt = gr.Textbox("你根据上述的内容,生成3道单选题和2道简答题。风格专业且简洁。", visible=False)
|
| 544 |
+
# btn_exam.click(user, [exam_prompt, chatbot], [exam_prompt, chatbot], queue=False).then(submit_message, [radio, chatbot,temperature,max_tokens,top_p,presence_penalty], chatbot) ## working.
|
| 545 |
+
|
| 546 |
+
## regenerate button。重新提交对话。
|
| 547 |
+
btn_regenerate.click(regenerate, chatbot, chatbot).then(submit_message, [
|
| 548 |
+
radio, chatbot, temperature, max_tokens, top_p, presence_penalty], chatbot)
|
| 549 |
+
## stop button中止提交程序运行。
|
| 550 |
+
# btn_stop.click(fn=None, inputs=None, outputs=None, cancels=[btn_submit_event])
|
| 551 |
+
|
| 552 |
+
prompt_template.change(on_prompt_template_change, inputs=[prompt_template], outputs=[prompt_template_preview])
|
| 553 |
+
|
| 554 |
+
demo.load()
|
| 555 |
+
|
| 556 |
+
auth_list = (
|
| 557 |
+
('1234', '1234'),
|
| 558 |
+
('joeshi', 'joeshi'),
|
| 559 |
+
('gj', 'gj'),
|
| 560 |
+
('cr', 'cr'),
|
| 561 |
+
('test001', 'test001'),
|
| 562 |
+
('test002', 'test002'),
|
| 563 |
+
('test003', 'test003'),
|
| 564 |
+
)
|
| 565 |
+
|
| 566 |
+
### 用户名和密码认证
|
| 567 |
+
# user_csv = pd.read_csv('auth_list_enterprise.csv')
|
| 568 |
+
# auth_list = [(x, y) for (x, y) in user_csv[['username', 'password']].values]
|
| 569 |
+
|
| 570 |
+
# demo.launch(height='1200px')
|
| 571 |
+
# demo.launch(height='1200px', auth=auth_list, auth_message="欢迎使用企业大语言模型培训专属模块", share=True)
|
| 572 |
+
# demo.launch(height='1500px', auth=auth_list, auth_message="欢迎使用中交建财务共享中心企业大语言模型平台", share=False).queue()
|
| 573 |
+
# demo.launch(height='1500px', server_name="0.0.0.0", server_port=7860, share=False).queue()
|
| 574 |
+
demo.launch(height='1500px', server_name="0.0.0.0", server_port=7860, auth=auth_list, auth_message="欢迎使用企业大语言模型平台", share=False).queue()
|
raw_prompts.xlsx
ADDED
|
Binary file (15.1 kB). View file
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
dashscope==1.17.0
|
| 2 |
+
docx==0.2.4
|
| 3 |
+
gradio==4.28.3
|
| 4 |
+
langchain==0.1.17
|
| 5 |
+
mdtex2html==1.2.0
|
| 6 |
+
numpy==1.26.4
|
| 7 |
+
pandas==2.2.2
|
| 8 |
+
PyPDF2==3.0.1
|
| 9 |
+
python-dotenv==1.0.1
|
| 10 |
+
python_docx==1.1.0
|
| 11 |
+
Requests==2.31.0
|
| 12 |
+
rich==13.7.1
|
| 13 |
+
transformers==4.37.1
|