YEP / app.py
atifsial123's picture
Update app.py
26575d6 verified
# Step 1: Import necessary libraries
import gradio as gr
from fpdf import FPDF
import pandas as pd
import os
# Step 2: Define a function to handle form submission and generate a PDF file
def submit_form(name, whatsapp, membership, protest):
# Save form data to a CSV file (backend storage)
data = {'Name': [name], 'WhatsApp': [whatsapp], 'Membership': [membership], 'Protest': [protest]}
df = pd.DataFrame(data)
# Save CSV in /tmp/ folder (Hugging Face Spaces temporary storage)
csv_file = "/tmp/submitted_data.csv"
# Check if the file exists, append data if it does, otherwise create a new one
if not os.path.isfile(csv_file):
df.to_csv(csv_file, index=False)
else:
df.to_csv(csv_file, mode='a', header=False, index=False)
# Create a PDF object and add form data (backend storage only)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=f"Name: {name}", ln=True)
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {whatsapp}", ln=True)
pdf.cell(200, 10, txt=f"Membership of YEP: {membership}", ln=True)
pdf.cell(200, 10, txt=f"Participation in Protest: {protest}", ln=True)
# Save PDF to /tmp/ folder (backend storage)
pdf_output_path = f"/tmp/form_data_{name}.pdf"
pdf.output(pdf_output_path)
# Return a success message for admin (optional) or None to the user
return None
# Step 3: Set up the Gradio interface without showing the outputs to the user
with gr.Blocks() as demo:
gr.Markdown("## Submit Your Information")
# Form input fields
name = gr.Textbox(label="Name", placeholder="Enter your name here")
whatsapp = gr.Textbox(label="WhatsApp Mobile No.", placeholder="Enter your WhatsApp mobile number here")
membership = gr.Dropdown(label="Are you a member of YEP?", choices=["Yes", "No"], value="No")
protest = gr.Dropdown(label="Did you join the protest?", choices=["Yes", "No"], value="No")
submit = gr.Button("Submit")
# Link the button to the function without showing outputs
submit.click(fn=submit_form, inputs=[name, whatsapp, membership, protest], outputs=[])
# Step 4: Launch the Gradio app
demo.launch()