|
import gradio as gr |
|
from transformers import pipeline |
|
import PyPDF2 |
|
import pandas as pd |
|
from fpdf import FPDF |
|
from datetime import datetime |
|
import langdetect |
|
|
|
|
|
fa_summarizer = pipeline("text2text-generation", model="google/mt5-small") |
|
en_summarizer = pipeline("summarization", model="facebook/bart-large-cnn") |
|
|
|
|
|
def detect_language(text): |
|
try: |
|
lang = langdetect.detect(text) |
|
return "fa" if lang == "fa" else "en" |
|
except: |
|
return "fa" |
|
|
|
|
|
def summarize_text(text): |
|
if not text.strip(): |
|
return "⚠️ لطفاً متن وارد کنید." |
|
|
|
lang = detect_language(text) |
|
if lang == "fa": |
|
prompt = f"لطفاً این متن را خلاصه کن:\n{text}" |
|
result = fa_summarizer(prompt, max_length=150, min_length=30, do_sample=False) |
|
return result[0]["generated_text"] |
|
else: |
|
result = en_summarizer(text, max_length=150, min_length=30, do_sample=False) |
|
return result[0]["summary_text"] |
|
|
|
|
|
def summarize_pdf(file_path): |
|
try: |
|
reader = PyPDF2.PdfReader(file_path) |
|
text = "" |
|
for page in reader.pages: |
|
txt = page.extract_text() |
|
if txt: |
|
text += txt + "\n" |
|
return summarize_text(text) |
|
except Exception as e: |
|
return f"❌ خطا در خواندن PDF: {e}" |
|
|
|
|
|
def save_to_pdf(text, summary): |
|
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf" |
|
pdf = FPDF() |
|
pdf.add_page() |
|
pdf.set_font("Arial", size=12) |
|
pdf.multi_cell(0, 10, f"📄 Original Text:\n\n{text}\n\n---\n\n📝 Summary:\n\n{summary}") |
|
pdf.output(filename) |
|
return filename |
|
|
|
|
|
def save_to_excel(text, summary): |
|
filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx" |
|
df = pd.DataFrame({"Original Text": [text], "Summary": [summary]}) |
|
df.to_excel(filename, index=False) |
|
return filename |
|
|
|
|
|
with gr.Blocks(css=""" |
|
body { font-family: Vazir, sans-serif; background: linear-gradient(120deg, #6a4cff, #00c9b7); } |
|
h1 { font-weight: bold; color: white; text-align: center; padding: 20px; |
|
background: rgba(255,255,255,0.08); border-radius: 8px; } |
|
.tab { background-color: white; border-radius: 12px; padding: 20px; |
|
box-shadow: 0px 4px 15px rgba(0,0,0,0.1); } |
|
button { border-radius: 8px !important; font-weight: bold; } |
|
""") as demo: |
|
|
|
gr.HTML("<h1>📝 SummarizeX Pro — Persian & English Text Summarizer</h1>") |
|
|
|
with gr.Tab("🖊 خلاصهسازی متن"): |
|
text_input = gr.Textbox(lines=10, placeholder="Paste your Persian or English text here...") |
|
summary_output = gr.Textbox(lines=8, label="Summary") |
|
btn_summary = gr.Button("✨ Summarize") |
|
|
|
pdf_btn = gr.Button("📄 Save to PDF") |
|
excel_btn = gr.Button("📊 Save to Excel") |
|
file_pdf_out = gr.File(label="Download PDF") |
|
file_excel_out = gr.File(label="Download Excel") |
|
|
|
btn_summary.click(summarize_text, inputs=text_input, outputs=summary_output) |
|
pdf_btn.click(lambda t, s: save_to_pdf(t, s), inputs=[text_input, summary_output], outputs=file_pdf_out) |
|
excel_btn.click(lambda t, s: save_to_excel(t, s), inputs=[text_input, summary_output], outputs=file_excel_out) |
|
|
|
with gr.Tab("📂 خلاصهسازی PDF"): |
|
pdf_input = gr.File(type="filepath", file_types=[".pdf"], label="Upload PDF File") |
|
pdf_summary_output = gr.Textbox(lines=8, label="Summary") |
|
btn_pdf_summary = gr.Button("✨ Summarize PDF") |
|
|
|
pdf_btn2 = gr.Button("📄 Save to PDF") |
|
excel_btn2 = gr.Button("📊 Save to Excel") |
|
file_pdf_out2 = gr.File(label="Download PDF") |
|
file_excel_out2 = gr.File(label="Download Excel") |
|
|
|
btn_pdf_summary.click(summarize_pdf, inputs=pdf_input, outputs=pdf_summary_output) |
|
pdf_btn2.click(lambda f, s: save_to_pdf("PDF File", s), inputs=[pdf_input, pdf_summary_output], outputs=file_pdf_out2) |
|
excel_btn2.click(lambda f, s: save_to_excel("PDF File", s), inputs=[pdf_input, pdf_summary_output], outputs=file_excel_out2) |
|
|
|
if __name__ == "__main__": |
|
demo.launch(server_name="0.0.0.0", server_port=7860) |