BabakBagheriGisour
commited on
Commit
•
8402dc2
1
Parent(s):
b76236a
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,107 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
|
8 |
-
# بارگذاری مدل
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")
|
13 |
-
return tokenizer, model
|
14 |
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
#
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
for page in
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
st.text_area("Vollständiger Text der PDF-Datei:", pdf_text, height=300)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
st.info(f"**Mögliches Thema der Datei:** {topic}")
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
progress_bar = st.progress(0)
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
input_ids = tokenizer.encode(line, return_tensors="pt", truncation=True, max_length=1024)
|
55 |
-
|
56 |
-
# تولید خلاصه برای هر خط
|
57 |
-
try:
|
58 |
-
summary_ids = model.generate(
|
59 |
-
input_ids=input_ids,
|
60 |
-
num_beams=2,
|
61 |
-
max_length=50, # حداکثر طول خ��اصه
|
62 |
-
min_length=10 # حداقل طول خلاصه، در صورت نیاز قابل تنظیم است
|
63 |
-
)
|
64 |
-
decoded_summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
65 |
-
final_summary += decoded_summary + " "
|
66 |
-
except Exception as e:
|
67 |
-
st.warning(f"Fehler bei Zeile {idx + 1}: {e}")
|
68 |
-
|
69 |
-
# نمایش خلاصه فعلی
|
70 |
-
st.write(f"**Zeile {idx + 1}:** {decoded_summary}")
|
71 |
-
|
72 |
-
# بهروزرسانی نوار پیشرفت
|
73 |
-
progress_bar.progress((idx + 1) / num_lines)
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
#
|
77 |
-
|
78 |
-
|
|
|
1 |
import streamlit as st
|
2 |
+
import pytesseract
|
3 |
+
from pdf2image import convert_from_path
|
4 |
+
import os
|
5 |
+
import re
|
6 |
+
import json
|
7 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
8 |
+
from tqdm import tqdm
|
9 |
|
10 |
+
# تنظیم Tesseract
|
11 |
+
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
|
12 |
|
13 |
+
# بارگذاری مدل سفارشی
|
14 |
+
model_name = "BabakBagheriGisour/NetworkPlus"
|
15 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
|
17 |
|
18 |
+
# بررسی GPU
|
19 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
+
model.to(device)
|
21 |
|
22 |
+
# پاکسازی متن
|
23 |
+
def clean_text(text):
|
24 |
+
text = re.sub(r'\s+', ' ', text) # حذف فضاهای اضافی
|
25 |
+
text = re.sub(r'http\S+|www\.[\w.-]+', '', text) # حذف لینکها
|
26 |
+
text = re.sub(r'[^\w\sÄäÖöÜüß]+', '', text) # حذف علائم غیر ضروری
|
27 |
+
return text.strip()
|
28 |
|
29 |
+
# استخراج متن از فایل PDF
|
30 |
+
def extract_text_using_ocr(pdf_path):
|
31 |
+
pages = convert_from_path(pdf_path, 300) # تبدیل PDF به تصاویر
|
32 |
+
all_text = []
|
33 |
+
for page in pages:
|
34 |
+
text = pytesseract.image_to_string(page, lang="deu") # زبان آلمانی
|
35 |
+
all_text.append(clean_text(text))
|
36 |
+
return all_text
|
|
|
37 |
|
38 |
+
# خلاصهسازی با مدل سفارشی
|
39 |
+
def summarize_text(text):
|
40 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True, padding=True).to(device)
|
41 |
+
summary_ids = model.generate(inputs['input_ids'], num_beams=4, max_length=100, early_stopping=True)
|
42 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
43 |
+
return summary
|
44 |
|
45 |
+
# پردازش PDF و تولید JSONL
|
46 |
+
def process_pdf_to_jsonl(pdf_file, progress_bar):
|
47 |
+
pages_text = extract_text_using_ocr(pdf_file)
|
48 |
+
total_lines = sum(len(page.splitlines()) for page in pages_text)
|
49 |
+
total_words = sum(len(page.split()) for page in pages_text)
|
50 |
+
|
51 |
+
data = []
|
52 |
+
for idx, page_text in enumerate(pages_text):
|
53 |
+
summary = summarize_text(page_text)
|
54 |
+
data.append({
|
55 |
+
"text": page_text,
|
56 |
+
"zusammenfassen": summary
|
57 |
+
})
|
58 |
+
# بهروزرسانی نوار پیشرفت
|
59 |
+
progress_bar.progress((idx + 1) / len(pages_text))
|
60 |
+
|
61 |
+
return data, len(pages_text), total_lines, total_words
|
62 |
|
63 |
+
# رابط کاربری با Streamlit
|
64 |
+
st.title("PDF to JSONL Converter with BabakBagheriGisour/NetworkPlus")
|
65 |
+
uploaded_file = st.file_uploader("Bitte laden Sie eine PDF-Datei hoch", type="pdf")
|
|
|
66 |
|
67 |
+
if uploaded_file:
|
68 |
+
# ذخیره فایل آپلود شده
|
69 |
+
temp_file_path = f"temp_{uploaded_file.name}"
|
70 |
+
with open(temp_file_path, "wb") as f:
|
71 |
+
f.write(uploaded_file.read())
|
72 |
+
|
73 |
+
# پردازش فایل
|
74 |
+
st.info("Das Modell verarbeitet den Text...")
|
75 |
progress_bar = st.progress(0)
|
76 |
+
data, total_pages, total_lines, total_words = process_pdf_to_jsonl(temp_file_path, progress_bar)
|
77 |
|
78 |
+
# نمایش اطلاعات فایل
|
79 |
+
st.subheader("Dateiinformationen:")
|
80 |
+
st.write(f"**Anzahl der Seiten:** {total_pages}")
|
81 |
+
st.write(f"**Anzahl der Zeilen:** {total_lines}")
|
82 |
+
st.write(f"**Anzahl der Wörter:** {total_words}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
+
# نمایش خلاصهها
|
85 |
+
st.subheader("Zusammenfassungen:")
|
86 |
+
for idx, item in enumerate(data):
|
87 |
+
st.write(f"**Seite {idx + 1}:**")
|
88 |
+
st.text_area("Originaltext", item["text"], height=200)
|
89 |
+
st.text_area("Zusammenfassung", item["zusammenfassen"], height=100)
|
90 |
+
|
91 |
+
# ذخیره به JSONL
|
92 |
+
output_file = f"{uploaded_file.name.split('.')[0]}.jsonl"
|
93 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
94 |
+
for item in data:
|
95 |
+
f.write(json.dumps(item, ensure_ascii=False) + '\n')
|
96 |
+
|
97 |
+
st.success("Verarbeitung abgeschlossen!")
|
98 |
+
st.download_button(
|
99 |
+
label="Download JSONL",
|
100 |
+
data=open(output_file, "rb").read(),
|
101 |
+
file_name=output_file,
|
102 |
+
mime="application/jsonl"
|
103 |
+
)
|
104 |
|
105 |
+
# حذف فایلهای موقت
|
106 |
+
os.remove(temp_file_path)
|
107 |
+
os.remove(output_file)
|