Update app.py
Browse files
app.py
CHANGED
|
@@ -4,20 +4,33 @@ import PyPDF2
|
|
| 4 |
import pandas as pd
|
| 5 |
from fpdf import FPDF
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
-
# 🎯
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
model="google/mt5-small"
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def summarize_text(text):
|
| 16 |
if not text.strip():
|
| 17 |
return "⚠️ لطفاً متن وارد کنید."
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# خلاصهسازی PDF
|
| 23 |
def summarize_pdf(file_path):
|
|
@@ -32,58 +45,58 @@ def summarize_pdf(file_path):
|
|
| 32 |
except Exception as e:
|
| 33 |
return f"❌ خطا در خواندن PDF: {e}"
|
| 34 |
|
| 35 |
-
#
|
| 36 |
def save_to_pdf(text, summary):
|
| 37 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 38 |
pdf = FPDF()
|
| 39 |
pdf.add_page()
|
| 40 |
pdf.set_font("Arial", size=12)
|
| 41 |
-
pdf.multi_cell(0, 10, f"📄
|
| 42 |
pdf.output(filename)
|
| 43 |
return filename
|
| 44 |
|
| 45 |
-
#
|
| 46 |
def save_to_excel(text, summary):
|
| 47 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
| 48 |
-
df = pd.DataFrame({"
|
| 49 |
df.to_excel(filename, index=False)
|
| 50 |
return filename
|
| 51 |
|
| 52 |
-
# رابط کاربری
|
| 53 |
with gr.Blocks(css="""
|
| 54 |
-
body { font-family: Vazir, sans-serif; background: #
|
| 55 |
h1 { font-weight: bold; color: white; text-align: center; padding: 20px;
|
| 56 |
-
background:
|
| 57 |
-
.tab { background-color: white;
|
| 58 |
-
box-shadow: 0px
|
| 59 |
button { border-radius: 8px !important; font-weight: bold; }
|
| 60 |
""") as demo:
|
| 61 |
|
| 62 |
-
gr.
|
| 63 |
|
| 64 |
-
with gr.Tab("
|
| 65 |
-
text_input = gr.Textbox(lines=10, placeholder="
|
| 66 |
-
summary_output = gr.Textbox(lines=8, label="
|
| 67 |
-
btn_summary = gr.Button("✨
|
| 68 |
-
|
| 69 |
-
pdf_btn = gr.Button("📄
|
| 70 |
-
excel_btn = gr.Button("📊
|
| 71 |
-
file_pdf_out = gr.File(label="
|
| 72 |
-
file_excel_out = gr.File(label="
|
| 73 |
|
| 74 |
btn_summary.click(summarize_text, inputs=text_input, outputs=summary_output)
|
| 75 |
pdf_btn.click(lambda t, s: save_to_pdf(t, s), inputs=[text_input, summary_output], outputs=file_pdf_out)
|
| 76 |
excel_btn.click(lambda t, s: save_to_excel(t, s), inputs=[text_input, summary_output], outputs=file_excel_out)
|
| 77 |
|
| 78 |
-
with gr.Tab("
|
| 79 |
-
pdf_input = gr.File(type="filepath", file_types=[".pdf"], label="
|
| 80 |
-
pdf_summary_output = gr.Textbox(lines=8, label="
|
| 81 |
-
btn_pdf_summary = gr.Button("✨
|
| 82 |
-
|
| 83 |
-
pdf_btn2 = gr.Button("📄
|
| 84 |
-
excel_btn2 = gr.Button("📊
|
| 85 |
-
file_pdf_out2 = gr.File(label="
|
| 86 |
-
file_excel_out2 = gr.File(label="
|
| 87 |
|
| 88 |
btn_pdf_summary.click(summarize_pdf, inputs=pdf_input, outputs=pdf_summary_output)
|
| 89 |
pdf_btn2.click(lambda f, s: save_to_pdf("PDF File", s), inputs=[pdf_input, pdf_summary_output], outputs=file_pdf_out2)
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
from fpdf import FPDF
|
| 6 |
from datetime import datetime
|
| 7 |
+
import langdetect
|
| 8 |
|
| 9 |
+
# 🎯 مدلهای عمومی سبک برای فارسی و انگلیسی
|
| 10 |
+
fa_summarizer = pipeline("text2text-generation", model="google/mt5-small")
|
| 11 |
+
en_summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# تشخیص زبان
|
| 14 |
+
def detect_language(text):
|
| 15 |
+
try:
|
| 16 |
+
lang = langdetect.detect(text)
|
| 17 |
+
return "fa" if lang == "fa" else "en"
|
| 18 |
+
except:
|
| 19 |
+
return "fa"
|
| 20 |
+
|
| 21 |
+
# خلاصهسازی متن (دو زبانه)
|
| 22 |
def summarize_text(text):
|
| 23 |
if not text.strip():
|
| 24 |
return "⚠️ لطفاً متن وارد کنید."
|
| 25 |
+
|
| 26 |
+
lang = detect_language(text)
|
| 27 |
+
if lang == "fa":
|
| 28 |
+
prompt = f"لطفاً این متن را خلاصه کن:\n{text}"
|
| 29 |
+
result = fa_summarizer(prompt, max_length=150, min_length=30, do_sample=False)
|
| 30 |
+
return result[0]["generated_text"]
|
| 31 |
+
else:
|
| 32 |
+
result = en_summarizer(text, max_length=150, min_length=30, do_sample=False)
|
| 33 |
+
return result[0]["summary_text"]
|
| 34 |
|
| 35 |
# خلاصهسازی PDF
|
| 36 |
def summarize_pdf(file_path):
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"❌ خطا در خواندن PDF: {e}"
|
| 47 |
|
| 48 |
+
# ذخیرهسازی به PDF
|
| 49 |
def save_to_pdf(text, summary):
|
| 50 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
|
| 51 |
pdf = FPDF()
|
| 52 |
pdf.add_page()
|
| 53 |
pdf.set_font("Arial", size=12)
|
| 54 |
+
pdf.multi_cell(0, 10, f"📄 Original Text:\n\n{text}\n\n---\n\n📝 Summary:\n\n{summary}")
|
| 55 |
pdf.output(filename)
|
| 56 |
return filename
|
| 57 |
|
| 58 |
+
# ذخیرهسازی به Excel
|
| 59 |
def save_to_excel(text, summary):
|
| 60 |
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
|
| 61 |
+
df = pd.DataFrame({"Original Text": [text], "Summary": [summary]})
|
| 62 |
df.to_excel(filename, index=False)
|
| 63 |
return filename
|
| 64 |
|
| 65 |
+
# 🎨 رابط کاربری
|
| 66 |
with gr.Blocks(css="""
|
| 67 |
+
body { font-family: Vazir, sans-serif; background: linear-gradient(120deg, #6a4cff, #00c9b7); }
|
| 68 |
h1 { font-weight: bold; color: white; text-align: center; padding: 20px;
|
| 69 |
+
background: rgba(255,255,255,0.08); border-radius: 8px; }
|
| 70 |
+
.tab { background-color: white; border-radius: 12px; padding: 20px;
|
| 71 |
+
box-shadow: 0px 4px 15px rgba(0,0,0,0.1); }
|
| 72 |
button { border-radius: 8px !important; font-weight: bold; }
|
| 73 |
""") as demo:
|
| 74 |
|
| 75 |
+
gr.HTML("<h1>📝 SummarizeX Pro — Persian & English Text Summarizer</h1>")
|
| 76 |
|
| 77 |
+
with gr.Tab("🖊 خلاصهسازی متن"):
|
| 78 |
+
text_input = gr.Textbox(lines=10, placeholder="Paste your Persian or English text here...")
|
| 79 |
+
summary_output = gr.Textbox(lines=8, label="Summary")
|
| 80 |
+
btn_summary = gr.Button("✨ Summarize")
|
| 81 |
+
|
| 82 |
+
pdf_btn = gr.Button("📄 Save to PDF")
|
| 83 |
+
excel_btn = gr.Button("📊 Save to Excel")
|
| 84 |
+
file_pdf_out = gr.File(label="Download PDF")
|
| 85 |
+
file_excel_out = gr.File(label="Download Excel")
|
| 86 |
|
| 87 |
btn_summary.click(summarize_text, inputs=text_input, outputs=summary_output)
|
| 88 |
pdf_btn.click(lambda t, s: save_to_pdf(t, s), inputs=[text_input, summary_output], outputs=file_pdf_out)
|
| 89 |
excel_btn.click(lambda t, s: save_to_excel(t, s), inputs=[text_input, summary_output], outputs=file_excel_out)
|
| 90 |
|
| 91 |
+
with gr.Tab("📂 خلاصهسازی PDF"):
|
| 92 |
+
pdf_input = gr.File(type="filepath", file_types=[".pdf"], label="Upload PDF File")
|
| 93 |
+
pdf_summary_output = gr.Textbox(lines=8, label="Summary")
|
| 94 |
+
btn_pdf_summary = gr.Button("✨ Summarize PDF")
|
| 95 |
+
|
| 96 |
+
pdf_btn2 = gr.Button("📄 Save to PDF")
|
| 97 |
+
excel_btn2 = gr.Button("📊 Save to Excel")
|
| 98 |
+
file_pdf_out2 = gr.File(label="Download PDF")
|
| 99 |
+
file_excel_out2 = gr.File(label="Download Excel")
|
| 100 |
|
| 101 |
btn_pdf_summary.click(summarize_pdf, inputs=pdf_input, outputs=pdf_summary_output)
|
| 102 |
pdf_btn2.click(lambda f, s: save_to_pdf("PDF File", s), inputs=[pdf_input, pdf_summary_output], outputs=file_pdf_out2)
|