Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,159 @@
|
|
1 |
-
# Import the necessary Libraries
|
2 |
-
import json
|
3 |
-
import uuid
|
4 |
-
import os
|
5 |
-
|
6 |
-
from openai import OpenAI
|
7 |
-
import gradio as gr
|
8 |
-
|
9 |
-
from langchain_community.embeddings.sentence_transformer import (
|
10 |
-
SentenceTransformerEmbeddings
|
11 |
-
)
|
12 |
-
from langchain_community.vectorstores import Chroma
|
13 |
-
from huggingface_hub import CommitScheduler
|
14 |
-
from pathlib import Path
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
demo.launch()
|
|
|
1 |
+
# Import the necessary Libraries
|
2 |
+
import json
|
3 |
+
import uuid
|
4 |
+
import os
|
5 |
+
|
6 |
+
from openai import OpenAI
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
from langchain_community.embeddings.sentence_transformer import (
|
10 |
+
SentenceTransformerEmbeddings
|
11 |
+
)
|
12 |
+
from langchain_community.vectorstores import Chroma
|
13 |
+
from huggingface_hub import CommitScheduler
|
14 |
+
from pathlib import Path
|
15 |
+
|
16 |
+
|
17 |
+
import os
|
18 |
+
|
19 |
+
os.environ['OPENAI_API_KEY'] = "gl-U2FsdGVkX18e2Pmna5tn6g6u7mqi55sN7xcOMntKGypQnR3Y4CQK5VfbJYc0Nt7c"
|
20 |
+
|
21 |
+
os.environ["OPENAI_BASE_URL"] = "https://aibe.mygreatlearning.com/openai/v1"
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
# Create Client
|
30 |
+
|
31 |
+
client = OpenAI()
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
model_name = 'gpt-4o-mini'
|
37 |
+
|
38 |
+
embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
|
39 |
+
|
40 |
+
streamlit_collection = 'reports_collection'
|
41 |
+
|
42 |
+
vectorstore_persisted = Chroma(
|
43 |
+
collection_name=streamlit_collection,
|
44 |
+
persist_directory='./reports_db',
|
45 |
+
embedding_function=embedding_model
|
46 |
+
)
|
47 |
+
|
48 |
+
# Prepare the logging functionality
|
49 |
+
|
50 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
51 |
+
log_folder = log_file.parent
|
52 |
+
|
53 |
+
scheduler = CommitScheduler(
|
54 |
+
repo_id="reports-qna",
|
55 |
+
repo_type="dataset",
|
56 |
+
folder_path=log_folder,
|
57 |
+
path_in_repo="data",
|
58 |
+
every=2
|
59 |
+
)
|
60 |
+
|
61 |
+
qna_system_message = """
|
62 |
+
You are an assistant to a Financial Analyst. Your task is to summarize and provide relevant information to the financial analyst's question based on the provided context.
|
63 |
+
|
64 |
+
User input will include the necessary context for you to answer their questions. This context will begin with the token: ###Context.
|
65 |
+
The context contains references to specific portions of documents relevant to the user's query, along with page number from the report.
|
66 |
+
The source for the context will begin with the token ###Page
|
67 |
+
|
68 |
+
When crafting your response:
|
69 |
+
1. Select only context relevant to answer the question.
|
70 |
+
2. Include the source links in your response.
|
71 |
+
3. User questions will begin with the token: ###Question.
|
72 |
+
4. If the question is irrelevant or if you do not have the information to respond with - "Sorry, this is out of my knowledge base"
|
73 |
+
|
74 |
+
Please adhere to the following guidelines:
|
75 |
+
- Your response should only be about the question asked and nothing else.
|
76 |
+
- Answer only using the context provided.
|
77 |
+
- Do not mention anything about the context in your final answer.
|
78 |
+
- If the answer is not found in the context, it is very very important for you to respond with "Sorry, this is out of my knowledge base"
|
79 |
+
- Always quote the page number when you use the context. Cite the relevant page number at the end of your response under the section - Page:
|
80 |
+
- Do not make up sources Use the links provided in the sources section of the context and nothing else. You are prohibited from providing other links/sources.
|
81 |
+
|
82 |
+
Here is an example of how to structure your response:
|
83 |
+
|
84 |
+
Answer:
|
85 |
+
[Answer]
|
86 |
+
|
87 |
+
Page:
|
88 |
+
[Page number]
|
89 |
+
"""
|
90 |
+
|
91 |
+
qna_user_message_template = """
|
92 |
+
###Context
|
93 |
+
Here are some documents and their page number that are relevant to the question mentioned below.
|
94 |
+
{context}
|
95 |
+
|
96 |
+
###Question
|
97 |
+
{question}
|
98 |
+
"""
|
99 |
+
|
100 |
+
# Define the predict function that runs when 'Submit' is clicked or when a API request is made
|
101 |
+
def predict(user_input,company):
|
102 |
+
|
103 |
+
filter = "dataset/"+company+"-10-k-2023.pdf"
|
104 |
+
relevant_document_chunks = vectorstore_persisted.similarity_search(user_input, k=5, filter={"source":filter})
|
105 |
+
context_list = [d.page_content + "\n ###Page: " + str(d.metadata['page']) + "\n\n " for d in relevant_document_chunks]
|
106 |
+
context_for_query = ".".join(context_list)
|
107 |
+
|
108 |
+
prompt = [
|
109 |
+
{'role':'system', 'content': qna_system_message},
|
110 |
+
{'role': 'user', 'content': qna_user_message_template.format(
|
111 |
+
context=context_for_query,
|
112 |
+
question=user_input
|
113 |
+
)
|
114 |
+
}
|
115 |
+
]
|
116 |
+
|
117 |
+
try:
|
118 |
+
response = client.chat.completions.create(
|
119 |
+
model='mistralai/Mixtral-8x7B-Instruct-v0.1',
|
120 |
+
messages=prompt,
|
121 |
+
temperature=0
|
122 |
+
)
|
123 |
+
|
124 |
+
prediction = response.choices[0].message.content
|
125 |
+
|
126 |
+
except Exception as e:
|
127 |
+
prediction = e
|
128 |
+
|
129 |
+
# While the prediction is made, log both the inputs and outputs to a local log file
|
130 |
+
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
131 |
+
# access
|
132 |
+
|
133 |
+
with scheduler.lock:
|
134 |
+
with log_file.open("a") as f:
|
135 |
+
f.write(json.dumps(
|
136 |
+
{
|
137 |
+
'user_input': user_input,
|
138 |
+
'retrieved_context': context_for_query,
|
139 |
+
'model_response': prediction
|
140 |
+
}
|
141 |
+
))
|
142 |
+
f.write("\n")
|
143 |
+
|
144 |
+
return prediction
|
145 |
+
|
146 |
+
|
147 |
+
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
|
148 |
+
company = gr.Radio(choices=["google", "msft", "aws", "ibm", "meta"], label="Select the company")
|
149 |
+
|
150 |
+
# Create the interface
|
151 |
+
demo = gr.Interface(
|
152 |
+
inputs=[textbox, company], fn=predict, outputs="text",
|
153 |
+
title="10-k Reports Q&A System",
|
154 |
+
description="This web API presents an interface to ask questions on 10-k reports ",
|
155 |
+
concurrency_limit=16
|
156 |
+
)
|
157 |
+
|
158 |
+
demo.queue()
|
159 |
demo.launch()
|