Spaces:
Sleeping
Sleeping
readme file from medicode repo is the only valuable file there, will delete repo
Browse files
README.md
CHANGED
|
@@ -1,14 +1,179 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.32.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
license:
|
| 11 |
-
short_description:
|
| 12 |
---
|
|
|
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: MedicodeMCP
|
| 3 |
+
emoji: 💬
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.32.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
short_description: an MCP Tool for Symptom-to-ICD Diagnosis Mapping.
|
| 12 |
---
|
| 13 |
+
A chatbot using [Gradio](https://gradio.app), [`huggingface_hub`](https://huggingface.co/docs/huggingface_hub/v0.22.2/en/index), and my local RTX 2060 instead of Cloud APIs
|
| 14 |
|
| 15 |
+
# MedicodeMCP – an MCP Tool for Symptom-to-ICD Diagnosis Mapping
|
| 16 |
+
|
| 17 |
+
## MVP Scope
|
| 18 |
+
- Accept a patient’s symptom description (free-text input).
|
| 19 |
+
- Output a structured JSON with a list of probable diagnoses, each including:
|
| 20 |
+
- ICD-10 code
|
| 21 |
+
- Diagnosis name
|
| 22 |
+
- Confidence score
|
| 23 |
+
- Handle a subset of common symptoms and return the top 3–5 likely diagnoses.
|
| 24 |
+
|
| 25 |
+
## How It Works
|
| 26 |
+
|
| 27 |
+
### Input Interface
|
| 28 |
+
- Gradio-based demo UI for testing:
|
| 29 |
+
- Single text box for symptoms (e.g., “chest pain and shortness of breath”).
|
| 30 |
+
- Primary interface is programmatic (MCP client calls the server).
|
| 31 |
+
|
| 32 |
+
### Processing Logic
|
| 33 |
+
- Leverage an LLM (e.g., OpenAI GPT-4 or Anthropic Claude) to parse symptoms and suggest diagnoses.
|
| 34 |
+
- Prompt example:
|
| 35 |
+
> “The patient reports: {symptoms}. Provide a JSON list of up to 5 possible diagnoses, each with an ICD-10 code and a confidence score between 0 and 1. Use official ICD-10 names and codes.”
|
| 36 |
+
- Recent experiments with medical foundation models (e.g., Google’s Med-PaLM/MedGEMMA) show they can identify relevant diagnosis codes via prompt-based reasoning ([medium.com](https://medium.com)).
|
| 37 |
+
- Using GPT-4/Claude in the loop ensures rapid development and high-quality suggestions ([publish0x.com](https://publish0x.com)).
|
| 38 |
+
|
| 39 |
+
### Confidence Scoring
|
| 40 |
+
- Instruct the LLM to assign a subjective probability (0–1) for each diagnosis.
|
| 41 |
+
- Accept approximate confidences for MVP.
|
| 42 |
+
- Alternative: rank by output order (first = highest confidence).
|
| 43 |
+
|
| 44 |
+
### ICD-10 Code Mapping
|
| 45 |
+
- Trust LLM’s knowledge of common ICD-10 codes (e.g., chest pain → R07.9, heart attack → I21.x).
|
| 46 |
+
- Sanity-check:
|
| 47 |
+
- Maintain a small dictionary of common ICD-10 codes.
|
| 48 |
+
- Use regex to verify code format.
|
| 49 |
+
- Flag or adjust codes that don’t match known patterns.
|
| 50 |
+
- Future improvement: integrate a full ICD-10 lookup list for validation.
|
| 51 |
+
|
| 52 |
+
### Alternate Approach
|
| 53 |
+
- Use an open model fine-tuned for ICD coding (e.g., Clinical BERT on Hugging Face) to predict top ICD-10 codes from clinical text.
|
| 54 |
+
- Requires more coding and possibly a GPU, but feasible.
|
| 55 |
+
- For hackathon MVP, prioritize API-based approach with GPT/Claude ([huggingface.co](https://huggingface.co)).
|
| 56 |
+
|
| 57 |
+
### Output Format
|
| 58 |
+
- JSON structure for easy agent parsing. Example:
|
| 59 |
+
```json
|
| 60 |
+
{
|
| 61 |
+
"diagnoses": [
|
| 62 |
+
{
|
| 63 |
+
"icd_code": "I20.0",
|
| 64 |
+
"diagnosis": "Unstable angina",
|
| 65 |
+
"confidence": 0.85
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"icd_code": "J18.9",
|
| 69 |
+
"diagnosis": "Pneumonia, unspecified organism",
|
| 70 |
+
"confidence": 0.60
|
| 71 |
+
}
|
| 72 |
+
]
|
| 73 |
+
}
|
| 74 |
+
````
|
| 75 |
+
|
| 76 |
+
* Input: “chest pain and shortness of breath”
|
| 77 |
+
* Output: Cardiac-related issues (e.g., angina/MI) and respiratory causes, each with confidence estimates.
|
| 78 |
+
* Structured output aligns with MCP tool requirements for downstream agent reasoning.
|
| 79 |
+
|
| 80 |
+
## Gradio MCP Integration
|
| 81 |
+
|
| 82 |
+
* Implement logic in `app.py` of a Gradio Space.
|
| 83 |
+
* Tag README with `mcp-server-track` as required by hackathon.
|
| 84 |
+
* Follow “Building an MCP Server with Gradio” guide:
|
| 85 |
+
|
| 86 |
+
* Use Gradio SDK 5.x.
|
| 87 |
+
* Define a tool function with metadata for agent discovery.
|
| 88 |
+
* Expose a prediction endpoint.
|
| 89 |
+
|
| 90 |
+
### Example Gradio Definition (simplified)
|
| 91 |
+
|
| 92 |
+
```python
|
| 93 |
+
import gradio as gr
|
| 94 |
+
import openai
|
| 95 |
+
def symptom_to_diagnosis(symptoms: str) -> dict:
|
| 96 |
+
prompt = f"""The patient reports: {symptoms}. Provide a JSON list of up to 5 possible diagnoses, each with an ICD-10 code and a confidence score between 0 and 1. Use official ICD-10 names and codes."""
|
| 97 |
+
response = openai.ChatCompletion.create(
|
| 98 |
+
model="gpt-4",
|
| 99 |
+
messages=[{"role": "system", "content": prompt}],
|
| 100 |
+
temperature=0.2
|
| 101 |
+
)
|
| 102 |
+
# Parse response content as JSON
|
| 103 |
+
return response.choices[0].message.content
|
| 104 |
+
demo = gr.Interface(
|
| 105 |
+
fn=symptom_to_diagnosis,
|
| 106 |
+
inputs=gr.Textbox(placeholder="Enter symptoms here..."),
|
| 107 |
+
outputs=gr.JSON(),
|
| 108 |
+
title="MedicodeMCP Symptom-to-ICD Mapper",
|
| 109 |
+
)
|
| 110 |
+
demo.launch()
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
* Ensure MCP metadata is included so an external agent can discover and call `symptom_to_diagnosis`.
|
| 114 |
+
|
| 115 |
+
## User Demo (Client App)
|
| 116 |
+
|
| 117 |
+
* Create a separate Gradio Space or local script that:
|
| 118 |
+
|
| 119 |
+
* Calls the MCP server endpoint.
|
| 120 |
+
* Renders JSON result in a user-friendly format.
|
| 121 |
+
* Optionally record a video demonstration:
|
| 122 |
+
|
| 123 |
+
* Show an agent (e.g., Claude-2 chatbot) calling the MCP tool.
|
| 124 |
+
* Verify end-to-end functionality.
|
| 125 |
+
|
| 126 |
+
## MVP Development Steps
|
| 127 |
+
|
| 128 |
+
1. **Set Up Gradio Space**
|
| 129 |
+
|
| 130 |
+
* Initialize a new Hugging Face Space with Gradio SDK 5.x.
|
| 131 |
+
* Tag README with `mcp-server-track`.
|
| 132 |
+
|
| 133 |
+
2. **Implement Symptom-to-Diagnosis Function**
|
| 134 |
+
|
| 135 |
+
* Write a Python function to:
|
| 136 |
+
|
| 137 |
+
* Accept symptom text.
|
| 138 |
+
* Call GPT-4/Claude API with JSON-output prompt.
|
| 139 |
+
* Parse the model’s JSON response into a Python dictionary.
|
| 140 |
+
* Sanitize and validate JSON output.
|
| 141 |
+
* Fallback: rule-based approach or offline model for demo cases if API limits are reached.
|
| 142 |
+
|
| 143 |
+
3. **Testing**
|
| 144 |
+
|
| 145 |
+
* Input various symptom combinations.
|
| 146 |
+
* Verify sensibility of diagnoses and correctness of ICD-10 codes.
|
| 147 |
+
* Tweak prompt to improve specificity.
|
| 148 |
+
* Ensure JSON structure is valid.
|
| 149 |
+
|
| 150 |
+
4. **Confidence Calibration**
|
| 151 |
+
|
| 152 |
+
* Define how confidence scores are assigned:
|
| 153 |
+
|
| 154 |
+
* Use LLM’s self-reported confidences, or
|
| 155 |
+
* Rank by output order.
|
| 156 |
+
* Document confidence methodology in README.
|
| 157 |
+
|
| 158 |
+
5. **Integrate with Gradio Blocks**
|
| 159 |
+
|
| 160 |
+
* Wrap the function in a Gradio interface (`gr.Interface` or `gr.ChatInterface`).
|
| 161 |
+
* Expose function as an MCP tool with appropriate metadata.
|
| 162 |
+
* Test via `gradio.Client` or HTTP requests.
|
| 163 |
+
|
| 164 |
+
6. **Build a Quick Client (Optional)**
|
| 165 |
+
|
| 166 |
+
* Option A: Second Gradio Space as MCP client showing how an LLM calls the tool.
|
| 167 |
+
* Option B: Local script using `requests` to call the deployed Space’s prediction API.
|
| 168 |
+
* Prepare a screen recording illustrating agent invocation.
|
| 169 |
+
|
| 170 |
+
7. **Polish Documentation**
|
| 171 |
+
|
| 172 |
+
* In README:
|
| 173 |
+
|
| 174 |
+
* Explain tool functionality and usage.
|
| 175 |
+
* Include hackathon requirements: track tag, demo video or client link.
|
| 176 |
+
* List technologies used (e.g., “Uses OpenAI GPT-4 via API to map symptoms to diagnoses”).
|
| 177 |
+
* Provide example usage and sample inputs/outputs.
|
| 178 |
+
|
| 179 |
+
*By completing these steps, the MVP will demonstrate end-to-end functionality: input symptoms → structured diagnostic insights with ICD-10 codes and confidence scores via an MCP server.*
|