LeeKinXUn commited on
Commit
7b2204c
·
1 Parent(s): b06970d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py CHANGED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
32
+ lines = []
33
+ for i in range(2,33,2):
34
+ line.pop(i)
35
+
36
+ for i in range(33):
37
+ line[i] = line[i][:-2]
38
+
39
+ for i in range(33):
40
+ lines.append([line[i],line[i+1]])
41
+
42
+ colu = ['question','answer']
43
+
44
+ df = pd.DataFrame(data=lines, columns=colu)
45
+ retriever = EmbeddingRetriever(
46
+ document_store=document_store,
47
+ embedding_model="sentence-transformers/all-MiniLM-L6-v2",
48
+ use_gpu=True,
49
+ scale_score=False,
50
+ )
51
+ question = list(df['question'].values)
52
+
53
+ df['embedding'] = retriever.embed_queries(queries=question).tolist()
54
+ df = df.rename(columns={'question': 'content'})
55
+ question = list(df['question'].values)
56
+ docs_to_index = df.to_dict(orient='records')
57
+ document_store.write_documents(docs_to_index)
58
+
59
+
60
+ def haysstack(input,retriever=retriever):
61
+ pipe = FAQPipeline(retriever=retriever)
62
+ prediction = pipe.run(query=input, params={"Retriever": {"top_k": 1}})
63
+ return prediction['answers']
64
+ # Run any question and change top_k to see more or less answers
65
+
66
+ import gradio as gr
67
+ from gradio.components import Textbox
68
+ inputs = Textbox(lines=7, label="请输入你的问题")
69
+ outputs = Textbox(lines=7, label="来自ChatGPT的回答")
70
+
71
+ gr.Interface(fn=haysstack, inputs=inputs, outputs=outputs, title="电商客服",
72
+ description="我是您的电商客服,您可以问任何你想知道的问题",
73
+ theme=gr.themes.Default()).launch(share=True)