YEP / app.py
atifsial123's picture
Update app.py
017718c verified
raw
history blame
3.39 kB
# Step 1: Install Gradio and FPDF (Hugging Face Spaces will use requirements.txt)
# !pip install gradio fpdf pandas
# Step 2: Import necessary libraries
import gradio as gr
from fpdf import FPDF
import pandas as pd
import os
# Step 3: 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"
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 in formal format (backend storage)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=f"Name: {'_'*50}", ln=True)
pdf.cell(200, 10, txt=f"WhatsApp Mobile No: {'_'*40}", ln=True)
pdf.cell(200, 10, txt=f"Are you a member of YEP?: {'_'*30}", ln=True)
pdf.cell(200, 10, txt=f"Did you join the protest?: {'_'*35}", ln=True)
pdf.ln(10) # Line break for spacing
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 = "/tmp/form_data.pdf"
pdf.output(pdf_output_path)
# Return a success message instead of the PDF file for the user
return "Thank you! Your information has been submitted successfully."
# Step 4: Set up the Gradio interface without showing the downloadable PDF
with gr.Blocks() as demo:
gr.Markdown("## Submit Your Information")
# Agenda of YEP
gr.Markdown("""
### Young Engineers Pakistan (YEP) Agenda
1. Grant rights to Young Engineers in the Governing Body.
2. Provide detailed transparency of PEC expenses, showing how thousands of rupees are utilized by PEC employees.
3. Immediately withdraw all illegal notifications and promotions.
4. Grant Young Engineers the right to resign from and rejoin any company.
5. Provide Young Engineers with opportunities for internships, innovation, and access to an Incubation Center.
6. The new PEC building should be used as an Incubation Center, not for renting purposes.
""")
# 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")
# Display a message after submission instead of the PDF download link
output_message = gr.Textbox(visible=False, label="Output")
# Link the button to the function
submit.click(submit_form, inputs=[name, whatsapp, membership, protest], outputs=output_message)
# Step 5: Launch the Gradio app
demo.launch()