pgurazada1 commited on
Commit
e6af5e0
·
verified ·
1 Parent(s): 49a0629

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
11
+ client = OpenAI(
12
+ base_url="https://api.endpoints.anyscale.com/v1",
13
+ api_key=os.environ['ANYSCALE_API_KEY']
14
+ )
15
+
16
+ embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-small')
17
+
18
+ tesla_10k_collection = 'tesla-10k-2019-to-2023'
19
+
20
+ vectorstore_persisted = Chroma(
21
+ collection_name=tesla_10k_collection,
22
+ persist_directory='./tesla_db',
23
+ embedding_function=embedding_model
24
+ )
25
+
26
+ retriever = vectorstore_persisted.as_retriever(
27
+ search_type='similarity',
28
+ search_kwargs={'k': 5}
29
+ )
30
+
31
+ qna_system_message = """
32
+ You are an assistant to a financial services firm who answers user queries on annual reports.
33
+ Users will ask questions delimited by triple backticks, that is, ```.
34
+ User input will have the context required by you to answer user questions.
35
+ This context will begin with the token: ###Context.
36
+ The context contains references to specific portions of a document relevant to the user query.
37
+ Please answer only using the context provided in the input. However, do not mention anything about the context in your answer.
38
+ If the answer is not found in the context, respond "I don't know".
39
+ """
40
+
41
+ qna_user_message_template = """
42
+ ###Context
43
+ Here are some documents that are relevant to the question.
44
+ {context}
45
+ ```
46
+ {question}
47
+ ```
48
+ """
49
+
50
+ def predict(user_input):
51
+
52
+ relevant_document_chunks = retriever.get_relevant_documents(user_input)
53
+ context_list = [d.page_content for d in relevant_document_chunks]
54
+ context_for_query = ".".join(context_list)
55
+
56
+ prompt = [
57
+ {'role':'system', 'content': qna_system_message},
58
+ {'role': 'user', 'content': qna_user_message_template.format(
59
+ context=context_for_query,
60
+ question=user_input
61
+ )
62
+ }
63
+ ]
64
+
65
+ try:
66
+ response = client.chat.completions.create(
67
+ model="mistralai/Mistral-7B-Instruct-v0.1",
68
+ messages=prompt,
69
+ temperature=0
70
+ )
71
+
72
+ prediction = response.choices[0].message.content
73
+
74
+ except Exception as e:
75
+ prediction = e
76
+
77
+ return prediction
78
+
79
+
80
+ textbox = gr.Textbox(placeholder="Enter your query here", lines=6)
81
+
82
+ interface = gr.Interface(
83
+ inputs=textbox, fn=predict, outputs="text",
84
+ title="AMA on Tesla 10-K statements",
85
+ description="This web API presents an interface to ask questions on contents of the Tesla 10-K reports for the period 2019 - 2023.",
86
+ article="Note that questions that are not relevant to the Tesla 10-K report will not be answered.",
87
+ examples=[["What was the total revenue of the company in 2022?", "$ 81.46 Billion"],
88
+ ["Summarize the Management Discussion and Analysis section of the 2021 report in 50 words.", ""],
89
+ ["What was the company's debt level in 2020?", ""],
90
+ ["Identify 5 key risks identified in the 2019 10k report? Respond with bullet point summaries.", ""]
91
+ ]
92
+ )
93
+
94
+ with gr.Blocks() as demo:
95
+ interface.launch()
96
+
97
+ demo.queue(concurrency_count=16)
98
+ demo.launch()