chikZ commited on
Commit
dd2c942
·
verified ·
1 Parent(s): 894a066

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
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
+ from dotenv import load_dotenv
14
+
15
+ # Create Client
16
+ load_dotenv()
17
+
18
+ os.environ['OPENAI_API_KEY'] = anyscale_api_key
19
+
20
+ client = OpenAI(
21
+ base_url="https://api.endpoints.anyscale.com/v1",
22
+ api_key=os.environ['OPENAI_API_KEY']
23
+ )
24
+
25
+ # Define the embedding model and the vectorstore
26
+ embedding_model = SentenceTransformerEmbeddings(model_name='thenlper/gte-large')
27
+
28
+ # Load the persisted vectorDB
29
+ collection_name = 'picco-2023'
30
+
31
+ vectorstore_persisted = Chroma(
32
+ collection_name=collection_name,
33
+ embedding_function=embedding_model,
34
+ persist_directory='/content/picco_db'
35
+ )
36
+
37
+ retriever = vectorstore_persisted.as_retriever(
38
+ search_type="similarity",
39
+ search_kwargs={'k': 5},
40
+ )
41
+
42
+ # Prepare the logging functionality
43
+ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
44
+ log_folder = log_file.parent
45
+
46
+ scheduler = CommitScheduler(
47
+ repo_id="picco-qna",
48
+ repo_type="dataset",
49
+ folder_path=log_folder,
50
+ path_in_repo="data",
51
+ every=2
52
+
53
+ # Define the Q&A system message
54
+ qna_system_message = """
55
+ You are an academic assistant who answers questions regarding pulse integrated continuous cardiac output (PiCCO) for learners preparing for PiCCO workshop .
56
+ User input will have the context required by you to answer user questions.
57
+ This context will begin with the token: ###Context.
58
+ The context contains references to specific portions of a document relevant to the user query.
59
+
60
+ User questions will begin with the token: ###Question.
61
+
62
+ Please answer only using the context provided in the input. Do not mention anything about the context in your final answer.
63
+
64
+ If the answer is not found in the context, respond "I don't know".
65
+ """
66
+
67
+ # Define the user message template
68
+ qna_user_message_template = """
69
+ ###Context
70
+ Here are some documents that are relevant to the question mentioned below.-
71
+ {context}
72
+
73
+ ###Question
74
+ {question}
75
+ """
76
+
77
+ # Define the predict function that runs when 'Submit' is clicked or when an API request is made
78
+ def predict(user_input):
79
+ #filter = {"source": f"/content/dataset/{company}-10-k-2023.pdf"}
80
+ relevant_document_chunks = vectorstore_persisted.similarity_search(user_input, k=5)
81
+
82
+
83
+ # Create context_for_query
84
+ context_list = [f"Page {doc.metadata['page']}: {doc.page_content}" for doc in relevant_document_chunks]
85
+ context_for_query = ".".join(context_list)
86
+
87
+ # Create messages
88
+ prompt = [
89
+ {'role': 'system', 'content': qna_system_message},
90
+ {'role': 'user', 'content': qna_user_message_template.format(context=context_for_query, question=user_input)}
91
+ ]
92
+
93
+ # Get response from the LLM
94
+ try:
95
+ response = client.chat.completions.create(
96
+ model="mlabonne/NeuralHermes-2.5-Mistral-7B",
97
+ messages=prompt,
98
+ temperature=0
99
+ )
100
+ prediction = response.choices[0].message.content
101
+ except Exception as e:
102
+ prediction = f'Sorry, I encountered the following error: \n {e}'
103
+
104
+ print(prediction)
105
+
106
+ # Log both the inputs and outputs to a local log file
107
+ with scheduler.lock:
108
+ with log_file.open("a") as f:
109
+ f.write(json.dumps({
110
+ 'user_input': user_input,
111
+ 'retrieved_context': context_for_query,
112
+ 'model_response': prediction
113
+ }))
114
+ f.write("\n")
115
+
116
+ return prediction
117
+
118
+ # Set-up the Gradio UI
119
+
120
+ # Create the interface
121
+ demo = gr.Interface(
122
+ inputs=gr.Textbox(placeholder="Enter your query here"),
123
+ fn=predict,
124
+ outputs="text",
125
+ description="This web API presents an interface to ask questions on PiCCO Technology",
126
+ article="Note that questions that are not relevant to PiCCO not be answered.",
127
+ title="KSA/CCSK Hemodynamic Workshop Question & Answer Bot, by Chiko",
128
+ concurrency_limit=16,
129
+ examples = [
130
+ ["State and explain the main scientific principles used in PiCCO technology"],
131
+ ["What is the relevance of PiCCO in the operating room and critical care unit?"],
132
+ ["Name and explain the parameters that can be measured using PiCCO, give their reference ranges"],
133
+ ["State the basic equation that is used to estimate cardiac output using pulse contour analysis, explain each variable in detail"]
134
+
135
+ ]
136
+ )
137
+
138
+ demo.queue()
139
+ demo.launch()