File size: 5,337 Bytes
a52ee17
ffb43be
 
 
 
a52ee17
b18cfe3
a52ee17
 
ffb43be
a52ee17
 
d93af2a
ffb43be
d93af2a
ffb43be
d93af2a
ffb43be
d93af2a
 
 
 
 
ffb43be
d93af2a
ffb43be
d93af2a
ffb43be
d93af2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffb43be
d93af2a
ffb43be
d93af2a
ffb43be
d93af2a
ffb43be
d93af2a
 
ffb43be
d93af2a
ffb43be
d93af2a
ffb43be
d93af2a
 
 
ffb43be
d93af2a
ffb43be
d93af2a
ffb43be
 
d93af2a
ffb43be
 
d93af2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffb43be
d93af2a
 
 
 
 
ffb43be
d93af2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffb43be
d93af2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: MedGemma Symptom Analyzer
emoji: πŸ₯
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 5.35.0
app_file: app.py
pinned: false
license: apache-2.0
---

# MedGemma Symptom Analyzer

A modern medical AI application using Google's MedGemma model via HuggingFace Inference API for symptom analysis and medical consultation.

## πŸ₯ Features

- **AI-Powered Symptom Analysis**: Uses Google's MedGemma-4B model for medical insights
- **Comprehensive Medical Reports**: Provides differential diagnoses, next steps, and red flags
- **Interactive Web Interface**: Built with Gradio for easy use
- **Demo Mode**: Fallback functionality when API is unavailable
- **Medical Safety**: Includes appropriate disclaimers and safety guidance

## πŸš€ Quick Start

### 1. Installation

```bash
# Clone the repository
git clone <your-repo-url>
cd medgemma-symptomps

# Install dependencies
pip install -r requirements.txt
```

### 2. HuggingFace Access Setup

The app uses Google's MedGemma model, which requires special access:

1. **Get HuggingFace Token**:
   - Visit [HuggingFace Settings](https://huggingface.co/settings/tokens)
   - Create a new token with `read` permissions

2. **Request MedGemma Access**:
   - Visit [google/medgemma-4b-it](https://huggingface.co/google/medgemma-4b-it)
   - Click "Request access to this model"
   - Wait for approval from Google (may take some time)

3. **Set Environment Variable**:
   ```bash
   export HF_TOKEN="your_huggingface_token_here"
   ```

### 3. Run the Application

```bash
python3 app.py
```

The app will start on `http://localhost:7860` (or next available port).

## πŸ”§ Configuration

### Environment Variables

- `HF_TOKEN`: Your HuggingFace API token (required for model access)
- `FORCE_CPU`: Set to `true` to force CPU usage (not needed for API version)

### Model Access Status

The app handles different access scenarios:

- βœ… **Full Access**: MedGemma model available via API
- ⚠️ **Pending Access**: Waiting for model approval (uses demo mode)
- ❌ **No Access**: Falls back to demo responses

## πŸ§ͺ Testing

Test the API connection:

```bash
python3 test_api.py
```

This will verify:
- HuggingFace API connectivity
- Token validity  
- Model access permissions

## πŸ“‹ Usage

### Web Interface

1. Open the app in your browser
2. Enter patient symptoms in the text area
3. Adjust creativity slider if desired
4. Click "Analyze Symptoms"
5. Review the comprehensive medical analysis

### Example Symptoms

Try these example symptom descriptions:

- **Flu-like**: "Fever, headache, body aches, and fatigue for 3 days"
- **Chest pain**: "Sharp chest pain worsening with breathing, shortness of breath"  
- **Digestive**: "Abdominal pain, nausea, and diarrhea after eating"

## πŸ”’ Medical Disclaimer

**⚠️ IMPORTANT**: This tool is for educational purposes only. It should never replace professional medical advice, diagnosis, or treatment. Always consult qualified healthcare professionals for medical concerns.

## πŸ—οΈ Architecture

### API-Based Design

The app now uses HuggingFace Inference API instead of local model loading:

- **Advantages**: 
  - No local GPU/CPU requirements
  - Faster startup time
  - Always up-to-date model
  - Reduced memory usage

- **Requirements**:
  - Internet connection
  - Valid HuggingFace token
  - Model access approval

### File Structure

```
medgemma-symptomps/
β”œβ”€β”€ app.py                    # Main Gradio application
β”œβ”€β”€ test_api.py              # API connection test script
β”œβ”€β”€ requirements.txt         # Python dependencies
β”œβ”€β”€ README.md               # This file
└── medgemma_app.log        # Application logs
```

## πŸ› οΈ Development

### Key Components

1. **MedGemmaSymptomAnalyzer**: Main class handling API connections
2. **Gradio Interface**: Web UI with symptom input and analysis display
3. **Demo Responses**: Fallback functionality for offline use

### API Integration

```python
from huggingface_hub import InferenceClient

client = InferenceClient(token=hf_token)
response = client.text_generation(
    prompt=medical_prompt,
    model="google/medgemma-4b-it",
    max_new_tokens=400,
    temperature=0.7
)
```

## πŸ” Troubleshooting

### Common Issues

1. **404 Model Not Found**:
   - Ensure you have requested access to MedGemma
   - Wait for Google's approval
   - Verify your HuggingFace token is valid

2. **Demo Mode Only**:
   - Check your internet connection
   - Verify HF_TOKEN environment variable
   - Confirm model access approval status

3. **Slow Responses**:
   - API responses may take 10-30 seconds
   - Consider adjusting max_tokens parameter

### Getting Help

- Check the application logs: `tail -f medgemma_app.log`
- Test API connection: `python3 test_api.py`
- Verify model access: Visit the HuggingFace model page

## πŸ“š Resources

- [MedGemma Model Card](https://huggingface.co/google/medgemma-4b-it)
- [HuggingFace Inference API](https://huggingface.co/docs/api-inference/index)
- [Gradio Documentation](https://gradio.app/docs/)

## πŸ“„ License

This project uses the MedGemma model which has its own licensing terms. Please review the [model license](https://huggingface.co/google/medgemma-4b-it) before use.

---

**Remember**: Always prioritize patient safety and consult healthcare professionals for medical decisions.