Spaces:
Sleeping
Sleeping
Upload 10 files
Browse files- .gitattributes +36 -35
- README.md +12 -14
- app.py +131 -0
- requirements.txt +5 -0
- tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/data_level0.bin +3 -0
- tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/header.bin +3 -0
- tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/index_metadata.pickle +3 -0
- tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/length.bin +3 -0
- tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/link_lists.bin +3 -0
- tesla_db/chroma.sqlite3 +3 -0
.gitattributes
CHANGED
@@ -1,35 +1,36 @@
|
|
1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
-
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
-
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
-
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
tesla_db/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,14 +1,12 @@
|
|
1 |
-
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
+
---
|
2 |
+
title: Document Qna Chroma Anyscale
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: blue
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 4.29.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
---
|
11 |
+
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
import json
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
from openai import OpenAI
|
8 |
+
|
9 |
+
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
10 |
+
from langchain_community.vectorstores import Chroma
|
11 |
+
|
12 |
+
from huggingface_hub import CommitScheduler
|
13 |
+
from pathlib import Path
|
14 |
+
|
15 |
+
|
16 |
+
client = OpenAI(
|
17 |
+
base_url="https://aibe.mygreatlearning.com/openai/v1",
|
18 |
+
api_key="gl-U2FsdGVkX1+v7Xh6OC7frJLcnSHSLr4I52JfcsVBUa7pkZ/t+J9GOkUbaUnqFnnZ"
|
19 |
+
)
|
20 |
+
|
21 |
+
embedding_model = SentenceTransformerEmbeddings(model_name='gpt=thenlper/gte-small')
|
22 |
+
|
23 |
+
tesla_10k_collection = 'tesla-10k-2019-to-2023'
|
24 |
+
|
25 |
+
vectorstore_persisted = Chroma(
|
26 |
+
collection_name=tesla_10k_collection,
|
27 |
+
persist_directory='./tesla_db',
|
28 |
+
embedding_function=embedding_model
|
29 |
+
)
|
30 |
+
|
31 |
+
retriever = vectorstore_persisted.as_retriever(
|
32 |
+
search_type='similarity',
|
33 |
+
search_kwargs={'k': 5}
|
34 |
+
)
|
35 |
+
|
36 |
+
# Prepare the logging functionality
|
37 |
+
|
38 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
39 |
+
log_folder = log_file.parent
|
40 |
+
|
41 |
+
scheduler = CommitScheduler(
|
42 |
+
repo_id="document-qna-chroma-anyscale-logs",
|
43 |
+
repo_type="dataset",
|
44 |
+
folder_path=log_folder,
|
45 |
+
path_in_repo="data",
|
46 |
+
every=2
|
47 |
+
)
|
48 |
+
|
49 |
+
qna_system_message = """
|
50 |
+
You are an assistant to a financial services firm who answers user queries on annual reports.
|
51 |
+
Users will ask questions delimited by triple backticks, that is, ```.
|
52 |
+
User input will have the context required by you to answer user questions.
|
53 |
+
This context will begin with the token: ###Context.
|
54 |
+
The context contains references to specific portions of a document relevant to the user query.
|
55 |
+
Please answer only using the context provided in the input. However, do not mention anything about the context in your answer.
|
56 |
+
If the answer is not found in the context, respond "I don't know".
|
57 |
+
"""
|
58 |
+
|
59 |
+
qna_user_message_template = """
|
60 |
+
###Context
|
61 |
+
Here are some documents that are relevant to the question.
|
62 |
+
{context}
|
63 |
+
```
|
64 |
+
{question}
|
65 |
+
```
|
66 |
+
"""
|
67 |
+
|
68 |
+
# Define the predict function that runs when 'Submit' is clicked or when a API request is made
|
69 |
+
def predict(user_input):
|
70 |
+
|
71 |
+
relevant_document_chunks = retriever.invoke(user_input)
|
72 |
+
context_list = [d.page_content for d in relevant_document_chunks]
|
73 |
+
context_for_query = ".".join(context_list)
|
74 |
+
|
75 |
+
prompt = [
|
76 |
+
{'role':'system', 'content': qna_system_message},
|
77 |
+
{'role': 'user', 'content': qna_user_message_template.format(
|
78 |
+
context=context_for_query,
|
79 |
+
question=user_input
|
80 |
+
)
|
81 |
+
}
|
82 |
+
]
|
83 |
+
|
84 |
+
try:
|
85 |
+
response = client.chat.completions.create(
|
86 |
+
model='mlabonne/NeuralHermes-2.5-Mistral-7B',
|
87 |
+
messages=prompt,
|
88 |
+
temperature=0
|
89 |
+
)
|
90 |
+
|
91 |
+
prediction = response.choices[0].message.content
|
92 |
+
|
93 |
+
except Exception as e:
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai==1.23.2
|
2 |
+
chromadb==0.4.22
|
3 |
+
langchain==0.1.9
|
4 |
+
langchain-community==0.0.32
|
5 |
+
sentence-transformers==2.3.1
|
tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/data_level0.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:27662eb029bbc3d09e4f17de86b0ec8d081222e7f0c2bf8b38c0f76588eb2878
|
3 |
+
size 5028000
|
tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/header.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ed67034369667b449e6a14ff788b4f792483e794a2ef335429a48cf9bec3a897
|
3 |
+
size 100
|
tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/index_metadata.pickle
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:708978bce9accecfb0aca5a97e8ab92a9e3237aacac297c5e164da3a17f8802e
|
3 |
+
size 172004
|
tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/length.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d247935870a99da0b73b5ff4fd66239d718bf01b76581c25351a875c7740c79e
|
3 |
+
size 12000
|
tesla_db/908b9485-d351-4c65-93e1-a9a76f864b14/link_lists.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:08c8d1f08527c0f21b5814da5dad0aa7f1ba98cab0738289e9d92a564c00cc2f
|
3 |
+
size 25736
|
tesla_db/chroma.sqlite3
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c00be5a240a3470d8494233a24d6881d28bc2f2fbe900c5a82b5ef2c20bd26fb
|
3 |
+
size 36327424
|