Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ from datetime import datetime, timedelta
|
|
| 9 |
import os
|
| 10 |
from qwen_agent.agents import Assistant
|
| 11 |
from qwen_agent.gui.web_ui import WebUI
|
|
|
|
| 12 |
|
| 13 |
@dataclass
|
| 14 |
class PatientMetadata:
|
|
@@ -68,6 +69,49 @@ class BreastCancerAgent(Assistant):
|
|
| 68 |
"SIATCN/vit_tumor_radius_detection_finetuned"
|
| 69 |
)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
def _process_image(self, image: Image.Image) -> Image.Image:
|
| 72 |
"""Process input image for model consumption."""
|
| 73 |
if image.mode != 'RGB':
|
|
@@ -116,36 +160,6 @@ class BreastCancerAgent(Assistant):
|
|
| 116 |
hormone_therapy=hormone_therapy
|
| 117 |
)
|
| 118 |
|
| 119 |
-
def run(self, image_path: str) -> str:
|
| 120 |
-
"""Run analysis on an image."""
|
| 121 |
-
try:
|
| 122 |
-
image = Image.open(image_path)
|
| 123 |
-
processed_image = self._process_image(image)
|
| 124 |
-
analysis = self._analyze_image(processed_image)
|
| 125 |
-
|
| 126 |
-
report = f"""MICROWAVE IMAGING ANALYSIS:
|
| 127 |
-
• Detection: {'Positive' if analysis.has_tumor else 'Negative'}
|
| 128 |
-
• Size: {analysis.tumor_size} cm
|
| 129 |
-
|
| 130 |
-
PATIENT INFO:
|
| 131 |
-
• Age: {analysis.metadata.age} years
|
| 132 |
-
• Risk Factors: {', '.join([
|
| 133 |
-
'family history' if analysis.metadata.family_history else '',
|
| 134 |
-
analysis.metadata.smoking_status.lower(),
|
| 135 |
-
'hormone therapy' if analysis.metadata.hormone_therapy else '',
|
| 136 |
-
]).strip(', ')}
|
| 137 |
-
|
| 138 |
-
REPORT:
|
| 139 |
-
{'Abnormal scan showing potential mass.' if analysis.has_tumor else 'Normal scan with no significant findings.'}
|
| 140 |
-
Confidence level: {analysis.confidence:.1%}
|
| 141 |
-
|
| 142 |
-
RECOMMENDATION:
|
| 143 |
-
{('Immediate follow-up imaging recommended.' if analysis.tumor_size in ['1.0', '1.5'] else 'Follow-up imaging in 6 months recommended.') if analysis.has_tumor else 'Continue routine screening per protocol.'}"""
|
| 144 |
-
|
| 145 |
-
return report
|
| 146 |
-
except Exception as e:
|
| 147 |
-
return f"Error during analysis: {str(e)}"
|
| 148 |
-
|
| 149 |
def run_interface():
|
| 150 |
"""Create and run the WebUI interface."""
|
| 151 |
agent = BreastCancerAgent()
|
|
@@ -157,7 +171,8 @@ def run_interface():
|
|
| 157 |
{'text': 'Can you analyze this mammogram?'},
|
| 158 |
{'text': 'What should I look for in the results?'},
|
| 159 |
{'text': 'How reliable is the detection?'}
|
| 160 |
-
]
|
|
|
|
| 161 |
}
|
| 162 |
|
| 163 |
app = WebUI(agent, chatbot_config=chatbot_config)
|
|
|
|
| 9 |
import os
|
| 10 |
from qwen_agent.agents import Assistant
|
| 11 |
from qwen_agent.gui.web_ui import WebUI
|
| 12 |
+
from qwen_agent.llm.schema import ROLE, CONTENT, USER, ASSISTANT, IMAGE, Message
|
| 13 |
|
| 14 |
@dataclass
|
| 15 |
class PatientMetadata:
|
|
|
|
| 69 |
"SIATCN/vit_tumor_radius_detection_finetuned"
|
| 70 |
)
|
| 71 |
|
| 72 |
+
def chat(self, message: str, history: Optional[List[Message]] = None) -> str:
|
| 73 |
+
"""Handle chat messages and image analysis."""
|
| 74 |
+
if not history:
|
| 75 |
+
return "Hello! Please upload a breast microwave image for analysis."
|
| 76 |
+
|
| 77 |
+
last_message = history[-1]
|
| 78 |
+
if last_message[ROLE] != USER:
|
| 79 |
+
return "Please provide an image for analysis."
|
| 80 |
+
|
| 81 |
+
# Check for image in the message
|
| 82 |
+
image_item = next((item for item in last_message[CONTENT] if IMAGE in item), None)
|
| 83 |
+
if not image_item:
|
| 84 |
+
return "Please upload an image for analysis. I can only process breast microwave images."
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
image_path = image_item[IMAGE].replace('file://', '')
|
| 88 |
+
image = Image.open(image_path)
|
| 89 |
+
processed_image = self._process_image(image)
|
| 90 |
+
analysis = self._analyze_image(processed_image)
|
| 91 |
+
|
| 92 |
+
report = f"""MICROWAVE IMAGING ANALYSIS:
|
| 93 |
+
• Detection: {'Positive' if analysis.has_tumor else 'Negative'}
|
| 94 |
+
• Size: {analysis.tumor_size} cm
|
| 95 |
+
|
| 96 |
+
PATIENT INFO:
|
| 97 |
+
• Age: {analysis.metadata.age} years
|
| 98 |
+
• Risk Factors: {', '.join([
|
| 99 |
+
'family history' if analysis.metadata.family_history else '',
|
| 100 |
+
analysis.metadata.smoking_status.lower(),
|
| 101 |
+
'hormone therapy' if analysis.metadata.hormone_therapy else '',
|
| 102 |
+
]).strip(', ')}
|
| 103 |
+
|
| 104 |
+
REPORT:
|
| 105 |
+
{'Abnormal scan showing potential mass.' if analysis.has_tumor else 'Normal scan with no significant findings.'}
|
| 106 |
+
Confidence level: {analysis.confidence:.1%}
|
| 107 |
+
|
| 108 |
+
RECOMMENDATION:
|
| 109 |
+
{('Immediate follow-up imaging recommended.' if analysis.tumor_size in ['1.0', '1.5'] else 'Follow-up imaging in 6 months recommended.') if analysis.has_tumor else 'Continue routine screening per protocol.'}"""
|
| 110 |
+
|
| 111 |
+
return report
|
| 112 |
+
except Exception as e:
|
| 113 |
+
return f"Error analyzing image: {str(e)}"
|
| 114 |
+
|
| 115 |
def _process_image(self, image: Image.Image) -> Image.Image:
|
| 116 |
"""Process input image for model consumption."""
|
| 117 |
if image.mode != 'RGB':
|
|
|
|
| 160 |
hormone_therapy=hormone_therapy
|
| 161 |
)
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
def run_interface():
|
| 164 |
"""Create and run the WebUI interface."""
|
| 165 |
agent = BreastCancerAgent()
|
|
|
|
| 171 |
{'text': 'Can you analyze this mammogram?'},
|
| 172 |
{'text': 'What should I look for in the results?'},
|
| 173 |
{'text': 'How reliable is the detection?'}
|
| 174 |
+
],
|
| 175 |
+
'verbose': True
|
| 176 |
}
|
| 177 |
|
| 178 |
app = WebUI(agent, chatbot_config=chatbot_config)
|