ruisp commited on
Commit
1b01c15
1 Parent(s): 606b05f

removed duplicate

Browse files
Files changed (1) hide show
  1. public_app.py +0 -83
public_app.py DELETED
@@ -1,83 +0,0 @@
1
- import logging
2
-
3
- from langchain import PromptTemplate, LLMChain
4
- from langchain.chains.question_answering import load_qa_chain
5
- from langchain.vectorstores import FAISS
6
- from langchain.embeddings import HuggingFaceEmbeddings
7
- from langchain.chat_models import ChatOpenAI
8
- import gradio as gr
9
- import json
10
-
11
- from prompts import PROMPT_EXTRACT_DATE, PROMPT_FED_ANALYST
12
- from filterminutes import search_with_filter
13
-
14
- # --------------------------Load the sentence transformer and the vector store--------------------------#
15
- model_name = 'sentence-transformers/all-mpnet-base-v2'
16
- model_kwargs = {'device': 'cpu'}
17
- encode_kwargs = {'normalize_embeddings': False}
18
- embeddings = HuggingFaceEmbeddings(model_name=model_name, model_kwargs=model_kwargs, encode_kwargs=encode_kwargs)
19
- vs = FAISS.load_local("MINUTES_FOMC_HISTORY", embeddings)
20
-
21
- # --------------------------Import the prompts------------------#
22
- PROMPT_DATE = PromptTemplate.from_template(PROMPT_EXTRACT_DATE)
23
- PROMPT_ANALYST = PromptTemplate.from_template(PROMPT_FED_ANALYST)
24
-
25
-
26
- # --------------------------define the qa chain for answering queries--------------------------#
27
- def load_chains(open_ai_key):
28
- date_extractor = LLMChain(llm=ChatOpenAI(temperature=0, model_name='gpt-3.5-turbo', openai_api_key=open_ai_key),
29
- prompt=PROMPT_DATE)
30
- fed_chain = load_qa_chain(llm=ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0, openai_api_key=open_ai_key),
31
- chain_type='stuff', prompt=PROMPT_ANALYST)
32
- return date_extractor, fed_chain
33
-
34
-
35
- def get_chain(query, api_key):
36
- """
37
- Detects the date, computes similarity, and answers the query using
38
- only documents corresponding to the date requested.
39
- The query is first passed to the date extractor to extract the date
40
- and then to the qa chain to answer the query.
41
- Parameters
42
- ----------
43
- query : str
44
- Query to be answered.
45
- api_key : str
46
- OpenAI API key.
47
-
48
- Returns
49
- Answer to the query.
50
- """
51
- date_extractor, fed_chain = load_chains(api_key)
52
- logging.info('Extracting the date in numeric format..')
53
- date_response = date_extractor.run(query)
54
- if date_response != 'False':
55
- filter_date = json.loads(date_response)
56
-
57
- logging.info(f'Date parameters retrieved: {filter_date}')
58
- logging.info('Running the qa with filtered context..')
59
- filtered_context = search_with_filter(vs, query, init_k=200, step=300, target_k=7, filter_dict=filter_date)
60
-
61
- logging.info(20 * '-' + 'Metadata for the documents to be used' + 20 * '-')
62
- for doc in filtered_context:
63
- logging.info(doc.metadata)
64
- else:
65
- logging.info('No date elements found. Running the qa without filtering can output incorrect results.')
66
- filtered_context = vs.similarity_search(query, k=7)
67
- return fed_chain({'input_documents': filtered_context[:7], 'question': query})['output_text']
68
-
69
-
70
- if __name__ == '__main__':
71
- app = gr.Interface(fn=get_chain,
72
- inputs=[gr.Textbox(lines=2, placeholder="Enter your query", label='Your query'),
73
- gr.Textbox(lines=1, placeholder="Your OpenAI API key here", label='OpenAI Key')],
74
- description='Query the public database in FRED from 1936-2023',
75
- outputs=gr.Textbox(lines=1, label='Answer'),
76
- title='Chat with the FOMC meeting minutes',
77
- examples=[['What was the economic outlook from the staff presented in the meeting '
78
- 'of April 2009 with respect to labour market developments and industrial production?'],
79
- ['Who were the voting members present in the meeting on March 2010?'],
80
- ['How important was the pandemic of Covid-19 in the discussions during 2020?'],
81
- ['What was the impact of the oil crisis for the economic outlook during 1973?']],
82
- )
83
- app.launch()