Spaces:
Running
Running
Upload 5 files
Browse files- app.py +39 -0
- config.py +6 -0
- get_answer.py +63 -0
- logs.py +50 -0
- requirements.txt +18 -0
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
from functools import partial
|
4 |
+
from get_answer import get_answer
|
5 |
+
from logs import save_logs
|
6 |
+
import gdown
|
7 |
+
from config import folder_id, json_url_id
|
8 |
+
download_url = f'https://drive.google.com/uc?id={json_url_id}'
|
9 |
+
output = 'secret_google_service_account.json'
|
10 |
+
gdown.download(download_url, output, quiet=False)
|
11 |
+
def stream(query):
|
12 |
+
resp = get_answer(query)
|
13 |
+
answer = ""
|
14 |
+
for chunk in resp:
|
15 |
+
if chunk.choices[0].delta.content is not None:
|
16 |
+
answer = answer + chunk.choices[0].delta.content
|
17 |
+
yield answer
|
18 |
+
# save_logs(query, answer, folder_id=folder_id)
|
19 |
+
|
20 |
+
title = ""
|
21 |
+
with gr.Blocks(title=title,theme='nota-ai/theme',css="footer {visibility: hidden}") as demo:
|
22 |
+
gr.Markdown(f"## {title}")
|
23 |
+
|
24 |
+
with gr.Row():
|
25 |
+
with gr.Column(scale=6):
|
26 |
+
with gr.Row():
|
27 |
+
with gr.Column(scale=3):
|
28 |
+
chat_submit_button = gr.Button(value="Submit ▶")
|
29 |
+
url_input = gr.Textbox(placeholder="Age, medical results", lines=15, label="Input patient data")
|
30 |
+
with gr.Column(scale=6):
|
31 |
+
compliance_output = gr.Markdown("Waiting for link...")
|
32 |
+
|
33 |
+
|
34 |
+
fn_chat = stream
|
35 |
+
|
36 |
+
chat_submit_button.click(fn=fn_chat, inputs=[url_input], outputs=[compliance_output])
|
37 |
+
|
38 |
+
|
39 |
+
demo.launch(max_threads=40)
|
config.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
|
4 |
+
openai_api = os.environ("openai_api")
|
5 |
+
folder_id = os.environ("folder_id")
|
6 |
+
json_url_id = os.environ("json_url_id")
|
get_answer.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
from openai import OpenAI
|
4 |
+
import base64
|
5 |
+
|
6 |
+
from config import openai_api
|
7 |
+
|
8 |
+
def encode_image(image_path):
|
9 |
+
with open(image_path, "rb") as image_file:
|
10 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
11 |
+
|
12 |
+
|
13 |
+
client = OpenAI(api_key=openai_api)
|
14 |
+
def get_ai_response(prompt_content):
|
15 |
+
response = client.chat.completions.create(
|
16 |
+
model="gpt-4o",
|
17 |
+
messages=[
|
18 |
+
{
|
19 |
+
"role": "system",
|
20 |
+
"content": [
|
21 |
+
{
|
22 |
+
"type": "text",
|
23 |
+
"text": f"""You are an experimental AI-copilot for doctors. They will check your outputs
|
24 |
+
You will be given by the doctor patient data input and your role will be to determine the most probable diagnose.
|
25 |
+
You will include all relevant literature backup and references needed and a whole reasoning path of why you think it is.
|
26 |
+
Be very professional and redact as a health practioner.
|
27 |
+
|
28 |
+
You format each output with only:
|
29 |
+
|
30 |
+
# Probable diagnose: X
|
31 |
+
|
32 |
+
#### likelihood: XX%
|
33 |
+
|
34 |
+
# Suggested treatment:
|
35 |
+
-XX
|
36 |
+
- XX
|
37 |
+
|
38 |
+
# Full reasoning and path to diagnose (as a clean decision path with all relevant elements)
|
39 |
+
|
40 |
+
|
41 |
+
"""
|
42 |
+
}
|
43 |
+
]
|
44 |
+
},
|
45 |
+
{
|
46 |
+
"role": "user",
|
47 |
+
"content": prompt_content
|
48 |
+
},
|
49 |
+
],
|
50 |
+
temperature=1,
|
51 |
+
max_tokens=1439,
|
52 |
+
top_p=1,
|
53 |
+
frequency_penalty=0,
|
54 |
+
presence_penalty=0,
|
55 |
+
stream=True
|
56 |
+
)
|
57 |
+
return response
|
58 |
+
|
59 |
+
|
60 |
+
def get_answer(patient_data):
|
61 |
+
answer = get_ai_response(patient_data)
|
62 |
+
# answer = ""
|
63 |
+
return answer
|
logs.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from google.oauth2 import service_account
|
3 |
+
from googleapiclient.discovery import build
|
4 |
+
from googleapiclient.http import MediaFileUpload
|
5 |
+
from datetime import datetime
|
6 |
+
|
7 |
+
def save_logs(query,response, folder_id = ""):
|
8 |
+
to_save = f"LOG ENTRY\nQUERY\n{query}\n=================================\nRESPONSE\n{response}\n****************************************\n"
|
9 |
+
|
10 |
+
# Get the current date and time
|
11 |
+
now = datetime.now()
|
12 |
+
filename = str(now).replace(":","").replace(" ","").replace("-","").replace(".","")+".txt"
|
13 |
+
with open(filename, 'w') as file:
|
14 |
+
file.write(to_save)
|
15 |
+
# Path to the service account key file
|
16 |
+
SERVICE_ACCOUNT_FILE = 'secret_google_service_account.json'
|
17 |
+
|
18 |
+
# Define the required scopes
|
19 |
+
SCOPES = ['https://www.googleapis.com/auth/drive.file']
|
20 |
+
|
21 |
+
# Authenticate using the service account key file
|
22 |
+
credentials = service_account.Credentials.from_service_account_file(
|
23 |
+
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
|
24 |
+
|
25 |
+
# Build the Google Drive API client
|
26 |
+
service = build('drive', 'v3', credentials=credentials)
|
27 |
+
|
28 |
+
# Specify the folder ID where you want to upload the file
|
29 |
+
|
30 |
+
# Metadata of the file to be uploaded
|
31 |
+
file_metadata = {
|
32 |
+
'name': filename, # Name of the file to be uploaded
|
33 |
+
'parents': [folder_id] # Folder ID
|
34 |
+
}
|
35 |
+
|
36 |
+
# Path to the file you want to upload
|
37 |
+
file_path = filename
|
38 |
+
|
39 |
+
# Create a MediaFileUpload object to upload the file
|
40 |
+
media = MediaFileUpload(file_path, mimetype='text/plain')
|
41 |
+
|
42 |
+
# Use the Drive API to upload the file
|
43 |
+
file = service.files().create(
|
44 |
+
body=file_metadata,
|
45 |
+
media_body=media,
|
46 |
+
fields='id'
|
47 |
+
).execute()
|
48 |
+
|
49 |
+
# Print the file ID of the uploaded file
|
50 |
+
print('Saved in Google Drive - File ID: %s' % file.get('id'))
|
requirements.txt
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
bs4
|
3 |
+
openai
|
4 |
+
pandas
|
5 |
+
numpy
|
6 |
+
tabula-py
|
7 |
+
openai
|
8 |
+
gradio
|
9 |
+
pyPDF2
|
10 |
+
bs4
|
11 |
+
nltk
|
12 |
+
tiktoken
|
13 |
+
pdf2image
|
14 |
+
gdown
|
15 |
+
google-auth
|
16 |
+
google-auth-oauthlib
|
17 |
+
google-auth-httplib2
|
18 |
+
google-api-python-client
|