yasamankm commited on
Commit
7e73e8c
·
verified ·
1 Parent(s): 75a9a6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -19
app.py CHANGED
@@ -1,16 +1,25 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  import PyPDF2
 
 
 
4
 
5
- # لود مدل یکبار هنگام شروع اپ
 
 
 
 
 
6
  summarizer = pipeline(
7
  "summarization",
8
  model="m3hrdadfi/mt5-small-parsinlu-summarization-fa"
9
  )
10
 
 
11
  def summarize_text(text):
12
  if not text.strip():
13
- return "متنی وارد نشده است."
14
  result = summarizer(text, max_length=150, min_length=30)
15
  return result[0]["summary_text"]
16
 
@@ -19,25 +28,74 @@ def summarize_pdf(file):
19
  reader = PyPDF2.PdfReader(file.name)
20
  text = ""
21
  for page in reader.pages:
22
- text += page.extract_text() + "\n"
 
 
23
  return summarize_text(text)
24
  except Exception as e:
25
- return f"خطا در خواندن PDF: {e}"
26
-
27
- with gr.Blocks() as demo:
28
- gr.Markdown("# 📝 SummarizeX — خلاصه‌ساز متن و PDF")
29
-
30
- with gr.Tab("خلاصه متن"):
31
- input_text = gr.Textbox(lines=12, placeholder="متن را اینجا وارد کنید...")
32
- output_summary = gr.Textbox(lines=8, label="خلاصه متن")
33
- btn1 = gr.Button("خلاصه کن")
34
- btn1.click(summarize_text, inputs=input_text, outputs=output_summary)
35
-
36
- with gr.Tab("خلاصه PDF"):
37
- pdf_input = gr.File(type="file", file_types=[".pdf"])
38
- pdf_output = gr.Textbox(lines=8, label="خلاصه PDF")
39
- btn2 = gr.Button("خلاصه PDF")
40
- btn2.click(summarize_pdf, inputs=pdf_input, outputs=pdf_output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  if __name__ == "__main__":
43
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  import PyPDF2
4
+ import pandas as pd
5
+ from fpdf import FPDF
6
+ from datetime import datetime
7
 
8
+ # رنگ‌های برند
9
+ PRIMARY_GRADIENT = "linear-gradient(90deg, #6a4cff, #00c9b7)"
10
+ HEADER_COLOR = "#1f2937"
11
+ TEXT_COLOR = "#111827"
12
+
13
+ # مدل خلاصه‌سازی
14
  summarizer = pipeline(
15
  "summarization",
16
  model="m3hrdadfi/mt5-small-parsinlu-summarization-fa"
17
  )
18
 
19
+ # --- توابع ---
20
  def summarize_text(text):
21
  if not text.strip():
22
+ return "⚠️ لطفاً متن وارد کنید."
23
  result = summarizer(text, max_length=150, min_length=30)
24
  return result[0]["summary_text"]
25
 
 
28
  reader = PyPDF2.PdfReader(file.name)
29
  text = ""
30
  for page in reader.pages:
31
+ txt = page.extract_text()
32
+ if txt:
33
+ text += txt + "\n"
34
  return summarize_text(text)
35
  except Exception as e:
36
+ return f"خطا در خواندن PDF: {e}"
37
+
38
+ def save_to_pdf(text, summary):
39
+ filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.pdf"
40
+ pdf = FPDF()
41
+ pdf.add_page()
42
+ pdf.set_font("Arial", size=12)
43
+ pdf.multi_cell(0, 10, f"📄 متن اصلی:\n\n{text}\n\n---\n\n📝 خلاصه:\n\n{summary}")
44
+ pdf.output(filename)
45
+ return filename
46
+
47
+ def save_to_excel(text, summary):
48
+ filename = f"summary_{datetime.now().strftime('%Y%m%d_%H%M%S')}.xlsx"
49
+ df = pd.DataFrame({"متن اصلی": [text], "خلاصه": [summary]})
50
+ df.to_excel(filename, index=False)
51
+ return filename
52
+
53
+ # --- رابط کاربری ---
54
+ with gr.Blocks(css="""
55
+ body { font-family: Vazir, sans-serif; background: #f9fafb; }
56
+ h1 { font-weight: bold; color: white; text-align: center; padding: 20px;
57
+ background: linear-gradient(90deg, #6a4cff, #00c9b7); border-radius: 8px; }
58
+ .tab { background-color: white; padding: 20px; border-radius: 8px;
59
+ box-shadow: 0px 2px 8px rgba(0,0,0,0.05); }
60
+ button { border-radius: 8px !important; font-weight: bold; }
61
+ """) as demo:
62
+
63
+ with gr.Row():
64
+ gr.Markdown("<h1>📝 SummarizeX — خلاصه‌ساز متن و PDF</h1>")
65
+
66
+ with gr.Tab("خلاصه متن") as tab1:
67
+ with gr.Row(elem_classes="tab"):
68
+ text_input = gr.Textbox(lines=10, placeholder="متن خود را اینجا وارد کنید...")
69
+ summary_output = gr.Textbox(lines=8, label="خلاصه متن")
70
+ with gr.Row():
71
+ btn_summary = gr.Button("✨ خلاصه کن", elem_id="btn-summary")
72
+ with gr.Row():
73
+ pdf_btn = gr.Button("📄 ذخیره به PDF")
74
+ excel_btn = gr.Button("📊 ذخیره به Excel")
75
+ with gr.Row():
76
+ file_pdf_out = gr.File(label="دانلود PDF")
77
+ file_excel_out = gr.File(label="دانلود Excel")
78
+
79
+ btn_summary.click(summarize_text, inputs=text_input, outputs=summary_output)
80
+ pdf_btn.click(lambda t, s: save_to_pdf(t, s), inputs=[text_input, summary_output], outputs=file_pdf_out)
81
+ excel_btn.click(lambda t, s: save_to_excel(t, s), inputs=[text_input, summary_output], outputs=file_excel_out)
82
+
83
+ with gr.Tab("خلاصه PDF") as tab2:
84
+ with gr.Row(elem_classes="tab"):
85
+ pdf_input = gr.File(type="file", file_types=[".pdf"], label="انتخاب فایل PDF")
86
+ pdf_summary_output = gr.Textbox(lines=8, label="خلاصه PDF")
87
+ with gr.Row():
88
+ btn_pdf_summary = gr.Button("✨ خلاصه PDF")
89
+ with gr.Row():
90
+ pdf_btn2 = gr.Button("📄 ذخیره به PDF")
91
+ excel_btn2 = gr.Button("📊 ذخیره به Excel")
92
+ with gr.Row():
93
+ file_pdf_out2 = gr.File(label="دانلود PDF")
94
+ file_excel_out2 = gr.File(label="دانلود Excel")
95
+
96
+ btn_pdf_summary.click(summarize_pdf, inputs=pdf_input, outputs=pdf_summary_output)
97
+ pdf_btn2.click(lambda f, s: save_to_pdf("PDF File", s), inputs=[pdf_input, pdf_summary_output], outputs=file_pdf_out2)
98
+ excel_btn2.click(lambda f, s: save_to_excel("PDF File", s), inputs=[pdf_input, pdf_summary_output], outputs=file_excel_out2)
99
 
100
  if __name__ == "__main__":
101
  demo.launch(server_name="0.0.0.0", server_port=7860)