Download app.py from LeeKinXUn/haysstack: direct link, hf CLI and curl.
- Browser
- Download file 2.52 kB
-
https://huggingface.co/spaces/LeeKinXUn/haysstack/resolve/main/app.py
- Command line
-
hf download hf://spaces/LeeKinXUn/haysstack/app.py
-
curl -L -o app.py https://huggingface.co/spaces/LeeKinXUn/haysstack/resolve/main/app.py
2.52 kB
| from haystack.telemetry import tutorial_running | |
| import logging | |
| from haystack.document_stores import InMemoryDocumentStore | |
| from haystack.pipelines.standard_pipelines import TextIndexingPipeline | |
| from haystack.nodes import BM25Retriever | |
| from haystack.nodes import FARMReader | |
| from haystack.pipelines import ExtractiveQAPipeline | |
| from pprint import pprint | |
| from haystack.utils import print_answers | |
| from haystack.nodes import EmbeddingRetriever | |
| import codecs | |
| from haystack.pipelines import FAQPipeline | |
| from haystack.utils import print_answers | |
| import logging | |
| from haystack.telemetry import tutorial_running | |
| from haystack.document_stores import InMemoryDocumentStore | |
| from haystack.nodes import EmbeddingRetriever | |
| import pandas as pd | |
| from haystack.pipelines import FAQPipeline | |
| from haystack.utils import print_answers | |
| tutorial_running(6) | |
| logging.basicConfig(format="%(levelname)s - %(name)s - %(message)s", level=logging.WARNING) | |
| logging.getLogger("haystack").setLevel(logging.INFO) | |
| document_store = InMemoryDocumentStore() | |
| f = codecs.open('faq.txt','r','UTF-8') | |
| line = f.readlines() | |
| lines = [] | |
| for i in range(2,33,2): | |
| line.pop(i) | |
| for i in range(33): | |
| line[i] = line[i][:-2] | |
| for i in range(0,33,2): | |
| lines.append([line[i],line[i+1]]) | |
| colu = ['question','answer'] | |
| df = pd.DataFrame(data=lines, columns=colu) | |
| retriever = EmbeddingRetriever( | |
| document_store=document_store, | |
| embedding_model="sentence-transformers/all-MiniLM-L6-v2", | |
| use_gpu=True, | |
| scale_score=False, | |
| ) | |
| question = list(df['question'].values) | |
| df['embedding'] = retriever.embed_queries(queries=question).tolist() | |
| df = df.rename(columns={'question': 'content'}) | |
| docs_to_index = df.to_dict(orient='records') | |
| document_store.write_documents(docs_to_index) | |
| def haysstack(input,retriever=retriever): | |
| pipe = FAQPipeline(retriever=retriever) | |
| prediction = pipe.run(query=input, params={"Retriever": {"top_k": 1}}) | |
| answer = str(prediction['answers'][0]) | |
| ans = answer.split(',')[0] | |
| return '亲,' + ans[19:] | |
| #prediction['answers'][0] | |
| # Run any question and change top_k to see more or less answers | |
| import gradio as gr | |
| from gradio.components import Textbox | |
| inputs = Textbox(lines=7, label="请输入你的问题") | |
| outputs = Textbox(lines=7, label="来自智能客服的回答") | |
| gr.Interface(fn=haysstack, inputs=inputs, outputs=outputs, title="电商客服", | |
| description="我是您的电商客服,您可以问任何你想知道的问题", | |
| theme=gr.themes.Default()).launch() | |