chikZ commited on
Commit
2123453
·
verified ·
1 Parent(s): 0a41b96

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -165
app.py DELETED
@@ -1,165 +0,0 @@
1
- ## Setup
2
- # Import the necessary Libraries
3
- import os
4
- import uuid
5
- import json
6
- import gradio as gr
7
- from dotenv import load_dotenv
8
-
9
-
10
-
11
- from openai import OpenAI
12
- from langchain_community.embeddings.sentence_transformer import (SentenceTransformerEmbeddings)
13
- from langchain_community.vectorstores import Chroma
14
-
15
- from huggingface_hub import CommitScheduler
16
- from pathlib import Path
17
-
18
-
19
- # Create Client
20
- load_dotenv()
21
-
22
- os.environ("ANYSCALE_API_KEY") = os.getenv("ANYSCALE_API_KEY")
23
- client = OpenAI(
24
- base_url="https://api.endpoints.anyscale.com/v1",
25
- api_key=os.environ("ANYSCALE_API_KEY")
26
- )
27
-
28
-
29
- # Define the embedding model and the vectorstore
30
- embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
31
-
32
- # Load the persisted vectorDB
33
-
34
- collection_name = 'finsights-grey-10k-2023'
35
-
36
- vectorstore_persisted = Chroma(
37
- collection_name=collection_name,
38
- embedding_function=embedding_model,
39
- persist_directory='/content/finsights23_db'
40
- )
41
-
42
- retriever = vectorstore_persisted.as_retriever(
43
- search_type="similarity",
44
- search_kwargs={'k': 5},
45
- )
46
-
47
- # Prepare the logging functionality
48
-
49
- log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
50
- log_folder = log_file.parent
51
-
52
- scheduler = CommitScheduler(
53
- repo_id="finsights-qna-logs",
54
- repo_type="dataset",
55
- folder_path=log_folder,
56
- path_in_repo="data",
57
- every=2
58
- )
59
-
60
- # Define the Q&A system message
61
- qna_system_message = """
62
- You are an assistant to a financial technology firm who answers user queries on 10-K reports from various indusrty players which contain detailed information about financial performance, risk factors, market trends, and strategic initiatives.
63
- User input will have the context required by you to answer user questions.
64
- This context will begin with the token: ###Context.
65
- The context contains references to specific portions of a document relevant to the user query.
66
-
67
- User questions will begin with the token: ###Question.
68
-
69
- Please answer only using the context provided in the input. Do not mention anything about the context in your final answer.
70
-
71
- If the answer is not found in the context, respond "I don't know".
72
- """
73
-
74
- # Define the user message template
75
- qna_user_message_template = """
76
- ###Context
77
- Here are some documents that are relevant to the question mentioned below.
78
- {context}
79
-
80
- ###Question
81
- {question}
82
- """
83
-
84
-
85
-
86
- # Define the predict function that runs when 'Submit' is clicked or when a API request is made
87
- def predict(user_input,company):
88
-
89
- filter = "dataset/"+company+"-10-k-2023.pdf"
90
- relevant_document_chunks = retriever.invoke(user_input, filter=filter)
91
- context_list = [f"Page {doc.metadata['page']}: {doc.page_content}" for doc in docs]
92
-
93
-
94
- # Create context_for_query
95
- context_for_query = ".".join(context_list)
96
-
97
-
98
-
99
- # Create messages
100
- prompt = [
101
- {'role':'system','content': qna_system_message},
102
- {'role':'user', 'content': qna_user_message_template.format(context=context_for_query,
103
- question=user_input
104
- )
105
- }
106
- ]
107
-
108
-
109
- # Get response from the LLM
110
- try:
111
- response = client.chat.completions.create(
112
- model="mlabonne/NeuralHermes-2.5-Mistral-7B",
113
- messages=prompt,
114
- temperature=0
115
- )
116
-
117
- prediction = response.choices[0].message.content
118
- except Exception as e:
119
- prediction = f'Sorry, I encountered the following error: \n {e}'
120
-
121
- print(prediction)
122
-
123
-
124
- # While the prediction is made, log both the inputs and outputs to a local log file
125
- # While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
126
- # access
127
-
128
- with scheduler.lock:
129
- with log_file.open("a") as f:
130
- f.write(json.dumps(
131
- {
132
- 'user_input': user_input,
133
- 'retrieved_context': context_for_query,
134
- 'model_response': prediction
135
- }
136
- ))
137
- f.write("\n")
138
-
139
- return prediction
140
-
141
- # Set-up the Gradio UI
142
- # Add text box and radio button to the interface
143
- # The radio button is used to select the company 10k report in which the context needs to be retrieved.
144
-
145
- textbox = gr.Textbox(placeholder = "Enter your query here")
146
- company = gr.Radio(choices=["IBM", "META", "aws", "google", "msft"], label="Company")
147
-
148
- # Create the interface
149
- # For the inputs parameter of Interface provide [textbox,company]
150
- demo = gr.Interface(
151
- inputs = [textbox,company],
152
- fn = predict,
153
- outputs = "text",
154
- description="This web API presents an interface to ask questions on contents of of IBM, META, AWS, GOOGLE and MSFT 10-K reports for the year 2023",
155
- article= "Note that questions that are not relevant to the aforermentioned companies' 10-K reports will not be answered",
156
- title = "Q&A for IBM, META, AWS, GOOG & MSFT 10-K Statements",
157
- examples = [["Has the company made any significant acquisitions in the AI space, and how are these acquisitions being integrated into the company's strategy?", ""],
158
- ["How much capital has been allocated towards AI research and development?", ""],
159
- ["What initiatives has the company implemented to address ethical concerns sorrounding AI, such as faireness, accountability, and privacy?", ""],
160
- ["How does the company plan to differentiate itself in the AI space relave to competitors?", ""]],
161
- concurrency_limit=16
162
- )
163
-
164
- demo.queue()
165
- demo.launch(share=True, debug =False)