yangd05 commited on
Commit
bcb2a93
·
1 Parent(s): 7395976

Initial commit with LLM app

Browse files
Files changed (3) hide show
  1. README.md +40 -5
  2. app.py +123 -33
  3. requirements.txt +2 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Radiology Report Optimizer
3
- emoji: 💬
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 6.5.1
8
  app_file: app.py
@@ -10,7 +10,42 @@ pinned: false
10
  hf_oauth: true
11
  hf_oauth_scopes:
12
  - inference-api
13
- short_description: Radiology Report Refinery
14
  ---
15
 
16
- An example chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and the [Hugging Face Inference API](https://huggingface.co/docs/api-inference/index).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Radiology Report Optimizer
3
+ emoji: 🩻
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 6.5.1
8
  app_file: app.py
 
10
  hf_oauth: true
11
  hf_oauth_scopes:
12
  - inference-api
13
+ short_description: Refine radiology reports for clarity and completeness
14
  ---
15
 
16
+ # Radiology Report Optimizer
17
+
18
+ A Gradio-powered demo that uses
19
+ [nvidia/Llama-3.1-Nemotron-70B-Instruct-HF](https://huggingface.co/nvidia/Llama-3.1-Nemotron-70B-Instruct-HF)
20
+ to refine draft radiology reports.
21
+
22
+ ## What it does
23
+
24
+ Paste a draft radiology report and the model will return an optimized version
25
+ with improvements across six dimensions:
26
+
27
+ | Dimension | Description |
28
+ |---|---|
29
+ | **Structure** | Standard report sections (Indication, Technique, Comparison, Findings, Impression) |
30
+ | **Clarity** | Precise descriptors and standardized terminology |
31
+ | **Completeness** | Flags missing anatomy, incomplete characterization |
32
+ | **Actionability** | Clear recommendations in the Impression |
33
+ | **Brevity** | Removes redundancy while keeping clinical detail |
34
+ | **Standardized Terminology** | BI-RADS, Lung-RADS, LI-RADS, TI-RADS, Bosniak where applicable |
35
+
36
+ ## Running locally
37
+
38
+ ```bash
39
+ pip install -r requirements.txt
40
+ python app.py
41
+ ```
42
+
43
+ The app will start at `http://localhost:7860`. Log in with your Hugging Face
44
+ account to authenticate with the Inference API.
45
+
46
+ ## Deploying to Hugging Face Spaces
47
+
48
+ Push this repository to a
49
+ [Hugging Face Space](https://huggingface.co/docs/hub/spaces-overview) with
50
+ **Gradio** as the SDK. The YAML front-matter in this README configures the
51
+ Space automatically.
app.py CHANGED
@@ -1,67 +1,157 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def respond(
6
- message,
7
  history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
  ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
 
 
21
  messages.extend(history)
22
-
23
  messages.append({"role": "user", "content": message})
24
 
25
  response = ""
26
-
27
- for message in client.chat_completion(
28
  messages,
29
  max_tokens=max_tokens,
30
  stream=True,
31
  temperature=temperature,
32
  top_p=top_p,
33
  ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  chatbot = gr.ChatInterface(
47
  respond,
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
  gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
 
 
 
 
 
57
  label="Top-p (nucleus sampling)",
58
  ),
59
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
62
- with gr.Blocks() as demo:
 
 
 
63
  with gr.Sidebar():
64
  gr.LoginButton()
 
 
 
 
 
 
 
 
 
 
 
 
65
  chatbot.render()
66
 
67
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ MODEL_ID = "nvidia/Llama-3.1-Nemotron-70B-Instruct-HF"
5
+
6
+ SYSTEM_PROMPT = """\
7
+ You are an expert radiologist AI assistant specializing in optimizing and \
8
+ refining radiology reports. Your task is to take draft radiology reports and \
9
+ improve them for clarity, accuracy, completeness, and adherence to best \
10
+ practices.
11
+
12
+ When a user provides a radiology report, you should:
13
+
14
+ 1. **Structure** – Ensure the report follows standard radiology report format:
15
+ - Clinical Indication / History
16
+ - Technique / Protocol
17
+ - Comparison (prior studies)
18
+ - Findings (organized by anatomical system or region)
19
+ - Impression (concise summary of key findings with numbered items)
20
+
21
+ 2. **Clarity** – Replace vague language with precise descriptors. Use \
22
+ standardized radiology terminology. Avoid hedging language when findings are \
23
+ definitive.
24
+
25
+ 3. **Completeness** – Identify and flag any missing sections, unreported \
26
+ anatomy visible on the study, or findings that lack adequate characterization \
27
+ (size, location, morphology, enhancement pattern, etc.).
28
+
29
+ 4. **Actionability** – Ensure the Impression section includes clear, \
30
+ actionable recommendations when appropriate (e.g., follow-up imaging, \
31
+ correlation with clinical findings, tissue sampling).
32
+
33
+ 5. **Brevity** – Remove redundant phrases and filler words while preserving \
34
+ all clinically relevant information.
35
+
36
+ 6. **Standardized Terminology** – Use ACR BI-RADS, Lung-RADS, LI-RADS, \
37
+ TI-RADS, Bosniak, or other applicable classification systems where relevant.
38
+
39
+ When providing the optimized report, present the refined version first, then \
40
+ provide a brief summary of the key changes you made. If the user asks \
41
+ follow-up questions, assist with further refinements.
42
+
43
+ If the user's message is not a radiology report, respond helpfully in the \
44
+ context of radiology reporting best practices.\
45
+ """
46
+
47
+ EXAMPLES = [
48
+ [
49
+ "Please optimize this CT abdomen report:\n\n"
50
+ "Clinical: abdominal pain\n\n"
51
+ "Findings: The liver looks normal. There is a small lesion in the "
52
+ "right kidney. The pancreas is unremarkable. There is some fluid in "
53
+ "the pelvis. The appendix is not well seen.\n\n"
54
+ "Impression: Small kidney lesion. Pelvic fluid. Suggest clinical "
55
+ "correlation."
56
+ ],
57
+ [
58
+ "Refine this chest X-ray report:\n\n"
59
+ "History: cough\n\n"
60
+ "Findings: Heart is normal size. Lungs show some haziness in the "
61
+ "right lower lobe. No pleural effusion. No pneumothorax. Bones are "
62
+ "normal.\n\n"
63
+ "Impression: Right lower lobe opacity, may represent pneumonia."
64
+ ],
65
+ [
66
+ "What are the best practices for structuring a brain MRI report?"
67
+ ],
68
+ ]
69
+
70
 
71
  def respond(
72
+ message: str,
73
  history: list[dict[str, str]],
74
+ max_tokens: int,
75
+ temperature: float,
76
+ top_p: float,
77
+ hf_token: gr.OAuthToken | None = None,
 
78
  ):
79
+ token = hf_token.token if hf_token else None
80
+ client = InferenceClient(token=token, model=MODEL_ID)
 
 
 
 
81
 
82
+ messages = [{"role": "system", "content": SYSTEM_PROMPT}]
83
  messages.extend(history)
 
84
  messages.append({"role": "user", "content": message})
85
 
86
  response = ""
87
+ for chunk in client.chat_completion(
 
88
  messages,
89
  max_tokens=max_tokens,
90
  stream=True,
91
  temperature=temperature,
92
  top_p=top_p,
93
  ):
94
+ choices = chunk.choices
95
+ if choices and choices[0].delta.content:
96
+ response += choices[0].delta.content
97
+ yield response
 
 
 
98
 
99
 
 
 
 
100
  chatbot = gr.ChatInterface(
101
  respond,
102
  additional_inputs=[
 
 
 
103
  gr.Slider(
104
+ minimum=256, maximum=4096, value=2048, step=64,
105
+ label="Max new tokens",
106
+ ),
107
+ gr.Slider(
108
+ minimum=0.1, maximum=1.5, value=0.3, step=0.1,
109
+ label="Temperature",
110
+ ),
111
+ gr.Slider(
112
+ minimum=0.1, maximum=1.0, value=0.9, step=0.05,
113
  label="Top-p (nucleus sampling)",
114
  ),
115
  ],
116
+ examples=EXAMPLES,
117
+ title="🩻 Radiology Report Optimizer",
118
+ description=(
119
+ "Paste a draft radiology report below to get an optimized version with "
120
+ "improved structure, clarity, completeness, and adherence to reporting "
121
+ "standards. Powered by "
122
+ "**nvidia/Llama-3.1-Nemotron-70B-Instruct-HF**."
123
+ ),
124
+ chatbot=gr.Chatbot(
125
+ height=520,
126
+ placeholder=(
127
+ "Paste your radiology report here to get started…\n\n"
128
+ "You can also ask questions about radiology reporting best practices."
129
+ ),
130
+ ),
131
+ textbox=gr.Textbox(
132
+ placeholder="Paste your draft radiology report or ask a question…",
133
+ lines=4,
134
+ ),
135
  )
136
 
137
+ with gr.Blocks(
138
+ title="Radiology Report Optimizer",
139
+ theme=gr.themes.Soft(primary_hue="blue", secondary_hue="cyan"),
140
+ ) as demo:
141
  with gr.Sidebar():
142
  gr.LoginButton()
143
+ gr.Markdown(
144
+ "### About\n"
145
+ "This tool uses **Llama-3.1-Nemotron-70B** to refine "
146
+ "radiology reports for clarity, structure, and "
147
+ "completeness.\n\n"
148
+ "**Tips**\n"
149
+ "- Paste your full draft report for comprehensive "
150
+ "optimization\n"
151
+ "- Ask follow-up questions to refine specific sections\n"
152
+ "- Request formatting in specific classification systems "
153
+ "(BI-RADS, Lung-RADS, LI-RADS, etc.)\n"
154
+ )
155
  chatbot.render()
156
 
157
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio>=4.44.0,<7.0.0
2
+ huggingface_hub>=0.25.0