Spaces:
Sleeping
Sleeping
initial commit
Browse files- .env +0 -0
- .gitignore +54 -0
- app.py +191 -0
- send_email.py +65 -0
.env
ADDED
|
File without changes
|
.gitignore
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# These are some examples of commonly ignored file patterns.
|
| 2 |
+
# You should customize this list as applicable to your project.
|
| 3 |
+
# Learn more about .gitignore:
|
| 4 |
+
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore
|
| 5 |
+
|
| 6 |
+
# Node artifact files
|
| 7 |
+
node_modules/
|
| 8 |
+
dist/
|
| 9 |
+
|
| 10 |
+
# Compiled Java class files
|
| 11 |
+
*.class
|
| 12 |
+
|
| 13 |
+
# Compiled Python bytecode
|
| 14 |
+
*.py[cod]
|
| 15 |
+
|
| 16 |
+
# Log files
|
| 17 |
+
*.log
|
| 18 |
+
|
| 19 |
+
# Package files
|
| 20 |
+
*.jar
|
| 21 |
+
|
| 22 |
+
# Maven
|
| 23 |
+
target/
|
| 24 |
+
|
| 25 |
+
# JetBrains IDE
|
| 26 |
+
.idea/
|
| 27 |
+
|
| 28 |
+
# Unit test reports
|
| 29 |
+
TEST*.xml
|
| 30 |
+
|
| 31 |
+
# Generated by MacOS
|
| 32 |
+
.DS_Store
|
| 33 |
+
|
| 34 |
+
# Generated by Windows
|
| 35 |
+
Thumbs.db
|
| 36 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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from send_email import send_email
|
| 5 |
+
import os
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def request_email_form(req_email, req_reason, req_message):
|
| 10 |
+
# Check if all required fields are filled and the conditions are met
|
| 11 |
+
if not req_email or not req_reason or not req_message:
|
| 12 |
+
return "**Please fill in all required fields.**"
|
| 13 |
+
if "@" not in req_email or "." not in req_email:
|
| 14 |
+
return "**Please enter a valid email.**"
|
| 15 |
+
|
| 16 |
+
send_email(req_reason, req_email, req_message)
|
| 17 |
+
|
| 18 |
+
return "**Your email has been submitted successfully! We will get back to you shortly!**"
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def request_meddra_encode_form(req_reported_term, req_version, req_terms_checkbox):
|
| 22 |
+
# Check if all required fields are filled and the conditions are met
|
| 23 |
+
if not req_reported_term:
|
| 24 |
+
return "**Please enter a medical term.**"
|
| 25 |
+
if not req_version:
|
| 26 |
+
return "**Please select a valid ICD version.**"
|
| 27 |
+
if not req_terms_checkbox:
|
| 28 |
+
return "**You need to agree to Safeterm terms of use.**"
|
| 29 |
+
load_dotenv()
|
| 30 |
+
|
| 31 |
+
req_apikey = os.getenv("SAFETERM_API_KEY")
|
| 32 |
+
|
| 33 |
+
encode_output = encode_caller(req_apikey, req_reported_term, req_version)
|
| 34 |
+
return encode_output
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def encode_caller(apikey, reported_terms, icd_version):
|
| 38 |
+
url = os.getenv("SAFETERM_ENCODE_URL")
|
| 39 |
+
reported_terms_list = [reported_terms.strip()] # Ensure it's a list of strings
|
| 40 |
+
|
| 41 |
+
payload = json.dumps({
|
| 42 |
+
"reported_terms": reported_terms_list,
|
| 43 |
+
"version": int(icd_version),
|
| 44 |
+
"medical_dictionary": "icd-11"
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
headers = {
|
| 48 |
+
'Content-Type': 'application/json',
|
| 49 |
+
'Authorization': f'Bearer {apikey}'
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
response = requests.post(url, headers=headers, data=payload)
|
| 53 |
+
data = response.json()
|
| 54 |
+
|
| 55 |
+
if "detail" in data:
|
| 56 |
+
return data["detail"]
|
| 57 |
+
|
| 58 |
+
results = []
|
| 59 |
+
|
| 60 |
+
for term_data in data.get('responses', []):
|
| 61 |
+
reported_term = term_data.get('reported_term', 'Term missing')
|
| 62 |
+
encoded_term_data = term_data.get('encoded_term')
|
| 63 |
+
|
| 64 |
+
llt_term = ''
|
| 65 |
+
pt_term = ''
|
| 66 |
+
llt_id = ''
|
| 67 |
+
pt_id = ''
|
| 68 |
+
alt_pt_term = []
|
| 69 |
+
result = f"Reported Term:\t {reported_term}\n-------------------------\n"
|
| 70 |
+
|
| 71 |
+
if isinstance(encoded_term_data, dict) and encoded_term_data:
|
| 72 |
+
report = encoded_term_data.get('report', ' ')
|
| 73 |
+
icd_id = encoded_term_data.get('icd_code', 'no_result')
|
| 74 |
+
icd_term = encoded_term_data.get('icd_term', 'no_result')
|
| 75 |
+
icd_parent_id = encoded_term_data.get('icd_parent_code', 'no_result')
|
| 76 |
+
icd_parent_term = encoded_term_data.get('icd_parent_term', 'no_result')
|
| 77 |
+
alt_icd_terms = encoded_term_data.get('alternative_icd_terms', [])
|
| 78 |
+
|
| 79 |
+
result += f"ICD code:\t\t\t\t {icd_term} [{icd_id}]\nIcd term:\t\t\t\t\t {icd_parent_term} [{icd_parent_id}]\n"
|
| 80 |
+
|
| 81 |
+
if alt_icd_terms:
|
| 82 |
+
result += "\nAlternative ICD Parent Terms:\n"
|
| 83 |
+
for term in alt_icd_terms:
|
| 84 |
+
result += f"\t\t\t\t\t\t\t{term}\n"
|
| 85 |
+
|
| 86 |
+
result += f"{report}\n-------------------------\n"
|
| 87 |
+
|
| 88 |
+
result += f"Status: {term_data['status']}"
|
| 89 |
+
# if detected_language: # Check if detected_language is not null and add to result
|
| 90 |
+
# result += f"\nDetected Language: {detected_language}"
|
| 91 |
+
results.append(result)
|
| 92 |
+
|
| 93 |
+
# Add the API messages at the end.
|
| 94 |
+
api_message = data.get("messages", "No API message available")
|
| 95 |
+
api_message = "OK" if api_message is None else api_message
|
| 96 |
+
results.append(f"API Message: {api_message}")
|
| 97 |
+
|
| 98 |
+
return "\n".join(results)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# Create Gradio interface with improved styling
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("### Safeterm: Translate Medical Narratives into Standardized Dictionaries")
|
| 104 |
+
desc_text = gr.HTML("""
|
| 105 |
+
<div style="line-height: 1.6; padding: 10px;">
|
| 106 |
+
<p>Navigate through the tabs to discover the various features of Safeterm:</p>
|
| 107 |
+
<ul>
|
| 108 |
+
<li>Multilanguage encoding of medical verbatims in MedDRA</li>
|
| 109 |
+
<li>Extract medical information from narrative by type</li>
|
| 110 |
+
<li>Verify existing MedDRA encodings and upgrade terms to latest version</li>
|
| 111 |
+
</ul>
|
| 112 |
+
</div>
|
| 113 |
+
""")
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Tab("ICD Encoder"):
|
| 117 |
+
# Multi-column layout for MedDRA Encoder tab
|
| 118 |
+
with gr.Row():
|
| 119 |
+
# Column 1: Inputs
|
| 120 |
+
with gr.Column():
|
| 121 |
+
intro_text = gr.HTML("""
|
| 122 |
+
Encode a medical verbatim into ICD. <br>
|
| 123 |
+
A ICD parent term is reported along with a few optional alternatives. <br>
|
| 124 |
+
After encoding, the matching term can be verified using the verify tab. <br>
|
| 125 |
+
""")
|
| 126 |
+
encode_reported_terms = gr.Dropdown(
|
| 127 |
+
["While walking across the street the patient was hit by a motor vehicle",
|
| 128 |
+
"Pijn in derde vinger",
|
| 129 |
+
"Mientras cruzaba la calle, el paciente fue golpeado por un vehículo motorizado.",
|
| 130 |
+
"a bigg Migrein W1th 0Ra", "lower left limb varices", "basse pression sanguine"],
|
| 131 |
+
label="Medical term",
|
| 132 |
+
info="Enter your medical term here or choose from presets.",
|
| 133 |
+
allow_custom_value=True
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
version_list = ["2023"]
|
| 137 |
+
|
| 138 |
+
# Set the only version as the default value for the dropdown.
|
| 139 |
+
encode_version = gr.Dropdown(choices=version_list, label="MedDRA Version", value=version_list[0])
|
| 140 |
+
|
| 141 |
+
terms_text = gr.HTML("""
|
| 142 |
+
I agree to the Safeterm <a href=https://www.proddis.com/pdf/Proddis_Terms_of_Use.pdf>Terms of Use</a>.
|
| 143 |
+
I consent to the storage of my personal data for training and communication purposes.
|
| 144 |
+
""")
|
| 145 |
+
terms_checkbox = gr.Checkbox(label="I agree.")
|
| 146 |
+
|
| 147 |
+
submit_button = gr.Button("Encode into ICD")
|
| 148 |
+
|
| 149 |
+
# Column 2: Output and Terms of use
|
| 150 |
+
with gr.Column():
|
| 151 |
+
api_response_encode = gr.Textbox(label="Safeterm Output")
|
| 152 |
+
|
| 153 |
+
submit_button.click(request_meddra_encode_form,
|
| 154 |
+
inputs=[encode_reported_terms, encode_version, terms_checkbox],
|
| 155 |
+
outputs=api_response_encode)
|
| 156 |
+
|
| 157 |
+
with gr.Tab("Contact us"):
|
| 158 |
+
# Multi-column layout for Contact us tab
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column():
|
| 161 |
+
gr.Markdown("### Safeterm Features:")
|
| 162 |
+
gr.Markdown(
|
| 163 |
+
"- ICD encode provides ICD-10 and ICD-11 encoding support for epidemiological tracking, health management"
|
| 164 |
+
"and legacy projects.")
|
| 165 |
+
gr.Markdown(
|
| 166 |
+
"- It simplifies tasks with its API-driven approach. It is easy to integrate within existing "
|
| 167 |
+
"e-clinical systems.")
|
| 168 |
+
gr.Markdown(
|
| 169 |
+
"- Subscription plans are adaptable to your needs, with a monthly fee and limited cost per "
|
| 170 |
+
"encoding.")
|
| 171 |
+
gr.Markdown("- Our team is available if you have any question.")
|
| 172 |
+
gr.Markdown("- Interested? Contact us for inquiries!")
|
| 173 |
+
|
| 174 |
+
with gr.Column():
|
| 175 |
+
gr.Markdown("### Contact us:")
|
| 176 |
+
email = gr.Textbox(label="Email", placeholder="Enter your professional email address...")
|
| 177 |
+
request_list = ["API Key", "Commercial inquiry", "Encoding support", "Partnership", "Others"]
|
| 178 |
+
reason = gr.Dropdown(choices=request_list, label="Request", value=request_list[0])
|
| 179 |
+
message = gr.Textbox(label="Message", placeholder="Enter your message...")
|
| 180 |
+
|
| 181 |
+
feedback_textbox = gr.Markdown(label="Feedback")
|
| 182 |
+
|
| 183 |
+
api_key_button = gr.Button("Submit your message")
|
| 184 |
+
api_key_button.click(request_email_form,
|
| 185 |
+
inputs=[email, reason, message],
|
| 186 |
+
outputs=feedback_textbox)
|
| 187 |
+
|
| 188 |
+
with gr.Row():
|
| 189 |
+
gr.Markdown("Safeterm v 0224 (c) Proddis - 2024. Contact us at [email protected]")
|
| 190 |
+
|
| 191 |
+
demo.launch()
|
send_email.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import msal
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def send_email(reason, email, message) -> None:
|
| 8 |
+
body = f"""
|
| 9 |
+
Safeterm_demo v1123 (Huggingface)
|
| 10 |
+
|
| 11 |
+
Subject: {reason}
|
| 12 |
+
|
| 13 |
+
Message: {message}
|
| 14 |
+
|
| 15 |
+
Contact Information: {email}
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
+
client_id = os.getenv("CLIENT_ID")
|
| 21 |
+
client_secret = os.getenv("CLIENT_SECRET")
|
| 22 |
+
tenant_id = os.getenv("TENANT_ID")
|
| 23 |
+
authority = f"https://login.microsoftonline.com/{tenant_id}"
|
| 24 |
+
sender = os.getenv("MAIL_SENDER")
|
| 25 |
+
receiver = os.getenv("MAIL_RECIPIENT")
|
| 26 |
+
cc_receiver = os.getenv("CC_RECIPIENT")
|
| 27 |
+
|
| 28 |
+
app = msal.ConfidentialClientApplication(
|
| 29 |
+
client_id=client_id,
|
| 30 |
+
client_credential=client_secret,
|
| 31 |
+
authority=authority)
|
| 32 |
+
|
| 33 |
+
scopes = ["https://graph.microsoft.com/.default"]
|
| 34 |
+
|
| 35 |
+
result = app.acquire_token_silent(scopes, account=None)
|
| 36 |
+
|
| 37 |
+
if not result:
|
| 38 |
+
print("No suitable token exists in cache. Let's get a new one from Azure Active Directory.")
|
| 39 |
+
result = app.acquire_token_for_client(scopes=scopes)
|
| 40 |
+
|
| 41 |
+
if "access_token" in result:
|
| 42 |
+
endpoint = f'https://graph.microsoft.com/v1.0/users/{sender}/sendMail'
|
| 43 |
+
email_msg = {
|
| 44 |
+
'Message': {
|
| 45 |
+
'Subject': reason,
|
| 46 |
+
'Body': {
|
| 47 |
+
'ContentType': 'Text',
|
| 48 |
+
'Content': body
|
| 49 |
+
},
|
| 50 |
+
'ToRecipients': [{'EmailAddress': {'Address': receiver}}],
|
| 51 |
+
'CcRecipients': [{'EmailAddress': {'Address': cc_receiver}}] # Added CcRecipients here
|
| 52 |
+
},
|
| 53 |
+
'SaveToSentItems': 'true'
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
r = requests.post(endpoint, headers={'Authorization': 'Bearer ' + result['access_token']}, json=email_msg)
|
| 57 |
+
|
| 58 |
+
if r.ok:
|
| 59 |
+
print('Sent email successfully')
|
| 60 |
+
else:
|
| 61 |
+
print(r.json())
|
| 62 |
+
else:
|
| 63 |
+
print(result.get("error"))
|
| 64 |
+
print(result.get("error_description"))
|
| 65 |
+
print(result.get("correlation_id"))
|