quincyqiang commited on
Commit
2a7b7b1
1 Parent(s): 972f2f1

feature@创建hf应用

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +2 -2
  3. clc/source_service.py +4 -4
  4. main.py +201 -0
README.md CHANGED
@@ -50,7 +50,7 @@ cchardet
50
  ### 启动Gradio
51
 
52
  ```shell
53
- python app.py
54
  ```
55
 
56
  ## 🚀 特性
 
50
  ### 启动Gradio
51
 
52
  ```shell
53
+ python main.py
54
  ```
55
 
56
  ## 🚀 特性
app.py CHANGED
@@ -192,9 +192,9 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
192
 
193
  demo.queue(concurrency_count=2).launch(
194
  server_name='0.0.0.0',
195
- server_port=8888,
196
  share=False,
197
  show_error=True,
198
  debug=True,
199
- enable_queue=True
 
200
  )
 
192
 
193
  demo.queue(concurrency_count=2).launch(
194
  server_name='0.0.0.0',
 
195
  share=False,
196
  show_error=True,
197
  debug=True,
198
+ enable_queue=True,
199
+ inbrowser=True,
200
  )
clc/source_service.py CHANGED
@@ -57,10 +57,10 @@ class SourceService(object):
57
 
58
  def search_web(self, query):
59
 
60
- SESSION.proxies = {
61
- "http": f"socks5h://localhost:7890",
62
- "https": f"socks5h://localhost:7890"
63
- }
64
  results = ddg(query)
65
  web_content = ''
66
  if results:
 
57
 
58
  def search_web(self, query):
59
 
60
+ # SESSION.proxies = {
61
+ # "http": f"socks5h://localhost:7890",
62
+ # "https": f"socks5h://localhost:7890"
63
+ # }
64
  results = ddg(query)
65
  web_content = ''
66
  if results:
main.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+
4
+ from app_modules.presets import *
5
+ from clc.langchain_application import LangChainApplication
6
+
7
+
8
+ # 修改成自己的配置!!!
9
+ class LangChainCFG:
10
+ llm_model_name = 'THUDM/chatglm-6b-int4-qe' # 本地模型文件 or huggingface远程仓库
11
+ embedding_model_name = 'GanymedeNil/text2vec-large-chinese' # 检索模型文件 or huggingface远程仓库
12
+ vector_store_path = './cache'
13
+ docs_path = './docs'
14
+ kg_vector_stores = {
15
+ '中文维基百科': './cache/zh_wikipedia',
16
+ '大规模金融研报知识图谱': '.cache/financial_research_reports',
17
+ '初始化知识库': '.cache',
18
+ } # 可以替换成自己的知识库,如果没有需要设置为None
19
+ # kg_vector_stores=None
20
+
21
+
22
+ config = LangChainCFG()
23
+ application = LangChainApplication(config)
24
+
25
+
26
+ def get_file_list():
27
+ if not os.path.exists("docs"):
28
+ return []
29
+ return [f for f in os.listdir("docs")]
30
+
31
+
32
+ file_list = get_file_list()
33
+
34
+
35
+ def upload_file(file):
36
+ if not os.path.exists("docs"):
37
+ os.mkdir("docs")
38
+ filename = os.path.basename(file.name)
39
+ shutil.move(file.name, "docs/" + filename)
40
+ # file_list首位插入新上传的文件
41
+ file_list.insert(0, filename)
42
+ application.source_service.add_document("docs/" + filename)
43
+ return gr.Dropdown.update(choices=file_list, value=filename)
44
+
45
+
46
+ def set_knowledge(kg_name, history):
47
+ try:
48
+ application.source_service.load_vector_store(config.kg_vector_stores[kg_name])
49
+ msg_status = f'{kg_name}知识库已成功加载'
50
+ except Exception as e:
51
+ msg_status = f'{kg_name}知识库未成功加载'
52
+ return history + [[None, msg_status]]
53
+
54
+
55
+ def clear_session():
56
+ return '', None
57
+
58
+
59
+ def predict(input,
60
+ large_language_model,
61
+ embedding_model,
62
+ top_k,
63
+ use_web,
64
+ history=None):
65
+ # print(large_language_model, embedding_model)
66
+ print(input)
67
+ if history == None:
68
+ history = []
69
+
70
+ if use_web == '使用':
71
+ web_content = application.source_service.search_web(query=input)
72
+ else:
73
+ web_content = ''
74
+ resp = application.get_knowledge_based_answer(
75
+ query=input,
76
+ history_len=1,
77
+ temperature=0.1,
78
+ top_p=0.9,
79
+ top_k=top_k,
80
+ web_content=web_content,
81
+ chat_history=history
82
+ )
83
+ history.append((input, resp['result']))
84
+ search_text = ''
85
+ for idx, source in enumerate(resp['source_documents'][:4]):
86
+ sep = f'----------【搜索结果{idx + 1}:】---------------\n'
87
+ search_text += f'{sep}\n{source.page_content}\n\n'
88
+ print(search_text)
89
+ search_text += "----------【网络检索内容】-----------\n"
90
+ search_text += web_content
91
+ return '', history, history, search_text
92
+
93
+
94
+ with open("assets/custom.css", "r", encoding="utf-8") as f:
95
+ customCSS = f.read()
96
+ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
97
+ gr.Markdown("""<h1><center>Chinese-LangChain</center></h1>
98
+ <center><font size=3>
99
+ </center></font>
100
+ """)
101
+ state = gr.State()
102
+
103
+ with gr.Row():
104
+ with gr.Column(scale=1):
105
+ embedding_model = gr.Dropdown([
106
+ "text2vec-base"
107
+ ],
108
+ label="Embedding model",
109
+ value="text2vec-base")
110
+
111
+ large_language_model = gr.Dropdown(
112
+ [
113
+ "ChatGLM-6B-int4",
114
+ ],
115
+ label="large language model",
116
+ value="ChatGLM-6B-int4")
117
+
118
+ top_k = gr.Slider(1,
119
+ 20,
120
+ value=4,
121
+ step=1,
122
+ label="检索top-k文档",
123
+ interactive=True)
124
+ kg_name = gr.Radio(['中文维基百科',
125
+ '大规模金融研报知识图谱',
126
+ '初始化知识库'
127
+ ],
128
+ label="知识库",
129
+ value='初始化知识库',
130
+ interactive=True)
131
+ set_kg_btn = gr.Button("重新加载知识库")
132
+
133
+ use_web = gr.Radio(["使用", "不使用"], label="web search",
134
+ info="是否使用网络搜索,使用时确保网络通常",
135
+ value="不使用"
136
+ )
137
+
138
+ file = gr.File(label="将文件上传到知识库库,内容要尽量匹配",
139
+ visible=True,
140
+ file_types=['.txt', '.md', '.docx', '.pdf']
141
+ )
142
+
143
+ file.upload(upload_file,
144
+ inputs=file,
145
+ outputs=None)
146
+ with gr.Column(scale=4):
147
+ with gr.Row():
148
+ chatbot = gr.Chatbot(label='Chinese-LangChain').style(height=400)
149
+ with gr.Row():
150
+ message = gr.Textbox(label='请输入问题')
151
+ with gr.Row():
152
+ clear_history = gr.Button("🧹 清除历史对话")
153
+ send = gr.Button("🚀 发送")
154
+ with gr.Row():
155
+ gr.Markdown("""提醒:<br>
156
+ [Chinese-LangChain](https://github.com/yanqiangmiffy/Chinese-LangChain) <br>
157
+ 有任何使用问题[Github Issue区](https://github.com/yanqiangmiffy/Chinese-LangChain)进行反馈. <br>
158
+ """)
159
+ with gr.Column(scale=2):
160
+ search = gr.Textbox(label='搜索结果')
161
+
162
+ set_kg_btn.click(
163
+ set_knowledge,
164
+ show_progress=True,
165
+ inputs=[kg_name, chatbot],
166
+ outputs=chatbot
167
+ )
168
+ # 发送按钮 提交
169
+ send.click(predict,
170
+ inputs=[
171
+ message, large_language_model,
172
+ embedding_model, top_k, use_web,
173
+
174
+ state
175
+ ],
176
+ outputs=[message, chatbot, state, search])
177
+
178
+ # 清空历史对话按钮 提交
179
+ clear_history.click(fn=clear_session,
180
+ inputs=[],
181
+ outputs=[chatbot, state],
182
+ queue=False)
183
+
184
+ # 输入框 回车
185
+ message.submit(predict,
186
+ inputs=[
187
+ message, large_language_model,
188
+ embedding_model, top_k, use_web,
189
+ state
190
+ ],
191
+ outputs=[message, chatbot, state, search])
192
+
193
+ demo.queue(concurrency_count=2).launch(
194
+ server_name='0.0.0.0',
195
+ server_port=8888,
196
+ share=False,
197
+ show_error=True,
198
+ debug=True,
199
+ enable_queue=True,
200
+ inbrowser=True,
201
+ )