File size: 4,427 Bytes
b2671d9
 
 
7e73e8c
 
 
a6aff7b
b2671d9
a6aff7b
 
 
b2671d9
a6aff7b
 
 
 
 
 
 
 
 
b2671d9
 
7e73e8c
a6aff7b
 
 
 
 
 
 
 
 
b2671d9
bbde9ea
14fa1b7
b2671d9
14fa1b7
b2671d9
 
7e73e8c
 
 
b2671d9
 
7e73e8c
 
a6aff7b
7e73e8c
 
 
 
 
a6aff7b
7e73e8c
 
 
a6aff7b
7e73e8c
 
a6aff7b
7e73e8c
 
 
a6aff7b
7e73e8c
a6aff7b
7e73e8c
a6aff7b
 
 
7e73e8c
 
 
a6aff7b
7e73e8c
a6aff7b
 
 
 
 
 
 
 
 
7e73e8c
 
 
 
 
a6aff7b
 
 
 
 
 
 
 
 
7e73e8c
 
 
 
b2671d9
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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"]

# خلاصه‌سازی PDF
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}"

# ذخیره‌سازی به PDF
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

# ذخیره‌سازی به Excel
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)