rsvp_exp / app.py
ricklon's picture
Update app.py
db82224 verified
import gradio as gr
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
import qrcode
from PIL import Image
import io
import numpy as np
import datetime
# Set global variables
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
HOST_EMAIL = os.getenv('HOST_EMAIL')
DISCORD_LINK = os.getenv('DISCORD_LINK')
# Add a set to track submitted emails
submitted_emails = set()
# Function to create email content
def create_email_content(name, email, question, event_date, event_time, rsvp_timestamp):
base_content = f'''
<p>Thank you for your RSVP, {name}. You are registered for the AI Study Group event on {event_date} from {event_time}.</p>
<p>Discord link to join the group: {DISCORD_LINK}</p>
<pre>
{discord_instructions_markdown}
FUBAR Labs - New Jersey's First Hackerspace
"Where fun is learning and work is fun."
Explore: [FUBAR Labs Website](https://fubarlabs.org)
Support Us: [Donate Here](https://fubarlabs.org/donate)
FUBAR Labs | 1510B Jersey Avenue, North Brunswick, NJ 08902
Phone: 732-50-FUBAR | Email: info@fubarlabs.org
</pre>
'''
question_content = f"<p>Your question for the AI Study Group: {question}</p>" if question else "<p>No specific questions for this session.</p>"
return base_content + question_content
# Function to send emails
def send_email(to_email, subject, content):
sg = SendGridAPIClient(SENDGRID_API_KEY)
email = Mail(from_email=HOST_EMAIL, to_emails=to_email, subject=subject, html_content=content)
sg.send(email)
# Discord Instructions in Markdown format
discord_instructions_markdown = """
### Joining the AI Study Group on Discord
To join the virtual AI Study Group on Discord, please follow these steps:
1. If you don't have Discord, download it from [Discord's official website](https://discord.com/) or install the app on your mobile device.
2. Create a Discord account if you don't have one.
3. Scan the QR code provided or click on the Discord link to join our server.
4. Once in the server, introduce yourself in the introduction channel and feel free to explore different channels.
5. Respect community guidelines and enjoy collaborating with fellow AI enthusiasts!
"""
# GDPR Notice and Consent in Markdown format
gdpr_markdown = """
### Privacy Notice and Consent
By submitting this form, you consent to the processing and storage of your personal data as outlined below:
- **Purpose**: Your data is collected to verify your intention to attend the event and to prevent spam and trolling.
- **Data Storage**: Your personal information is stored securely and will only be used for the purposes of event management.
- **Data Access**: You have the right to access your personal data upon request.
- **Data Deletion**: You can request the deletion of your personal data at any time.
- **No Third Party Sharing**: Your data will not be shared with any third parties.
Please check the box above to indicate your consent.
"""
def calculate_next_event_date():
today = datetime.date.today()
first_of_month = datetime.date(today.year, today.month, 1)
first_sunday = first_of_month + datetime.timedelta(days=(6-first_of_month.weekday()))
if today <= first_sunday:
return first_sunday
else:
if first_of_month.month == 12:
return datetime.date(first_of_month.year + 1, 1, 1) + datetime.timedelta(days=(6-1))
else:
return datetime.date(first_of_month.year, first_of_month.month + 1, 1) + datetime.timedelta(days=(6-1))
# Main RSVP function
def add_rsvp(name, email, question, consent):
if not consent or not email:
return "**Error**: Consent and Email are required to RSVP."
if email in submitted_emails:
return ["**Error**: You have already submitted an RSVP.", "", ""]
event_date = calculate_next_event_date().strftime("%B %d, %Y")
event_time = "2:00 PM to 3:30 PM"
rsvp_timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Create email content
attendee_email_content = create_email_content(name, email, question, event_date, event_time, rsvp_timestamp)
host_email_content = f'''
<p>New RSVP received for the AI Study Group:</p>
<p><b>Name:</b> {name}</p>
<p><b>Email:</b> {email}</p>
<p><b>RSVP Timestamp:</b> {rsvp_timestamp}</p>
<p><b>Event Date:</b> {event_date} {event_time}</p>
{attendee_email_content}
'''
# Send emails
send_email(email, f'Thank you for your RSVP on {event_date} at {event_time}', attendee_email_content)
send_email(HOST_EMAIL, f'New RSVP to AI Study Group {event_date} received', host_email_content)
# Add the email to the set of submitted emails
submitted_emails.add(email)
# Return the response, GDPR notice, and Discord instructions
response = f"**Thank you** for your RSVP, {name}. Please [join the study group on Discord]({DISCORD_LINK})."
return [response, gdpr_markdown, discord_instructions_markdown]
# Gradio interface with Markdown component for GDPR notice and Discord Instructions
demo = gr.Interface(
fn=add_rsvp,
inputs=[
gr.Textbox(label="Enter your full name"),
gr.Textbox(label="Enter your email", placeholder="example@example.com", type="email"),
gr.Textbox(label="What is your question for the study group?", lines=7),
gr.Checkbox(label="I consent to my data being processed and stored in accordance with the privacy policy.", value=False)
],
outputs=[
gr.Markdown(), # Response
gr.Markdown(), # GDPR Notice
gr.Markdown()
],
title="AI Study Group RSVP",
description="Please fill in your details to RSVP for the study group.",
article=gdpr_markdown + discord_instructions_markdown # Combining GDPR and Discord instructions in the article
)
# Run the Gradio app
demo.launch()