Update app.py
Browse files
app.py
CHANGED
|
@@ -5,24 +5,23 @@ import pandas as pd
|
|
| 5 |
from fpdf import FPDF
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
-
# 🎯 مدل عمومی
|
| 9 |
-
# با استفاده از mt5-small و prompt فارسی
|
| 10 |
summarizer = pipeline(
|
| 11 |
"text2text-generation",
|
| 12 |
-
model="
|
| 13 |
-
tokenizer="google/mt5-small"
|
| 14 |
)
|
| 15 |
|
|
|
|
| 16 |
def summarize_text(text):
|
| 17 |
if not text.strip():
|
| 18 |
return "⚠️ لطفاً متن وارد کنید."
|
| 19 |
-
|
| 20 |
-
result = summarizer(prompt, max_length=150, min_length=30, do_sample=False)
|
| 21 |
return result[0]["generated_text"]
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
try:
|
| 25 |
-
reader = PyPDF2.PdfReader(
|
| 26 |
text = ""
|
| 27 |
for page in reader.pages:
|
| 28 |
txt = page.extract_text()
|
|
@@ -32,6 +31,7 @@ def summarize_pdf(file):
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"❌ خطا در خواندن PDF: {e}"
|
| 34 |
|
|
|
|
| 35 |
def save_to_pdf(text, summary):
|
| 36 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 37 |
pdf = FPDF()
|
|
@@ -41,12 +41,14 @@ def save_to_pdf(text, summary):
|
|
| 41 |
pdf.output(filename)
|
| 42 |
return filename
|
| 43 |
|
|
|
|
| 44 |
def save_to_excel(text, summary):
|
| 45 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
| 46 |
df = pd.DataFrame({"متن اصلی": [text], "خلاصه": [summary]})
|
| 47 |
df.to_excel(filename, index=False)
|
| 48 |
return filename
|
| 49 |
|
|
|
|
| 50 |
with gr.Blocks(css="""
|
| 51 |
body { font-family: Vazir, sans-serif; background: #f9fafb; }
|
| 52 |
h1 { font-weight: bold; color: white; text-align: center; padding: 20px;
|
|
@@ -56,8 +58,7 @@ with gr.Blocks(css="""
|
|
| 56 |
button { border-radius: 8px !important; font-weight: bold; }
|
| 57 |
""") as demo:
|
| 58 |
|
| 59 |
-
|
| 60 |
-
gr.Markdown("<h1>📝 SummarizeX — خلاصهساز متن و PDF (نسخه عمومی)</h1>")
|
| 61 |
|
| 62 |
with gr.Tab("خلاصه متن"):
|
| 63 |
text_input = gr.Textbox(lines=10, placeholder="متن خود را اینجا وارد کنید...")
|
|
@@ -74,7 +75,7 @@ with gr.Blocks(css="""
|
|
| 74 |
excel_btn.click(lambda t, s: save_to_excel(t, s), inputs=[text_input, summary_output], outputs=file_excel_out)
|
| 75 |
|
| 76 |
with gr.Tab("خلاصه PDF"):
|
| 77 |
-
pdf_input = gr.File(type="
|
| 78 |
pdf_summary_output = gr.Textbox(lines=8, label="خلاصه PDF")
|
| 79 |
btn_pdf_summary = gr.Button("✨ خلاصه PDF")
|
| 80 |
|
|
|
|
| 5 |
from fpdf import FPDF
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
+
# 🎯 مدل عمومی سبک خلاصهسازی فارسی
|
|
|
|
| 9 |
summarizer = pipeline(
|
| 10 |
"text2text-generation",
|
| 11 |
+
model="persiannlp/mt5-small-parsinlu-summarization"
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# تابع خلاصهسازی متن
|
| 15 |
def summarize_text(text):
|
| 16 |
if not text.strip():
|
| 17 |
return "⚠️ لطفاً متن وارد کنید."
|
| 18 |
+
result = summarizer(text, max_length=150, min_length=30, do_sample=False)
|
|
|
|
| 19 |
return result[0]["generated_text"]
|
| 20 |
|
| 21 |
+
# تابع خلاصهسازی PDF
|
| 22 |
+
def summarize_pdf(file_path):
|
| 23 |
try:
|
| 24 |
+
reader = PyPDF2.PdfReader(file_path)
|
| 25 |
text = ""
|
| 26 |
for page in reader.pages:
|
| 27 |
txt = page.extract_text()
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
return f"❌ خطا در خواندن PDF: {e}"
|
| 33 |
|
| 34 |
+
# ذخیره به PDF
|
| 35 |
def save_to_pdf(text, summary):
|
| 36 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 37 |
pdf = FPDF()
|
|
|
|
| 41 |
pdf.output(filename)
|
| 42 |
return filename
|
| 43 |
|
| 44 |
+
# ذخیره به Excel
|
| 45 |
def save_to_excel(text, summary):
|
| 46 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
| 47 |
df = pd.DataFrame({"متن اصلی": [text], "خلاصه": [summary]})
|
| 48 |
df.to_excel(filename, index=False)
|
| 49 |
return filename
|
| 50 |
|
| 51 |
+
# 🎨 رابط کاربری
|
| 52 |
with gr.Blocks(css="""
|
| 53 |
body { font-family: Vazir, sans-serif; background: #f9fafb; }
|
| 54 |
h1 { font-weight: bold; color: white; text-align: center; padding: 20px;
|
|
|
|
| 58 |
button { border-radius: 8px !important; font-weight: bold; }
|
| 59 |
""") as demo:
|
| 60 |
|
| 61 |
+
gr.Markdown("<h1>📝 SummarizeX — خلاصهساز متن و PDF (نسخه عمومی سبک)</h1>")
|
|
|
|
| 62 |
|
| 63 |
with gr.Tab("خلاصه متن"):
|
| 64 |
text_input = gr.Textbox(lines=10, placeholder="متن خود را اینجا وارد کنید...")
|
|
|
|
| 75 |
excel_btn.click(lambda t, s: save_to_excel(t, s), inputs=[text_input, summary_output], outputs=file_excel_out)
|
| 76 |
|
| 77 |
with gr.Tab("خلاصه PDF"):
|
| 78 |
+
pdf_input = gr.File(type="filepath", file_types=[".pdf"], label="انتخاب فایل PDF")
|
| 79 |
pdf_summary_output = gr.Textbox(lines=8, label="خلاصه PDF")
|
| 80 |
btn_pdf_summary = gr.Button("✨ خلاصه PDF")
|
| 81 |
|