File size: 4,635 Bytes
eafef8c | 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 | import streamlit as st
import os
import sys
import pandas as pd
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
try:
from analyzer import MedicalKnowledgeBase
db_path = os.path.join("data", "medical_db.json")
kb = MedicalKnowledgeBase(db_path)
except Exception as e:
st.error(f"خطا در لود دیتابیس: {e}")
st.stop()
st.set_page_config(
page_title="سیستم هوشمند تحلیل آزمایش خون",
page_icon="🩺",
layout="wide"
)
st.markdown("""
<style>
.big-font { font-size:20px !important; font-weight: bold; color: #FF4B4B; }
.success-msg { background-color: #d4edda; padding: 15px; border-radius: 10px; border-left: 5px solid #28a745; }
.main-header { text-align: center; color: #2E86C1; }
</style>
""", unsafe_allow_html=True)
st.markdown('<h1 class="main-header">🩺 سیستم هوشمند تحلیل آزمایش خون</h1>', unsafe_allow_html=True)
st.markdown("---")
st.markdown('<p class="big-font">لطفاً تصویر آزمایش خون خود را انتخاب کنید:</p>', unsafe_allow_html=True)
uploaded_file = st.file_uploader("انتخاب عکس (JPG, PNG)", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
st.image(uploaded_file, caption="پیشنمایش عکس", use_column_width=True)
if st.button("🚀 شروع تحلیل هوشمند", type="primary"):
temp_dir = "data/uploads"
os.makedirs(temp_dir, exist_ok=True)
temp_path = os.path.join(temp_dir, uploaded_file.name)
with open(temp_path, "wb") as f:
f.write(uploaded_file.getbuffer())
status_placeholder = st.empty()
try:
# لود مدلها
status_placeholder.info("⏳ در حال بارگذاری سیستم OCR...")
from ocr_reader import OCRReader
from lab_extractor import LabDataExtractor
from analyzer import BloodAnalyzer
from ai_doctor import AIDoctor
ocr = OCRReader()
extractor = LabDataExtractor()
analyzer = BloodAnalyzer(kb)
ai_doctor = AIDoctor()
# پردازش
status_placeholder.info("👁️ در حال خواندن متن از عکس...")
raw_text = ocr.extract_text(temp_path)
status_placeholder.info("🔍 در حال استخراج اعداد...")
extracted_data = extractor.extract_all(raw_text)
if not extracted_data:
st.error("❌ سیستم نتوانست اعداد را پیدا کند.")
else:
status_placeholder.info("🧠 در حال تحلیل با هوش مصنوعی...")
# دریافت سن از متن
patient_age = extractor.get_patient_age(raw_text)
# تحلیل جدولی با ارسال سن
report = analyzer.generate_full_report(extracted_data, gender="male", age=patient_age)
# دکتر هوشمند با ارسال سن
doctor_note = ai_doctor.generate_explanation(report, patient_gender="male", patient_age=patient_age)
status_placeholder.empty()
st.success("✅ تحلیل کامل شد!")
tab1, tab2 = st.tabs(["📊 جدول نتایج", "👨⚕️ توضیحات دکتر هوشمند"])
with tab1:
df = pd.DataFrame(report)
def get_status(status):
return "✅ نرمال" if status == "NORMAL" else "⚠️ غیرنرمال"
df['وضعیت'] = df['status'].apply(get_status)
df_view = df[['name_fa', 'value', 'unit', 'وضعیت', 'message']]
df_view.columns = ['نام آزمایش', 'مقدار', 'واحد', 'وضعیت', 'تشخیص']
st.dataframe(df_view, use_container_width=True)
with tab2:
st.markdown(f"<div class='success-msg'>{doctor_note}</div>", unsafe_allow_html=True)
except Exception as e:
status_placeholder.error(f"❌ خطا در فرایند تحلیل: {str(e)}")
st.exception(e)
else:
st.warning("⚠️ لطفاً ابتدا یک عکس آپلود کنید.") |