Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,131 +1,93 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
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 |
-
prediction = e
|
95 |
-
|
96 |
-
# While the prediction is made, log both the inputs and outputs to a local log file
|
97 |
-
# While writing to the log file, ensure that the commit scheduler is locked to avoid parallel
|
98 |
-
# access
|
99 |
-
|
100 |
-
with scheduler.lock:
|
101 |
-
with log_file.open("a") as f:
|
102 |
-
f.write(json.dumps(
|
103 |
-
{
|
104 |
-
'user_input': user_input,
|
105 |
-
'retrieved_context': context_for_query,
|
106 |
-
'model_response': prediction
|
107 |
-
}
|
108 |
-
))
|
109 |
-
f.write("\n")
|
110 |
-
|
111 |
-
return prediction
|
112 |
-
|
113 |
-
|
114 |
-
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
|
115 |
-
|
116 |
-
# Create the interface
|
117 |
-
demo = gr.Interface(
|
118 |
-
inputs=textbox, fn=predict, outputs="text",
|
119 |
-
title="AMA on Tesla 10-K statements",
|
120 |
-
description="This web API presents an interface to ask questions on contents of the Tesla 10-K reports for the period 2019 - 2023.",
|
121 |
-
article="Note that questions that are not relevant to the Tesla 10-K report will not be answered.",
|
122 |
-
examples=[["What was the total revenue of the company in 2022?", "$ 81.46 Billion"],
|
123 |
-
["Summarize the Management Discussion and Analysis section of the 2021 report in 50 words.", ""],
|
124 |
-
["What was the company's debt level in 2020?", ""],
|
125 |
-
["Identify 5 key risks identified in the 2019 10k report? Respond with bullet point summaries.", ""]
|
126 |
-
],
|
127 |
-
concurrency_limit=16
|
128 |
-
)
|
129 |
-
|
130 |
-
demo.queue()
|
131 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
8 |
+
from langchain_community.vectorstores import Chroma
|
9 |
+
|
10 |
+
client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
|
11 |
+
|
12 |
+
embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-small')
|
13 |
+
|
14 |
+
tesla_10k_collection = 'tesla-10k-2019-to-2023'
|
15 |
+
|
16 |
+
vectorstore_persisted = Chroma(
|
17 |
+
collection_name=tesla_10k_collection,
|
18 |
+
persist_directory='./tesla_db',
|
19 |
+
embedding_function=embedding_model
|
20 |
+
)
|
21 |
+
|
22 |
+
retriever = vectorstore_persisted.as_retriever(
|
23 |
+
search_type='similarity',
|
24 |
+
search_kwargs={'k': 5}
|
25 |
+
)
|
26 |
+
|
27 |
+
qna_system_message = """
|
28 |
+
You are an assistant to a financial services firm who answers user queries on annual reports.
|
29 |
+
Users will ask questions delimited by triple backticks, that is, ```.
|
30 |
+
User input will have the context required by you to answer user questions.
|
31 |
+
This context will begin with the token: ###Context.
|
32 |
+
The context contains references to specific portions of a document relevant to the user query.
|
33 |
+
Please answer only using the context provided in the input. However, do not mention anything about the context in your answer.
|
34 |
+
If the answer is not found in the context, respond "I don't know".
|
35 |
+
"""
|
36 |
+
|
37 |
+
qna_user_message_template = """
|
38 |
+
###Context
|
39 |
+
Here are some documents that are relevant to the question.
|
40 |
+
{context}
|
41 |
+
```
|
42 |
+
{question}
|
43 |
+
```
|
44 |
+
"""
|
45 |
+
|
46 |
+
def predict(user_input):
|
47 |
+
|
48 |
+
relevant_document_chunks = retriever.get_relevant_documents(user_input)
|
49 |
+
context_list = [d.page_content for d in relevant_document_chunks]
|
50 |
+
context_for_query = ".".join(context_list)
|
51 |
+
|
52 |
+
prompt = [
|
53 |
+
{'role':'system', 'content': qna_system_message},
|
54 |
+
{'role': 'user', 'content': qna_user_message_template.format(
|
55 |
+
context=context_for_query,
|
56 |
+
question=user_input
|
57 |
+
)
|
58 |
+
}
|
59 |
+
]
|
60 |
+
|
61 |
+
try:
|
62 |
+
response = client.chat.completions.create(
|
63 |
+
model="gpt-3.5-turbo",
|
64 |
+
messages=prompt,
|
65 |
+
temperature=0
|
66 |
+
)
|
67 |
+
|
68 |
+
prediction = response.choices[0].message.content
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
prediction = e
|
72 |
+
|
73 |
+
return prediction
|
74 |
+
|
75 |
+
|
76 |
+
textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
|
77 |
+
|
78 |
+
demo = gr.Interface(
|
79 |
+
inputs=textbox, fn=predict, outputs="text",
|
80 |
+
title="AMA on Tesla 10-K statements",
|
81 |
+
description="This web API presents an interface to ask questions on contents of the Tesla 10-K reports for the period 2019 - 2023.",
|
82 |
+
article="Note that questions that are not relevant to the Tesla 10-K report will not be answered.",
|
83 |
+
examples=[["What was the total revenue of the company in 2022?", "$ 81.46 Billion"],
|
84 |
+
["Summarize the Management Discussion and Analysis section of the 2021 report in 50 words.", ""],
|
85 |
+
["What was the company's debt level in 2020?", ""],
|
86 |
+
["Identify 5 key risks identified in the 2019 10k report? Respond with bullet point summaries.", ""]
|
87 |
+
],
|
88 |
+
concurrency_limit=16
|
89 |
+
)
|
90 |
+
|
91 |
+
|
92 |
+
demo.queue()
|
93 |
+
demo.launch(auth=("demouser", os.getenv('PASSWD')))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|