Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from model import (
|
| 4 |
+
images_to_pdf,
|
| 5 |
+
merge_pdfs,
|
| 6 |
+
compress_pdf,
|
| 7 |
+
save_feedback,
|
| 8 |
+
show_feedback
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
css = """
|
| 12 |
+
.title {color:green; font-size:32px; font-weight:bold; text-align:center; margin-bottom:20px;}
|
| 13 |
+
.download-btn {background-color:green !important; color:white !important;}
|
| 14 |
+
.reset-btn {background-color:red !important; color:white !important;}
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
with gr.Blocks(css=css, title="Its Nadish - Image to PDF") as demo:
|
| 18 |
+
gr.HTML('<div class="title">Its Nadish</div>')
|
| 19 |
+
gr.Markdown("### Powerful Image to PDF | Merge | Compress Tool")
|
| 20 |
+
|
| 21 |
+
with gr.Tabs():
|
| 22 |
+
# Tab 1: Images to PDF
|
| 23 |
+
with gr.TabItem("🖼️ Images to PDF"):
|
| 24 |
+
with gr.Row():
|
| 25 |
+
with gr.Column():
|
| 26 |
+
img_input = gr.File(
|
| 27 |
+
file_types=["image"],
|
| 28 |
+
file_count="multiple",
|
| 29 |
+
label="Upload Images (JPG, PNG, etc.)"
|
| 30 |
+
)
|
| 31 |
+
convert_btn = gr.Button("Convert to PDF", variant="primary")
|
| 32 |
+
|
| 33 |
+
with gr.Column():
|
| 34 |
+
p_size = gr.Dropdown(["A4", "Letter", "Legal", "Original"], value="A4", label="Page Size")
|
| 35 |
+
p_orient = gr.Radio(["Portrait", "Landscape"], value="Portrait", label="Orientation")
|
| 36 |
+
p_qual = gr.Dropdown(["Low", "Medium", "High", "Maximum"], value="High", label="Quality")
|
| 37 |
+
p_comp = gr.Checkbox(label="Enable Compression", value=True)
|
| 38 |
+
img_download = gr.DownloadButton("Download PDF", elem_classes="download-btn")
|
| 39 |
+
img_msg = gr.Markdown()
|
| 40 |
+
|
| 41 |
+
# Tab 2: Merge PDFs
|
| 42 |
+
with gr.TabItem("📑 Merge PDFs"):
|
| 43 |
+
merge_input = gr.File(file_types=[".pdf"], file_count="multiple", label="Upload PDF Files")
|
| 44 |
+
merge_btn = gr.Button("Merge Files", variant="primary")
|
| 45 |
+
merge_download = gr.DownloadButton("Download Merged PDF", elem_classes="download-btn")
|
| 46 |
+
comb_msg = gr.Markdown()
|
| 47 |
+
|
| 48 |
+
# Tab 3: Compress PDF
|
| 49 |
+
with gr.TabItem("📉 Compress PDF"):
|
| 50 |
+
comp_file = gr.File(file_types=[".pdf"], label="Upload PDF")
|
| 51 |
+
comp_lvl = gr.Dropdown(
|
| 52 |
+
list(compression_map.keys()), # compression_map model.py se aa raha hai
|
| 53 |
+
value="Balanced (Good Quality + Small Size)",
|
| 54 |
+
label="Compression Level"
|
| 55 |
+
)
|
| 56 |
+
comp_btn = gr.Button("Compress Now", variant="primary")
|
| 57 |
+
comp_download = gr.DownloadButton("Download Compressed PDF")
|
| 58 |
+
comp_msg = gr.Markdown()
|
| 59 |
+
comp_table = gr.Markdown()
|
| 60 |
+
|
| 61 |
+
# Feedback Section
|
| 62 |
+
with gr.Accordion("⭐ Give Feedback to Nadish", open=False):
|
| 63 |
+
f_name = gr.Textbox(label="Name or Email (Optional)")
|
| 64 |
+
f_txt = gr.Textbox(label="Your Feedback", lines=3)
|
| 65 |
+
f_rate = gr.Radio(["⭐", "⭐⭐", "⭐⭐⭐", "⭐⭐⭐⭐", "⭐⭐⭐⭐⭐"], label="Rating")
|
| 66 |
+
f_btn = gr.Button("Submit Feedback")
|
| 67 |
+
f_msg = gr.Markdown()
|
| 68 |
+
|
| 69 |
+
with gr.Accordion("📋 View All Feedback", open=False):
|
| 70 |
+
view_btn = gr.Button("Show Feedback")
|
| 71 |
+
view_msg = gr.Markdown()
|
| 72 |
+
|
| 73 |
+
reset_btn = gr.Button("Reset Everything", variant="stop", elem_classes="reset-btn")
|
| 74 |
+
|
| 75 |
+
# Button Actions
|
| 76 |
+
convert_btn.click(
|
| 77 |
+
images_to_pdf,
|
| 78 |
+
inputs=[img_input, p_size, p_orient, p_qual, p_comp],
|
| 79 |
+
outputs=[img_download, img_msg]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
merge_btn.click(
|
| 83 |
+
merge_pdfs,
|
| 84 |
+
inputs=[merge_input],
|
| 85 |
+
outputs=[merge_download, comb_msg]
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
comp_btn.click(
|
| 89 |
+
compress_pdf,
|
| 90 |
+
inputs=[comp_file, comp_lvl],
|
| 91 |
+
outputs=[comp_download, comp_msg, comp_table]
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
f_btn.click(
|
| 95 |
+
save_feedback,
|
| 96 |
+
inputs=[f_name, f_txt, f_rate],
|
| 97 |
+
outputs=f_msg
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
view_btn.click(show_feedback, outputs=view_msg)
|
| 101 |
+
|
| 102 |
+
# Reset function
|
| 103 |
+
def clear_all():
|
| 104 |
+
return [None] * 10 + [""]
|
| 105 |
+
|
| 106 |
+
reset_btn.click(
|
| 107 |
+
clear_all,
|
| 108 |
+
outputs=[img_input, merge_input, comp_file, img_download, merge_download,
|
| 109 |
+
img_msg, comb_msg, comp_msg, comp_table, f_msg]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|