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