Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,72 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
|
24 |
-
logging.basicConfig(format=
|
25 |
-
logging.getLogger(
|
26 |
|
27 |
-
|
28 |
|
29 |
-
f = codecs.open(
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
lines.append([line[i],line[i+
|
40 |
|
41 |
-
colu = [
|
42 |
|
43 |
-
df = pd.DataFrame
|
44 |
-
|
45 |
-
|
46 |
-
embedding_model=
|
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 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# 运行任何问题并更改 top_k 以查看更多或更少的答案
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
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)
|
|
|
|
|
|
|
|
|
|