Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
import fitz # PyMuPDF
|
| 7 |
+
from core import analyze_or_save # ⚠️ اینجا اصلاح شده: از core import میشود، نه ilia4
|
| 8 |
+
|
| 9 |
+
# ---------- تنظیمات فارسی و راستچین ----------
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="تحلیلگر جلد کتاب",
|
| 12 |
+
layout="centered",
|
| 13 |
+
initial_sidebar_state="auto"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
st.markdown("""
|
| 17 |
+
<style>
|
| 18 |
+
body, .css-1d391kg, .css-1v3fvcr, .css-1lcbmhc, .css-qbe2hs, .css-1kyxreq {
|
| 19 |
+
direction: rtl;
|
| 20 |
+
text-align: right;
|
| 21 |
+
}
|
| 22 |
+
</style>
|
| 23 |
+
""", unsafe_allow_html=True)
|
| 24 |
+
|
| 25 |
+
st.title("📚 تحلیلگر جلد کتابهای چندجلدی")
|
| 26 |
+
|
| 27 |
+
threshold = st.sidebar.slider("آستانه تشابه (٪)", 50, 99, 90) / 100.0
|
| 28 |
+
st.sidebar.markdown("هرچه آستانه بالاتر باشد، تشابه سختگیرانهتر است.")
|
| 29 |
+
|
| 30 |
+
uploaded_files = st.file_uploader(
|
| 31 |
+
"📤 PDFهای کتاب را انتخاب کنید (چندانتخابی فعال است)",
|
| 32 |
+
type=["pdf"],
|
| 33 |
+
accept_multiple_files=True
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if uploaded_files:
|
| 37 |
+
st.markdown("### نامگذاری کتابها (اختیاری)")
|
| 38 |
+
names = []
|
| 39 |
+
for idx, file in enumerate(uploaded_files):
|
| 40 |
+
name = st.text_input(f"نام کتاب {idx+1} (اگر خالی بماند، نام فایل استفاده میشود)", key=f"name_{idx}")
|
| 41 |
+
names.append(name.strip() if name.strip() else None)
|
| 42 |
+
|
| 43 |
+
if st.button("🚀 تحلیل جلدها"):
|
| 44 |
+
for idx, (uploaded_file, custom_name) in enumerate(zip(uploaded_files, names)):
|
| 45 |
+
with st.spinner(f"در حال تحلیل {uploaded_file.name}..."):
|
| 46 |
+
temp_path = f"temp_{idx}.pdf"
|
| 47 |
+
with open(temp_path, "wb") as f:
|
| 48 |
+
f.write(uploaded_file.getbuffer())
|
| 49 |
+
|
| 50 |
+
# تبدیل صفحه اول به تصویر
|
| 51 |
+
doc = fitz.open(temp_path)
|
| 52 |
+
page = doc.load_page(0)
|
| 53 |
+
pix = page.get_pixmap()
|
| 54 |
+
img_data = pix.tobytes("ppm")
|
| 55 |
+
img = Image.open(io.BytesIO(img_data)).convert("RGB")
|
| 56 |
+
doc.close()
|
| 57 |
+
|
| 58 |
+
# تحلیل
|
| 59 |
+
result = analyze_or_save(temp_path, img, custom_name)
|
| 60 |
+
|
| 61 |
+
# نمایش نتیجه
|
| 62 |
+
col1, col2 = st.columns([1, 2])
|
| 63 |
+
with col1:
|
| 64 |
+
st.image(img, caption="جلد کتاب", use_column_width=True)
|
| 65 |
+
with col2:
|
| 66 |
+
if result["status"] == "new":
|
| 67 |
+
st.success(f"✅ جلد جدید ذخیره شد.")
|
| 68 |
+
st.info(f"📁 نام ذخیرهشده: {result['saved_path']}")
|
| 69 |
+
st.write(f"میزان تشابه با نزدیکترین مورد: {result['similarity']:.2f}%")
|
| 70 |
+
else:
|
| 71 |
+
st.warning(f"⚠️ جلد تکراری شناسایی شد.")
|
| 72 |
+
st.write(f"میزان تشابه: {result['similarity']:.2f}%")
|
| 73 |
+
st.info(f"📁 نام فایل مشابه: {result['similar_path']}")
|
| 74 |
+
similar_path = result["similar_path"]
|
| 75 |
+
if os.path.exists(similar_path + ".png"):
|
| 76 |
+
st.image(similar_path + ".png", caption="جلد مشابه", use_column_width=True)
|
| 77 |
+
|
| 78 |
+
# حذف فایل موقت
|
| 79 |
+
if os.path.exists(temp_path):
|
| 80 |
+
os.remove(temp_path)
|
| 81 |
+
|
| 82 |
+
st.markdown("---")
|