suhana13 commited on
Commit
0a32303
1 Parent(s): c7d9eb6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -0
app.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """NER.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/14VcPCWWSAS7tEIolL_I9iA0xulk2gHuN
8
+ """
9
+
10
+ !pip install -q transformers
11
+
12
+ from transformers import pipeline
13
+
14
+ !pip install -q gradio
15
+
16
+ import gradio as gr
17
+
18
+ # Corrected examples in the nested list format
19
+ examples = [
20
+ ["Date of Service: March 15, 2024\nPatient Name: John Doe\nAge: 45\nDiagnosis: Acute bronchitis\n\nChief Complaint:\nPatient presents with a persistent cough, productive of green sputum, for the past week. Reports accompanying symptoms of low-grade fever, malaise, and mild shortness of breath on exertion.\n\nHistory of Present Illness:\nMr. Doe reports the onset of symptoms approximately 10 days ago with a gradual worsening of his cough and overall condition. Denies any recent travel or sick contacts.\n\nAssessment:\nUpon examination, patient displays signs consistent with acute bronchitis, including rhonchi on auscultation, mild tachypnea, and low-grade fever of 100.4°F.\n\nPlan:\n1. Prescribed 7-day course of azithromycin 500mg once daily.\n2. Encouraged increased fluid intake and rest.\n3. Follow-up appointment scheduled in one week for reassessment.\n\nDr. Jane Smith, MD"],
21
+ ["Date of Service: March 16, 2024\nPatient Name: Jane Johnson\nAge: 32\nDiagnosis: Hypertension\n\nChief Complaint:\nPatient presents for routine follow-up of hypertension.\n\nHistory of Present Illness:\nMs. Johnson was diagnosed with hypertension 6 months ago and has been taking lisinopril 10mg daily. Reports occasional headaches and dizziness, denies any chest pain or shortness of breath.\n\nAssessment:\nBlood pressure today is 140/90 mmHg. Otherwise, general examination is unremarkable.\n\nPlan:\n1. Increased lisinopril dosage to 20mg daily.\n2. Advised lifestyle modifications including low-sodium diet and regular exercise.\n3. Follow-up appointment scheduled in 3 months.\n\nDr. Michael Lee, MD"],
22
+ ["Date of Service: March 17, 2024\nPatient Name: Sarah Adams\nAge: 28\nDiagnosis: Urinary Tract Infection (UTI)\n\nChief Complaint:\nPatient complains of burning sensation on urination and increased frequency.\n\nHistory of Present Illness:\nMs. Adams reports symptoms starting 3 days ago. Denies any fever, flank pain, or hematuria.\n\nAssessment:\nUrinalysis reveals presence of leukocytes and nitrites. Mild suprapubic tenderness on examination.\n\nPlan:\n1. Prescribed 3-day course of ciprofloxacin 500mg twice daily.\n2. Advised increased fluid intake and avoidance of irritants like caffeine.\n3. Follow-up in one week for reassessment or sooner if symptoms worsen.\n\nDr. Emily Davis, MD"]
23
+ ]
24
+
25
+ # Define multiple NER models
26
+ models = {
27
+ "deid_roberta_i2b2": "obi/deid_roberta_i2b2",
28
+ "bert-base-NER": "dslim/bert-base-NER",
29
+ # Add more models as needed
30
+ }
31
+
32
+ # Create a dictionary of model names to their corresponding pipeline instances
33
+ ner_pipelines = {name: pipeline("ner", model=model) for name, model in models.items()}
34
+
35
+ def ner(text, model_name="deid_roberta_i2b2"):
36
+ # Get the NER pipeline based on the selected model name
37
+ ner_pipeline = ner_pipelines[model_name]
38
+ output = ner_pipeline(text, aggregation_strategy="simple")
39
+ return {"text": text, "entities": output}
40
+
41
+ # Define the Gradio interface
42
+ demo = gr.Interface(
43
+ fn=ner,
44
+ inputs=[
45
+ gr.Textbox(placeholder="Enter a sentence here..", lines=10),
46
+ gr.Dropdown(choices=list(models.keys()), label="Select Model"),
47
+ ],
48
+ outputs=gr.HighlightedText(),
49
+ examples=examples,
50
+ title="Named Entity Recognition (NER) Demo",
51
+ description="Select a model and enter a sentence to extract named entities.",
52
+ )
53
+
54
+ # Launch the Gradio interface
55
+ demo.launch(share=True)