Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,48 @@
|
|
1 |
-
# Step 1: Install Gradio and FPDF (
|
2 |
-
# !pip install gradio fpdf
|
3 |
|
4 |
# Step 2: Import necessary libraries
|
5 |
import gradio as gr
|
6 |
from fpdf import FPDF
|
|
|
|
|
7 |
|
8 |
# Step 3: Define a function to handle form submission and generate a PDF file
|
9 |
def submit_form(name, whatsapp, membership, protest):
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
pdf = FPDF()
|
12 |
pdf.add_page()
|
13 |
pdf.set_font("Arial", size=12)
|
14 |
-
|
15 |
-
# Add form data in formal format
|
16 |
pdf.cell(200, 10, txt=f"Name: {'_'*50}", ln=True)
|
17 |
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {'_'*40}", ln=True)
|
18 |
-
pdf.cell(200, 10, txt=f"Are you
|
19 |
pdf.cell(200, 10, txt=f"Did you join the protest?: {'_'*35}", ln=True)
|
20 |
|
21 |
-
# Now add the user's input
|
22 |
pdf.ln(10) # Line break for spacing
|
23 |
pdf.cell(200, 10, txt=f"Name: {name}", ln=True)
|
24 |
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {whatsapp}", ln=True)
|
25 |
pdf.cell(200, 10, txt=f"Membership of YEP: {membership}", ln=True)
|
26 |
pdf.cell(200, 10, txt=f"Participation in Protest: {protest}", ln=True)
|
27 |
|
28 |
-
# Save
|
29 |
-
pdf_output_path = "/tmp/form_data.pdf"
|
30 |
pdf.output(pdf_output_path)
|
31 |
|
32 |
-
# Return
|
33 |
-
return
|
34 |
|
35 |
-
# Step 4: Set up the Gradio interface
|
36 |
with gr.Blocks() as demo:
|
37 |
gr.Markdown("## Submit Your Information")
|
38 |
|
@@ -47,16 +57,19 @@ with gr.Blocks() as demo:
|
|
47 |
6. The new PEC building should be used as an Incubation Center, not for renting purposes.
|
48 |
""")
|
49 |
|
|
|
50 |
name = gr.Textbox(label="Name", placeholder="Enter your name here")
|
51 |
whatsapp = gr.Textbox(label="WhatsApp Mobile No.", placeholder="Enter your WhatsApp mobile number here")
|
52 |
membership = gr.Dropdown(label="Are you a member of YEP?", choices=["Yes", "No"], value="No")
|
53 |
protest = gr.Dropdown(label="Did you join the protest?", choices=["Yes", "No"], value="No")
|
54 |
|
55 |
submit = gr.Button("Submit")
|
56 |
-
|
|
|
|
|
57 |
|
58 |
# Link the button to the function
|
59 |
-
submit.click(submit_form, inputs=[name, whatsapp, membership, protest], outputs=
|
60 |
|
61 |
# Step 5: Launch the Gradio app
|
62 |
demo.launch()
|
|
|
1 |
+
# Step 1: Install Gradio and FPDF (Hugging Face Spaces will use requirements.txt)
|
2 |
+
# !pip install gradio fpdf pandas
|
3 |
|
4 |
# Step 2: Import necessary libraries
|
5 |
import gradio as gr
|
6 |
from fpdf import FPDF
|
7 |
+
import pandas as pd
|
8 |
+
import os
|
9 |
|
10 |
# Step 3: Define a function to handle form submission and generate a PDF file
|
11 |
def submit_form(name, whatsapp, membership, protest):
|
12 |
+
# Save form data to a CSV file (backend storage)
|
13 |
+
data = {'Name': [name], 'WhatsApp': [whatsapp], 'Membership': [membership], 'Protest': [protest]}
|
14 |
+
df = pd.DataFrame(data)
|
15 |
+
|
16 |
+
# Save CSV in /tmp/ folder (Hugging Face Spaces temporary storage)
|
17 |
+
csv_file = "/tmp/submitted_data.csv"
|
18 |
+
if not os.path.isfile(csv_file):
|
19 |
+
df.to_csv(csv_file, index=False)
|
20 |
+
else:
|
21 |
+
df.to_csv(csv_file, mode='a', header=False, index=False)
|
22 |
+
|
23 |
+
# Create a PDF object and add form data in formal format (backend storage)
|
24 |
pdf = FPDF()
|
25 |
pdf.add_page()
|
26 |
pdf.set_font("Arial", size=12)
|
|
|
|
|
27 |
pdf.cell(200, 10, txt=f"Name: {'_'*50}", ln=True)
|
28 |
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {'_'*40}", ln=True)
|
29 |
+
pdf.cell(200, 10, txt=f"Are you a member of YEP?: {'_'*30}", ln=True)
|
30 |
pdf.cell(200, 10, txt=f"Did you join the protest?: {'_'*35}", ln=True)
|
31 |
|
|
|
32 |
pdf.ln(10) # Line break for spacing
|
33 |
pdf.cell(200, 10, txt=f"Name: {name}", ln=True)
|
34 |
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {whatsapp}", ln=True)
|
35 |
pdf.cell(200, 10, txt=f"Membership of YEP: {membership}", ln=True)
|
36 |
pdf.cell(200, 10, txt=f"Participation in Protest: {protest}", ln=True)
|
37 |
|
38 |
+
# Save PDF to /tmp/ folder (backend storage)
|
39 |
+
pdf_output_path = "/tmp/form_data.pdf"
|
40 |
pdf.output(pdf_output_path)
|
41 |
|
42 |
+
# Return a success message instead of the PDF file for the user
|
43 |
+
return "Thank you! Your information has been submitted successfully."
|
44 |
|
45 |
+
# Step 4: Set up the Gradio interface without showing the downloadable PDF
|
46 |
with gr.Blocks() as demo:
|
47 |
gr.Markdown("## Submit Your Information")
|
48 |
|
|
|
57 |
6. The new PEC building should be used as an Incubation Center, not for renting purposes.
|
58 |
""")
|
59 |
|
60 |
+
# Form input fields
|
61 |
name = gr.Textbox(label="Name", placeholder="Enter your name here")
|
62 |
whatsapp = gr.Textbox(label="WhatsApp Mobile No.", placeholder="Enter your WhatsApp mobile number here")
|
63 |
membership = gr.Dropdown(label="Are you a member of YEP?", choices=["Yes", "No"], value="No")
|
64 |
protest = gr.Dropdown(label="Did you join the protest?", choices=["Yes", "No"], value="No")
|
65 |
|
66 |
submit = gr.Button("Submit")
|
67 |
+
|
68 |
+
# Display a message after submission instead of the PDF download link
|
69 |
+
output_message = gr.Textbox(visible=False, label="Output")
|
70 |
|
71 |
# Link the button to the function
|
72 |
+
submit.click(submit_form, inputs=[name, whatsapp, membership, protest], outputs=output_message)
|
73 |
|
74 |
# Step 5: Launch the Gradio app
|
75 |
demo.launch()
|