File size: 2,218 Bytes
a86f196
afd3e5b
 
017718c
 
afd3e5b
5be3459
afd3e5b
017718c
 
 
 
 
 
3200ebc
 
017718c
 
 
 
 
3200ebc
afd3e5b
 
 
 
 
 
 
 
017718c
e8204ec
afd3e5b
 
26575d6
e581f14
afd3e5b
e581f14
afd3e5b
 
 
017718c
afd3e5b
 
 
 
 
 
734927a
e581f14
 
afd3e5b
5be3459
afd3e5b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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()