YoniFriedman commited on
Commit
f30f7fb
·
verified ·
1 Parent(s): 388abf2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["OPENAI_API_KEY"]
3
+
4
+ from llama_index import (
5
+ VectorStoreIndex,
6
+ SummaryIndex,
7
+ SimpleKeywordTableIndex,
8
+ SimpleDirectoryReader,
9
+ ServiceContext,
10
+ StorageContext,
11
+ load_index_from_storage
12
+ )
13
+ from llama_index.schema import IndexNode
14
+ from llama_index.tools import QueryEngineTool, ToolMetadata
15
+ from llama_index.llms import OpenAI
16
+
17
+ llm = OpenAI(temperature=0, model="gpt-3.5-turbo")
18
+ service_context = ServiceContext.from_defaults(llm=llm)
19
+
20
+ PERSIST_DIR = "arv_metadata"
21
+ storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
22
+ index = load_index_from_storage(storage_context)
23
+ query_engine = index.as_query_engine(similarity_top_k=3, llm=OpenAI(model="gpt-3.5-turbo"))
24
+
25
+
26
+ preamble = (" The person asking the following prompt is a person living with HIV in Kenya."
27
+ " For every response, recognize that they already have HIV and do not suggest that they have to get tested"
28
+ " for HIV or take post-exposure prophylaxis, as that is not relevant, though their partners perhaps should."
29
+ " Do not suggest anything that is not relevant to someone who already has HIV."
30
+ " They are asking questions through a mobile application called Nishauri"
31
+ " through which they can see their lab results, appointment histories, and upcoming appointments."
32
+ " Here is some information that is authoritative and should guide responses, when relevant."
33
+ " For questions about viral load, be sure to provide specific information"
34
+ " about cutoffs for viral load categories. Under 50 copies/ml is low detectable level,"
35
+ " 50 - 199 copies/ml is low level viremia, 200 - 999 is high level viremia, and "
36
+ " 1000 and above is suspected treatment failure."
37
+ " A high viral load or non-suppressed viral load is any viral load above 200 copies/ml."
38
+ " A suppressed viral load is one below 200 copies / ml."
39
+ " An established client is one who is on their current ART regimen for a period greater"
40
+ " than 6 months, had no active OI or in the previous 6 months, has adhered to scheduled"
41
+ " clinic visits for the previous 6 months and Viral load results has been less than 200 copies/ml"
42
+ " within the last 6 months."
43
+ " For questions about when patients should get their viral loads taken,"
44
+ " if they are newly initiated on ART, the first viral load sample should be taken after 3 months of"
45
+ " taking ART. Otherwise, if they are not new on ART, then if their previous result was below 50 to 199 cp/ml,"
46
+ " their viral load should be taken after every 12 months. If their previous result was above 200cp/ml,"
47
+ " then viral load sample should be taken after three months."
48
+ " Please answer the prompt using the information retrieved"
49
+ " and do not rely at all on your prior knowledge."
50
+ " Please keep your reply to no longer than three sentences, and please use simple language. ")
51
+
52
+ prompt_intro = (" Here is the prompt: ")
53
+
54
+ import gradio as gr
55
+
56
+ def nishauri(question: str, conversation_history: list[str]):
57
+
58
+
59
+ context = " ".join([item["user"] + " " + item["chatbot"] for item in conversation_history])
60
+ response = query_engine.query(preamble +
61
+ "the user previously asked and received the following: " +
62
+ context +
63
+ prompt_intro +
64
+ question)
65
+
66
+ conversation_history.append({"user": question, "chatbot": response.response})
67
+
68
+ source1 = ("File Name: " +
69
+ response.source_nodes[0].metadata["file_name"] +
70
+ "\nPage Number: " +
71
+ response.source_nodes[0].metadata["page_label"] +
72
+ "\n Source Text: " +
73
+ response.source_nodes[0].text)
74
+
75
+ source2 = ("File Name: " +
76
+ response.source_nodes[1].metadata["file_name"] +
77
+ "\nPage Number: " +
78
+ response.source_nodes[1].metadata["page_label"] +
79
+ "\n Source Text: " +
80
+ response.source_nodes[1].text)
81
+
82
+ source3 = ("File Name: " +
83
+ response.source_nodes[2].metadata["file_name"] +
84
+ "\nPage Number: " +
85
+ response.source_nodes[2].metadata["page_label"] +
86
+ "\n Source Text: " +
87
+ response.source_nodes[2].text)
88
+
89
+ return response, source1, source2, source3, conversation_history
90
+
91
+ inputs = [gr.Textbox(lines=10, label="Question"),
92
+ gr.State(value=[])]
93
+ outputs = [
94
+ gr.Textbox(label="Chatbot Response", type="text"),
95
+ gr.Textbox(label="Source 1", max_lines = 10, autoscroll = False, type="text"),
96
+ gr.Textbox(label="Source 2", max_lines = 10, autoscroll = False, type="text"),
97
+ gr.Textbox(label="Source 3", max_lines = 10, autoscroll = False, type="text"),
98
+ gr.State()
99
+ ]
100
+
101
+ gr.Interface(fn=nishauri, inputs=inputs, outputs=outputs, title="Nishauri Chatbot",
102
+ description="Enter a question and see the processed outputs in collapsible boxes.").launch()