| import gradio as gr |
| import requests |
| import pandas as pd |
|
|
| |
| GOOGLE_SHEET_ID = "1ic4J-NFI-72gMhjpf-EEmj5jFPc_2kWTTOtlMZmI0OM" |
| API_KEY = "AIzaSyCHQI_cHzqrDzGji87enYAa6CfyZBHUPRE" |
| SHEET_NAME = "Sheet1" |
|
|
| def get_match(phone_number): |
| """ |
| Looks up a phone number in your pairing sheet using Google Sheets API. |
| """ |
| try: |
| |
| phone_number = str(phone_number).strip() |
| |
| |
| url = f"https://sheets.googleapis.com/v4/spreadsheets/{GOOGLE_SHEET_ID}/values/{SHEET_NAME}?key={API_KEY}" |
| |
| |
| response = requests.get(url) |
| |
| if response.status_code != 200: |
| return f"β Could not connect to the sheet. Error: {response.status_code}" |
| |
| data = response.json() |
| |
| if 'values' not in data: |
| return "β No data found in the sheet. Please check the sheet name and sharing settings." |
| |
| |
| all_rows = data['values'] |
| |
| |
| headers = all_rows[0] |
| |
| |
| |
| |
| |
| |
| person_num_col = -1 |
| person_name_col = -1 |
| match_num_col = -1 |
| match_name_col = -1 |
| |
| for i, header in enumerate(headers): |
| header_lower = header.lower() |
| if "person's number" in header_lower or "person" in header_lower and "number" in header_lower: |
| person_num_col = i |
| elif "person's name" in header_lower or "person" in header_lower and "name" in header_lower: |
| person_name_col = i |
| elif "match's number" in header_lower or "match" in header_lower and "number" in header_lower: |
| match_num_col = i |
| elif "match's name" in header_lower or "match" in header_lower and "name" in header_lower: |
| match_name_col = i |
| |
| |
| if person_num_col == -1: |
| return "β Could not find 'Person's Number' column in sheet. Please check column names." |
| |
| |
| for row in all_rows[1:]: |
| |
| if len(row) > person_num_col: |
| |
| current_phone = str(row[person_num_col]).strip() |
| |
| |
| if current_phone == phone_number: |
| |
| |
| |
| person_name = "" |
| if person_name_col != -1 and len(row) > person_name_col: |
| person_name = row[person_name_col] |
| |
| |
| match_name = "" |
| match_number = "" |
| |
| if match_name_col != -1 and len(row) > match_name_col: |
| match_name = row[match_name_col] |
| |
| if match_num_col != -1 and len(row) > match_num_col: |
| match_number = row[match_num_col] |
| |
| |
| result_message = f""" |
| π **Your Secret Valentine Match is...** π |
| |
| **{match_name if match_name else 'Match Found!'}** |
| |
| **Contact:** {match_number if match_number else 'Number not available'} |
| |
| π Reach out and make their day special! |
| |
| --- |
| *Remember, you are: {person_name if person_name else phone_number}* |
| """ |
| return result_message |
| |
| |
| return "β Phone number not found. Please check the number and try again." |
| |
| except Exception as e: |
| |
| error_msg = str(e) |
| if "404" in error_msg: |
| return "β Sheet not found. Please check your Sheet ID and API Key." |
| elif "403" in error_msg: |
| return "β Access denied. Please ensure your sheet is shared publicly and your API key is valid." |
| else: |
| return f"β An error occurred: {error_msg[:100]}..." |
|
|
| |
| with gr.Blocks(title="Find Your Secret Valentine π", theme=gr.themes.Soft()) as demo: |
| gr.Markdown(""" |
| # π Find Your Secret Valentine Match |
| Enter the phone number you used to sign up (exactly as you registered). |
| """) |
| |
| with gr.Row(): |
| phone_input = gr.Textbox( |
| label="Your Phone Number", |
| placeholder="e.g., 0206931244", |
| info="Enter your number exactly as it appears in the sheet." |
| ) |
| |
| submit_btn = gr.Button("Find My Match! π", variant="primary") |
| output = gr.Markdown(label="Your Match") |
| |
| |
| submit_btn.click(fn=get_match, inputs=phone_input, outputs=output) |
| |
| |
| |
| |
| |
| gr.Markdown("---") |
| gr.Markdown("*If you encounter any issues, please contact the organizers.*") |
|
|
| |
| if __name__ == "__main__": |
| demo.launch() |