LeeKinXUn commited on
Commit
2df6947
1 Parent(s): d2045bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -58
app.py CHANGED
@@ -1,72 +1,72 @@
1
- haystack.telemetry导入tutorial_running
2
- 导入日志
3
- haystack.document_stores导入InMemoryDocumentStore
4
- haystack.pipelines.standard_pipelines导入TextIndexingPipeline
5
- haystack.nodes导入BM25Retriever
6
- haystack.nodes导入FARMReader
7
- haystack.pipelines导入ExtractiveQAPipeline
8
- pprint导入pprint
9
- haystack.utils导入print_answers
10
- haystack.nodes 导入 EmbeddingRetriever
11
- 导入解码器
12
- haystack.pipelines 导入 FAQPipeline
13
- haystack.utils 导入 print_answers
14
- 导入日志
15
- haystack.telemetry 导入tutorial_running
16
- haystack.document_stores 导入 InMemoryDocumentStore
17
- haystack.nodes 导入 EmbeddingRetriever
18
- 将熊猫导入为pd
19
- haystack.pipelines 导入 FAQPipeline
20
- haystack.utils 导入 print_answers
21
 
22
- 教程_运行(6
23
 
24
- logging.basicConfig(format= "%(levelname)s - %(name)s - %(message)s" , level=logging.WARNING)
25
- logging.getLogger( “干草堆” ).setLevel(logging.INFO)
26
 
27
- 文档存储器 = InMemoryDocumentStore()
28
 
29
- f = codecs.open( 'faq.txt' , 'r' , 'UTF-8' )
30
- = f.readlines()
31
- = []
32
- 对于范围内的 i ( 2 , 33 , 2 ):
33
- 行.pop(i)
34
 
35
- 对于我在范围内(33):
36
- [i] = [i][:- 2 ]
37
 
38
- 对于我在范围内(0332): _
39
- lines.append([line[i],line[i+ 1 ]])
40
 
41
- colu = [ '问题' , '答案' ]
42
 
43
- df = pd.DataFrame(数据=行,列=列)
44
- 检测器 = EmbeddingRetriever(
45
- 文档存储器=文档存储器,
46
- embedding_model= "句子变换器/all-MiniLM-L6-v2" ,
47
- use_gpu=真,
48
- scale_score=假,
49
  )
 
50
 
 
 
 
 
 
51
 
52
- df[ '嵌入'] = retriever.embed_queries(查询=问题).tolist()
53
- df = df.rename(columns={ 'question' : 'content' })
54
- 问题 = 列表(df[ '问题' ].values)
55
- docs_to_index = df.to_dict(orient= '记录' )
56
- document_store.write_documents(docs_to_index)
57
 
 
 
 
 
 
58
 
59
- 定义haysstack(输入,检查器=检查器):
60
- pipe = FAQPipeline(检测器=检测器)
61
- prediction = pipe.run(query=input, params={ "Retriever" : { "top_k" : 1 }})
62
- 返回预测
63
- # 运行任何问题并更改 top_k 以查看更多或更少的答案
64
 
65
- 将易变导入为gr
66
- 从gradio.components导入文档
67
- inputs = Textbox(lines= 7 , label= "请输入你的问题" )
68
- outputs = Textbox(lines= 7 , label= "来自ChatGPT的回答" )
69
-
70
- gr.Interface(fn=haysstack, inputs=inputs, outputs=outputs, title= "电商客服" ,
71
- description= "我是您的电商客服,您可以问任何您想知道的问题" ,
72
- 主题=gr.themes.Default()).launch(share= True )
 
1
+ from haystack.telemetry import tutorial_running
2
+ import logging
3
+ from haystack.document_stores import InMemoryDocumentStore
4
+ from haystack.pipelines.standard_pipelines import TextIndexingPipeline
5
+ from haystack.nodes import BM25Retriever
6
+ from haystack.nodes import FARMReader
7
+ from haystack.pipelines import ExtractiveQAPipeline
8
+ from pprint import pprint
9
+ from haystack.utils import print_answers
10
+ from haystack.nodes import EmbeddingRetriever
11
+ import codecs
12
+ from haystack.pipelines import FAQPipeline
13
+ from haystack.utils import print_answers
14
+ import logging
15
+ from haystack.telemetry import tutorial_running
16
+ from haystack.document_stores import InMemoryDocumentStore
17
+ from haystack.nodes import EmbeddingRetriever
18
+ import pandas as pd
19
+ from haystack.pipelines import FAQPipeline
20
+ from haystack.utils import print_answers
21
 
22
+ tutorial_running(6)
23
 
24
+ logging.basicConfig(format="%(levelname)s - %(name)s - %(message)s", level=logging.WARNING)
25
+ logging.getLogger("haystack").setLevel(logging.INFO)
26
 
27
+ document_store = InMemoryDocumentStore()
28
 
29
+ f = codecs.open('faq.txt','r','UTF-8')
30
+ line = f.readlines()
31
+ lines = []
32
+ for i in range(2,33,2):
33
+ line.pop(i)
34
 
35
+ for i in range(33):
36
+ line[i] = line[i][:-2]
37
 
38
+ for i in range(0,33,2):
39
+ lines.append([line[i],line[i+1]])
40
 
41
+ colu = ['question','answer']
42
 
43
+ df = pd.DataFrame(data=lines, columns=colu)
44
+ retriever = EmbeddingRetriever(
45
+ document_store=document_store,
46
+ embedding_model="sentence-transformers/all-MiniLM-L6-v2",
47
+ use_gpu=True,
48
+ scale_score=False,
49
  )
50
+ question = list(df['question'].values)
51
 
52
+ df['embedding'] = retriever.embed_queries(queries=question).tolist()
53
+ df = df.rename(columns={'question': 'content'})
54
+ question = list(df['question'].values)
55
+ docs_to_index = df.to_dict(orient='records')
56
+ document_store.write_documents(docs_to_index)
57
 
 
 
 
 
 
58
 
59
+ def haysstack(input,retriever=retriever):
60
+ pipe = FAQPipeline(retriever=retriever)
61
+ prediction = pipe.run(query=input, params={"Retriever": {"top_k": 1}})
62
+ return prediction['answers'].split(',')
63
+ # Run any question and change top_k to see more or less answers
64
 
65
+ import gradio as gr
66
+ from gradio.components import Textbox
67
+ inputs = Textbox(lines=7, label="请输入你的问题")
68
+ outputs = Textbox(lines=7, label="来自智能客服的回答")
 
69
 
70
+ gr.Interface(fn=haysstack, inputs=inputs, outputs=outputs, title="电商客服",
71
+ description="我是您的电商客服,您可以问任何你想知道的问题",
72
+ theme=gr.themes.Default()).launch(share=True)