Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- Dockerfile +25 -0
- app.py +202 -60
- gitattributes +35 -0
- requirements.txt +5 -1
Dockerfile
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
# Install git
|
| 6 |
+
RUN apt-get update && apt-get install -y git
|
| 7 |
+
|
| 8 |
+
# Install PyTorch and other dependencies
|
| 9 |
+
COPY requirements.txt /code/
|
| 10 |
+
RUN pip install --no-cache-dir torch --index-url https://download.pytorch.org/whl/cu118
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the application code
|
| 14 |
+
COPY app.py /code/
|
| 15 |
+
|
| 16 |
+
# Set environment variables
|
| 17 |
+
ENV PYTHONPATH=/code
|
| 18 |
+
ENV GRADIO_SERVER_NAME=0.0.0.0
|
| 19 |
+
ENV GRADIO_SERVER_PORT=7860
|
| 20 |
+
|
| 21 |
+
# Expose the port
|
| 22 |
+
EXPOSE 7860
|
| 23 |
+
|
| 24 |
+
# Run the application
|
| 25 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -1,64 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
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
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.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 |
-
demo = 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 |
-
),
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Optional, List, Dict
|
| 3 |
+
from contextlib import asynccontextmanager
|
| 4 |
+
import re
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
from fastapi import FastAPI, HTTPException, status
|
| 8 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 11 |
+
import torch
|
| 12 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
class MedicalReport(BaseModel):
|
| 15 |
+
text: str
|
| 16 |
+
|
| 17 |
+
class ReportResponse(BaseModel):
|
| 18 |
+
assessment: str
|
| 19 |
+
|
| 20 |
+
class MedicalAssessmentModel:
|
| 21 |
+
def __init__(self):
|
| 22 |
+
# Initialize model and tokenizer
|
| 23 |
+
model_name = "meta-llama/Llama-2-7b-chat-hf" # or any other model you prefer
|
| 24 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 25 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
+
model_name,
|
| 27 |
+
torch_dtype=torch.float16,
|
| 28 |
+
device_map="auto"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def generate_response(self, messages: List[Dict]) -> str:
|
| 32 |
+
# Combine messages into a single prompt
|
| 33 |
+
prompt = ""
|
| 34 |
+
for msg in messages:
|
| 35 |
+
role = msg['role']
|
| 36 |
+
content = msg['content']
|
| 37 |
+
prompt += f"{role}: {content}\n"
|
| 38 |
+
|
| 39 |
+
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
|
| 40 |
+
|
| 41 |
+
with torch.no_grad():
|
| 42 |
+
outputs = self.model.generate(
|
| 43 |
+
**inputs,
|
| 44 |
+
max_new_tokens=512,
|
| 45 |
+
temperature=0.7,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
top_p=0.9,
|
| 48 |
+
num_return_sequences=1,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 52 |
+
# Extract only the generated part
|
| 53 |
+
response = response[len(self.tokenizer.decode(inputs['input_ids'][0], skip_special_tokens=True)):]
|
| 54 |
+
return response.strip()
|
| 55 |
+
|
| 56 |
+
def run_env1(self, patient_text: str) -> str:
|
| 57 |
+
"""Tool Selection Stage"""
|
| 58 |
+
messages = [
|
| 59 |
+
{
|
| 60 |
+
"role": "system",
|
| 61 |
+
"content": "You are a medical professional expert in selecting appropriate clinical risk assessment tools."
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"role": "user",
|
| 65 |
+
"content": f"""Based on the patient's discharge summary, identify potential disease risks and assessment needs.
|
| 66 |
+
|
| 67 |
+
Patient Information:
|
| 68 |
+
{patient_text}
|
| 69 |
+
|
| 70 |
+
Please analyze:
|
| 71 |
+
1. Primary health concerns
|
| 72 |
+
2. Risk factors identified
|
| 73 |
+
3. Potential complications
|
| 74 |
+
4. Areas requiring risk assessment"""
|
| 75 |
+
}
|
| 76 |
+
]
|
| 77 |
+
return self.generate_response(messages)
|
| 78 |
+
|
| 79 |
+
def run_env2(self, patient_text: str, env1_output: str) -> str:
|
| 80 |
+
"""Parameter Extraction Stage"""
|
| 81 |
+
messages = [
|
| 82 |
+
{
|
| 83 |
+
"role": "system",
|
| 84 |
+
"content": "You are a medical professional expert in extracting clinical parameters from patient records."
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"role": "user",
|
| 88 |
+
"content": f"""Extract relevant clinical parameters from the patient's information.
|
| 89 |
+
|
| 90 |
+
Patient Information:
|
| 91 |
+
{patient_text}
|
| 92 |
+
|
| 93 |
+
Previous Analysis:
|
| 94 |
+
{env1_output}
|
| 95 |
+
|
| 96 |
+
Please provide:
|
| 97 |
+
1. Key vital signs
|
| 98 |
+
2. Relevant lab values
|
| 99 |
+
3. Clinical findings
|
| 100 |
+
4. Risk factors identified"""
|
| 101 |
+
}
|
| 102 |
+
]
|
| 103 |
+
return self.generate_response(messages)
|
| 104 |
+
|
| 105 |
+
def run_env3(self, patient_text: str, env1_output: str, env2_output: str) -> str:
|
| 106 |
+
"""Risk Interpretation Stage"""
|
| 107 |
+
messages = [
|
| 108 |
+
{
|
| 109 |
+
"role": "system",
|
| 110 |
+
"content": "You are a medical expert specialized in clinical risk assessment and interpretation."
|
| 111 |
+
},
|
| 112 |
+
{
|
| 113 |
+
"role": "user",
|
| 114 |
+
"content": f"""Interpret the identified risks and clinical parameters.
|
| 115 |
+
|
| 116 |
+
Patient Information:
|
| 117 |
+
{patient_text}
|
| 118 |
+
|
| 119 |
+
Risk Analysis:
|
| 120 |
+
{env1_output}
|
| 121 |
+
|
| 122 |
+
Clinical Parameters:
|
| 123 |
+
{env2_output}
|
| 124 |
+
|
| 125 |
+
Please provide:
|
| 126 |
+
1. Risk level assessment for each identified condition
|
| 127 |
+
2. Clinical significance of findings
|
| 128 |
+
3. Interaction between different risk factors
|
| 129 |
+
4. Severity assessment"""
|
| 130 |
+
}
|
| 131 |
+
]
|
| 132 |
+
return self.generate_response(messages)
|
| 133 |
+
|
| 134 |
+
def run_env4(self, patient_text: str, env1_output: str, env2_output: str, env3_output: str) -> str:
|
| 135 |
+
"""Final Assessment Stage"""
|
| 136 |
+
messages = [
|
| 137 |
+
{
|
| 138 |
+
"role": "system",
|
| 139 |
+
"content": "You are a medical expert specialized in comprehensive risk assessment and patient care planning."
|
| 140 |
+
},
|
| 141 |
+
{
|
| 142 |
+
"role": "user",
|
| 143 |
+
"content": f"""Based on all previous analyses, provide a comprehensive assessment of the patient's disease risks.
|
| 144 |
+
|
| 145 |
+
Patient Information:
|
| 146 |
+
{patient_text}
|
| 147 |
+
|
| 148 |
+
Previous Analyses:
|
| 149 |
+
Risk Identification: {env1_output}
|
| 150 |
+
Parameter Analysis: {env2_output}
|
| 151 |
+
Risk Interpretation: {env3_output}
|
| 152 |
+
|
| 153 |
+
Please provide:
|
| 154 |
+
1. Summary of significant disease risks identified
|
| 155 |
+
2. Overall risk assessment
|
| 156 |
+
3. Key areas of concern
|
| 157 |
+
4. Recommended monitoring or preventive measures
|
| 158 |
+
5. Suggestions for risk mitigation
|
| 159 |
+
|
| 160 |
+
Format the response in clear sections with headers."""
|
| 161 |
+
}
|
| 162 |
+
]
|
| 163 |
+
return self.generate_response(messages)
|
| 164 |
+
|
| 165 |
+
def process_report(self, patient_text: str) -> str:
|
| 166 |
+
"""Process the entire pipeline and return ENV4 output"""
|
| 167 |
+
try:
|
| 168 |
+
# Run all environments sequentially
|
| 169 |
+
env1_output = self.run_env1(patient_text)
|
| 170 |
+
env2_output = self.run_env2(patient_text, env1_output)
|
| 171 |
+
env3_output = self.run_env3(patient_text, env1_output, env2_output)
|
| 172 |
+
env4_output = self.run_env4(patient_text, env1_output, env2_output, env3_output)
|
| 173 |
+
|
| 174 |
+
return env4_output
|
| 175 |
+
except Exception as e:
|
| 176 |
+
return f"Error in processing: {str(e)}"
|
| 177 |
+
|
| 178 |
+
def create_gradio_interface():
|
| 179 |
+
model = MedicalAssessmentModel()
|
| 180 |
+
|
| 181 |
+
def analyze_text(text):
|
| 182 |
+
return model.process_report(text)
|
| 183 |
+
|
| 184 |
+
iface = gr.Interface(
|
| 185 |
+
fn=analyze_text,
|
| 186 |
+
inputs=gr.Textbox(
|
| 187 |
+
lines=10,
|
| 188 |
+
placeholder="Enter patient medical report here...",
|
| 189 |
+
label="Medical Report"
|
| 190 |
+
),
|
| 191 |
+
outputs=gr.Textbox(
|
| 192 |
+
lines=15,
|
| 193 |
+
label="Risk Assessment Report"
|
| 194 |
+
),
|
| 195 |
+
title="Medical Report Risk Assessment",
|
| 196 |
+
description="Enter a medical report to get a comprehensive risk assessment. The system will analyze the report through multiple stages and provide a final assessment.",
|
| 197 |
+
examples=[
|
| 198 |
+
["Patient was admitted with chest pain and shortness of breath. History of hypertension and diabetes. BP 160/95, HR 98. Recent smoker with 30 pack-year history."],
|
| 199 |
+
["83-year-old female presents with confusion and fever. Recent fall at home. History of osteoporosis and mild cognitive impairment. Lives alone. Temperature 38.5C, BP 135/85."]
|
| 200 |
+
]
|
| 201 |
+
)
|
| 202 |
+
return iface
|
| 203 |
|
| 204 |
if __name__ == "__main__":
|
| 205 |
+
iface = create_gradio_interface()
|
| 206 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
gradio>=4.0.0
|
| 5 |
+
pydantic
|