ganesh0001 commited on
Commit
41a54f4
1 Parent(s): 8e441fb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+ import json
4
+ import re
5
+
6
+ #API Keys
7
+ os.environ['OPENAI_API_TOKEN'] = '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'
8
+ openai.api_key = os.environ['OPENAI_API_TOKEN']
9
+
10
+ import os
11
+ import io
12
+ import IPython.display
13
+ from PIL import Image
14
+ import base64
15
+ import requests, json
16
+ import gradio as gr
17
+ requests.adapters.DEFAULT_TIMEOUT = 60
18
+
19
+ def chat(system_prompt, user_prompt, model = 'gpt-3.5-turbo', temperature = 0, verbose = False):
20
+ ''' Normal call of OpenAI API '''
21
+ response = openai.ChatCompletion.create(
22
+ temperature = temperature,
23
+ model=model,
24
+ messages=[
25
+ {"role": "system", "content": system_prompt},
26
+ {"role": "user", "content": user_prompt}
27
+ ])
28
+
29
+ res = response['choices'][0]['message']['content']
30
+
31
+ if verbose:
32
+ print('System prompt:', system_prompt)
33
+ print('User prompt:', user_prompt)
34
+ print('GPT response:', res)
35
+
36
+ return res
37
+
38
+ def format_chat_prompt(message, chat_history, max_convo_length):
39
+ prompt = ""
40
+ for turn in chat_history[-max_convo_length:]:
41
+ user_message, bot_message = turn
42
+ prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
43
+ prompt = f"{prompt}\nUser: {message}\nAssistant:"
44
+ return prompt
45
+
46
+ def respond(message, chat_history, max_convo_length = 10):
47
+ formatted_prompt = format_chat_prompt(message, chat_history, max_convo_length)
48
+ print('Prompt + Context:')
49
+ print(formatted_prompt)
50
+ bot_message = chat(system_prompt = 'You are a friendly chatbot. Generate the output for only the Assistant.',
51
+ user_prompt = formatted_prompt)
52
+ chat_history.append((message, bot_message))
53
+ return "", chat_history
54
+
55
+ with gr.Blocks() as demo:
56
+ chatbot = gr.Chatbot(height=300) #just to fit the notebook
57
+ msg = gr.Textbox(label="Prompt")
58
+ btn = gr.Button("Submit")
59
+ clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
60
+
61
+ btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
62
+ msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit
63
+ gr.close_all()
64
+
65
+ context = '''Background
66
+ Higher resistance rates of > 20% have been noted in Enterobacteriaceae urinary isolates towards ciprofloxacin and co-trimoxazole (C + C) in Singapore, compared with amoxicillin-clavulanate and nitrofurantoin (AC + N). This study examined if treatment failure varied between different antibiotics, given different resistant rates, for uncomplicated urinary tract infections (UTIs) managed in primary care. We also aimed to identify gaps for improvement in diagnosis, investigations, and management.
67
+
68
+ Methods
69
+ A retrospective cohort study was conducted from 2019 to 2021 on female patients aged 18–50 with uncomplicated UTIs at 6 primary care clinics in Singapore. ORENUC classification was used to exclude complicated UTIs. Patients with uncomplicated UTIs empirically treated with amoxicillin-clavulanate, nitrofurantoin, ciprofloxacin or co-trimoxazole were followed-up for 28 days. Treatment failure was defined as re-attendance for symptoms and antibiotic re-prescription, or hospitalisation for UTI complications. After 2:1 propensity score matching in each group, modified Poisson regression and Cox proportional hazard regression accounting for matched data were used to determine risk and time to treatment failure.
70
+
71
+ Results
72
+ 3194 of 4253 (75.1%) UTIs seen were uncomplicated, of which only 26% were diagnosed clinically. Urine cultures were conducted for 1094 (34.3%) uncomplicated UTIs, of which only 410 (37.5%) had bacterial growth. The most common organism found to cause uncomplicated UTIs was Escherichia coli (64.6%), with 92.6% and 99.4% of isolates sensitive to amoxicillin-clavulanate and nitrofurantoin respectively. Treatment failure occurred in 146 patients (4.57%). Among 1894 patients treated with AC + N matched to 947 patients treated with C + C, patients treated with C + C were 50% more likely to fail treatment (RR 1.49, 95% CI 1.10–2.01), with significantly higher risk of experiencing shorter time to failure (HR 1.61, 95% CI 1.12–2.33), compared to patients treated with AC + N.
73
+
74
+ Conclusion
75
+ Treatment failure rate was lower for antibiotics with lower reported resistance rates (AC + N). We recommend treating uncomplicated UTIs in Singapore with amoxicillin-clavulanate or nitrofurantoin, based on current local antibiograms. Diagnosis, investigations and management of UTIs remained sub-optimal. Future studies should be based on updating antibiograms, highlighting its importance in guideline development.'''
76
+
77
+ def format_chat_prompt(message, chat_history, max_convo_length):
78
+ prompt = ""
79
+ for turn in chat_history[-max_convo_length:]:
80
+ user_message, bot_message = turn
81
+ prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
82
+ prompt = f"{prompt}\nUser: {message}\nAssistant:"
83
+ return prompt
84
+
85
+ def respond(message, chat_history, max_convo_length = 10):
86
+ formatted_prompt = format_chat_prompt(message, chat_history, max_convo_length)
87
+ print('Prompt + Context:')
88
+ print(formatted_prompt)
89
+ bot_message = chat(system_prompt = f'''You are a friendly chatbot. Generate the output for only the Assistant.
90
+ Only answer based on the context. If unsure, output "I don't know". Context: {context}"''',
91
+ user_prompt = formatted_prompt)
92
+ chat_history.append((message, bot_message))
93
+ return "", chat_history
94
+
95
+ with gr.Blocks() as demo:
96
+ chatbot = gr.Chatbot(height=300) #just to fit the notebook
97
+ msg = gr.Textbox(label="Prompt")
98
+ btn = gr.Button("Submit")
99
+ clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
100
+
101
+ btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
102
+ msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot]) #Press enter to submit
103
+ gr.close_all()
104
+ demo.launch(share=True)