Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,26 +9,16 @@ from duckduckgo_search import DDGS
|
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
import isbnlib
|
| 11 |
|
| 12 |
-
# --- 1. SETUP & AUTHENTICATION ---
|
| 13 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 14 |
-
|
| 15 |
-
st.set_page_config(
|
| 16 |
-
page_title="Medical AI Librarian",
|
| 17 |
-
layout="wide",
|
| 18 |
-
page_icon="🏥",
|
| 19 |
-
initial_sidebar_state="collapsed"
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
try:
|
| 23 |
-
client = InferenceClient(model="Qwen/Qwen2.5-72B-Instruct", token=hf_token)
|
| 24 |
-
except:
|
| 25 |
-
client = None
|
| 26 |
-
|
| 27 |
# استيراد الملفات المحلية
|
| 28 |
try:
|
| 29 |
from nlm_classifier import AdvancedNLMClassifier
|
| 30 |
from metadata_fetcher import EnhancedMetadataFetcher
|
| 31 |
from cover_fetcher import EnhancedCoverFetcher
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except ImportError as e:
|
| 33 |
st.error(f"Error importing modules: {e}")
|
| 34 |
# واجهات افتراضية
|
|
@@ -43,6 +33,30 @@ except ImportError as e:
|
|
| 43 |
class EnhancedCoverFetcher:
|
| 44 |
def get_cover(self, isbn, title=None):
|
| 45 |
return {'url': f"https://placehold.co/400x600?text=ISBN:{isbn}", 'status': 'fallback'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# --- 2. CSS STYLING ---
|
| 48 |
st.markdown("""
|
|
@@ -371,6 +385,31 @@ CRITICAL: The NLM classification must be accurate and specific to the main subje
|
|
| 371 |
else:
|
| 372 |
data['authors'] = []
|
| 373 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
return data
|
| 375 |
|
| 376 |
except Exception as e:
|
|
@@ -388,54 +427,11 @@ CRITICAL: The NLM classification must be accurate and specific to the main subje
|
|
| 388 |
'summary': 'AI analysis incomplete. Using enhanced local classification.',
|
| 389 |
'acquisition_decision': 'Review Required',
|
| 390 |
'quality_score': 5,
|
| 391 |
-
'data_accuracy': 'Medium'
|
|
|
|
|
|
|
| 392 |
}
|
| 393 |
|
| 394 |
-
def build_marc_record(data, isbn):
|
| 395 |
-
"""بناء سجل MARC21"""
|
| 396 |
-
marc_fields = []
|
| 397 |
-
marc_fields.append(f"001 ## $a ISBN{isbn}")
|
| 398 |
-
marc_fields.append(f"005 ## $a {datetime.now().strftime('%Y%m%d%H%M%S')}.0")
|
| 399 |
-
marc_fields.append(f"020 ## $a {data.get('isbn_13', isbn)} $c (print)")
|
| 400 |
-
if data.get('isbn_10'):
|
| 401 |
-
marc_fields.append(f"020 ## $a {data['isbn_10']}")
|
| 402 |
-
marc_fields.append(f"060 10 $a {data.get('nlm_class', 'W 1')}")
|
| 403 |
-
|
| 404 |
-
if data.get('authors'):
|
| 405 |
-
marc_fields.append(f"100 1# $a {data['authors'][0]}")
|
| 406 |
-
|
| 407 |
-
title_field = f"245 10 $a {data.get('title', 'Untitled')}"
|
| 408 |
-
if data.get('sub_title'):
|
| 409 |
-
title_field += f" : $b {data['sub_title']}"
|
| 410 |
-
marc_fields.append(title_field)
|
| 411 |
-
|
| 412 |
-
if data.get('edition'):
|
| 413 |
-
marc_fields.append(f"250 ## $a {data['edition']}")
|
| 414 |
-
|
| 415 |
-
pub_info = f"264 #1 $a [Place of publication not identified] : $b {data.get('publisher', 'Unknown')}"
|
| 416 |
-
if data.get('pub_year'):
|
| 417 |
-
pub_info += f", $c {data['pub_year']}"
|
| 418 |
-
marc_fields.append(pub_info)
|
| 419 |
-
|
| 420 |
-
marc_fields.append(f"300 ## $a {data.get('pages', 'xii')} pages : $b illustrations ; $c 24 cm")
|
| 421 |
-
|
| 422 |
-
if data.get('contents_note'):
|
| 423 |
-
contents = str(data['contents_note']).replace('\n', ' -- ')
|
| 424 |
-
marc_fields.append(f"505 0# $a {contents[:500]}")
|
| 425 |
-
|
| 426 |
-
if data.get('summary'):
|
| 427 |
-
marc_fields.append(f"520 ## $a {data['summary'][:500]}")
|
| 428 |
-
|
| 429 |
-
for i, mesh in enumerate(data.get('mesh_subjects', [])[:5]):
|
| 430 |
-
marc_fields.append(f"650 #{i+1}2 $a {mesh}")
|
| 431 |
-
|
| 432 |
-
if len(data.get('authors', [])) > 1:
|
| 433 |
-
for author in data['authors'][1:]:
|
| 434 |
-
marc_fields.append(f"700 1# $a {author}")
|
| 435 |
-
|
| 436 |
-
marc_fields.append(f"856 42 $3 WorldCat $u https://worldcat.org/isbn/{isbn}")
|
| 437 |
-
return marc_fields
|
| 438 |
-
|
| 439 |
# --- 4. MAIN UI ---
|
| 440 |
if 'analysis_data' not in st.session_state:
|
| 441 |
st.session_state.analysis_data = None
|
|
@@ -703,26 +699,57 @@ if st.session_state.analysis_data:
|
|
| 703 |
|
| 704 |
with tab3:
|
| 705 |
st.markdown('<div class="modern-card">', unsafe_allow_html=True)
|
| 706 |
-
st.markdown("### 💻 MARC21 Record")
|
| 707 |
|
| 708 |
marc_lines = build_marc_record(ai, data['isbn'])
|
| 709 |
marc_text = "\n".join(marc_lines)
|
| 710 |
-
|
|
|
|
|
|
|
| 711 |
|
| 712 |
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 713 |
with col1:
|
| 714 |
st.download_button(
|
| 715 |
-
label="📥
|
| 716 |
data=marc_text,
|
| 717 |
file_name=f"{data['isbn']}.mrc",
|
| 718 |
-
mime="text/plain"
|
|
|
|
| 719 |
)
|
| 720 |
with col2:
|
| 721 |
st.download_button(
|
| 722 |
-
label="📊
|
| 723 |
-
data=
|
| 724 |
-
file_name=f"{data['isbn']}
|
| 725 |
-
mime="application/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
)
|
| 727 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 728 |
|
|
|
|
| 9 |
from huggingface_hub import InferenceClient
|
| 10 |
import isbnlib
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# استيراد الملفات المحلية
|
| 13 |
try:
|
| 14 |
from nlm_classifier import AdvancedNLMClassifier
|
| 15 |
from metadata_fetcher import EnhancedMetadataFetcher
|
| 16 |
from cover_fetcher import EnhancedCoverFetcher
|
| 17 |
+
from marc_generator import (
|
| 18 |
+
build_marc_record,
|
| 19 |
+
generate_marc_iso2709,
|
| 20 |
+
generate_marcxml
|
| 21 |
+
)
|
| 22 |
except ImportError as e:
|
| 23 |
st.error(f"Error importing modules: {e}")
|
| 24 |
# واجهات افتراضية
|
|
|
|
| 33 |
class EnhancedCoverFetcher:
|
| 34 |
def get_cover(self, isbn, title=None):
|
| 35 |
return {'url': f"https://placehold.co/400x600?text=ISBN:{isbn}", 'status': 'fallback'}
|
| 36 |
+
|
| 37 |
+
def build_marc_record(data, isbn):
|
| 38 |
+
return ["001 ## $a ISBN" + isbn]
|
| 39 |
+
|
| 40 |
+
def generate_marc_iso2709(marc_fields, isbn):
|
| 41 |
+
return {'iso2709': '', 'human_readable': '', 'fields_count': 0, 'record_length': 0}
|
| 42 |
+
|
| 43 |
+
def generate_marcxml(marc_fields, isbn):
|
| 44 |
+
return "<?xml version='1.0'?>"
|
| 45 |
+
|
| 46 |
+
# --- 1. SETUP & AUTHENTICATION ---
|
| 47 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 48 |
+
|
| 49 |
+
st.set_page_config(
|
| 50 |
+
page_title="Medical AI Librarian",
|
| 51 |
+
layout="wide",
|
| 52 |
+
page_icon="🏥",
|
| 53 |
+
initial_sidebar_state="collapsed"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
client = InferenceClient(model="Qwen/Qwen2.5-72B-Instruct", token=hf_token)
|
| 58 |
+
except:
|
| 59 |
+
client = None
|
| 60 |
|
| 61 |
# --- 2. CSS STYLING ---
|
| 62 |
st.markdown("""
|
|
|
|
| 385 |
else:
|
| 386 |
data['authors'] = []
|
| 387 |
|
| 388 |
+
# إضافة حقول إضافية مهمة
|
| 389 |
+
data['illustrations_note'] = 'illustrations (chiefly color), portraits'
|
| 390 |
+
data['series'] = 'Medical education series' if 'textbook' in data.get('title', '').lower() else ''
|
| 391 |
+
data['institution'] = enhanced_meta.get('institution', '')
|
| 392 |
+
|
| 393 |
+
# تحسين بيانات الصفحات
|
| 394 |
+
if not data.get('pages') and enhanced_meta.get('page_count'):
|
| 395 |
+
page_count = enhanced_meta['page_count']
|
| 396 |
+
if isinstance(page_count, int):
|
| 397 |
+
if page_count > 100:
|
| 398 |
+
data['pages'] = f"xvi, {page_count}"
|
| 399 |
+
else:
|
| 400 |
+
data['pages'] = str(page_count)
|
| 401 |
+
|
| 402 |
+
# إضافة تصنيفات إضافية
|
| 403 |
+
text_to_analyze = f"{api_title} {summary_context} {web_context_text}".lower()
|
| 404 |
+
if 'anatomy' in text_to_analyze:
|
| 405 |
+
data['lccn'] = 'QM23.2'
|
| 406 |
+
elif 'pharmacology' in text_to_analyze:
|
| 407 |
+
data['lccn'] = 'RM300'
|
| 408 |
+
elif 'surgery' in text_to_analyze:
|
| 409 |
+
data['lccn'] = 'RD31'
|
| 410 |
+
elif 'medicine' in text_to_analyze:
|
| 411 |
+
data['lccn'] = 'R130'
|
| 412 |
+
|
| 413 |
return data
|
| 414 |
|
| 415 |
except Exception as e:
|
|
|
|
| 427 |
'summary': 'AI analysis incomplete. Using enhanced local classification.',
|
| 428 |
'acquisition_decision': 'Review Required',
|
| 429 |
'quality_score': 5,
|
| 430 |
+
'data_accuracy': 'Medium',
|
| 431 |
+
'illustrations_note': 'illustrations',
|
| 432 |
+
'pages': 'xii, 500'
|
| 433 |
}
|
| 434 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
# --- 4. MAIN UI ---
|
| 436 |
if 'analysis_data' not in st.session_state:
|
| 437 |
st.session_state.analysis_data = None
|
|
|
|
| 699 |
|
| 700 |
with tab3:
|
| 701 |
st.markdown('<div class="modern-card">', unsafe_allow_html=True)
|
| 702 |
+
st.markdown("### 💻 MARC21 Record - Complete Edition")
|
| 703 |
|
| 704 |
marc_lines = build_marc_record(ai, data['isbn'])
|
| 705 |
marc_text = "\n".join(marc_lines)
|
| 706 |
+
|
| 707 |
+
# إنشاء تنسيق ISO 2709
|
| 708 |
+
iso_record = generate_marc_iso2709(marc_lines, data['isbn'])
|
| 709 |
|
| 710 |
col1, col2 = st.columns(2)
|
| 711 |
+
with col1:
|
| 712 |
+
st.code(marc_text, language="marc")
|
| 713 |
+
with col2:
|
| 714 |
+
st.code(iso_record['iso2709'], language="text")
|
| 715 |
+
st.caption(f"ISO 2709 Record - {iso_record['record_length']} bytes")
|
| 716 |
+
|
| 717 |
+
# إنشاء MARCXML
|
| 718 |
+
marcxml_text = generate_marcxml(marc_lines, data['isbn'])
|
| 719 |
+
|
| 720 |
+
# أزرار التحميل
|
| 721 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 722 |
with col1:
|
| 723 |
st.download_button(
|
| 724 |
+
label="📥 MARC21 (.mrc)",
|
| 725 |
data=marc_text,
|
| 726 |
file_name=f"{data['isbn']}.mrc",
|
| 727 |
+
mime="text/plain",
|
| 728 |
+
use_container_width=True
|
| 729 |
)
|
| 730 |
with col2:
|
| 731 |
st.download_button(
|
| 732 |
+
label="📊 ISO 2709 (.iso)",
|
| 733 |
+
data=iso_record['iso2709'].encode('utf-8'),
|
| 734 |
+
file_name=f"{data['isbn']}_iso2709.iso",
|
| 735 |
+
mime="application/octet-stream",
|
| 736 |
+
use_container_width=True
|
| 737 |
+
)
|
| 738 |
+
with col3:
|
| 739 |
+
st.download_button(
|
| 740 |
+
label="📋 JSON Metadata",
|
| 741 |
+
data=json.dumps(data, indent=2, ensure_ascii=False),
|
| 742 |
+
file_name=f"{data['isbn']}_complete.json",
|
| 743 |
+
mime="application/json",
|
| 744 |
+
use_container_width=True
|
| 745 |
+
)
|
| 746 |
+
with col4:
|
| 747 |
+
st.download_button(
|
| 748 |
+
label="📑 XML/MARCXML",
|
| 749 |
+
data=marcxml_text,
|
| 750 |
+
file_name=f"{data['isbn']}.xml",
|
| 751 |
+
mime="application/xml",
|
| 752 |
+
use_container_width=True
|
| 753 |
)
|
| 754 |
st.markdown('</div>', unsafe_allow_html=True)
|
| 755 |
|