Download app.py from Tree-Diagram/Secrete_Valentine_Prototype: direct link, hf CLI and curl.
- Browser
- Download file 5.99 kB
-
https://huggingface.co/spaces/Tree-Diagram/Secrete_Valentine_Prototype/resolve/main/app.py
- Command line
-
hf download hf://spaces/Tree-Diagram/Secrete_Valentine_Prototype/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Tree-Diagram/Secrete_Valentine_Prototype/resolve/main/app.py
5.99 kB
| import gradio as gr | |
| import requests | |
| import pandas as pd | |
| # 1. CONFIGURATION - SET THESE VALUES | |
| GOOGLE_SHEET_ID = "1ic4J-NFI-72gMhjpf-EEmj5jFPc_2kWTTOtlMZmI0OM" # From your spreadsheet URL (e.g., 1xYz... from docs.google.com/spreadsheets/d/1xYz.../edit) | |
| API_KEY = "AIzaSyCHQI_cHzqrDzGji87enYAa6CfyZBHUPRE" # Your Google Sheets API key | |
| SHEET_NAME = "Sheet1" # Name of your pairing sheet tab/worksheet | |
| def get_match(phone_number): | |
| """ | |
| Looks up a phone number in your pairing sheet using Google Sheets API. | |
| """ | |
| try: | |
| # Clean the phone number input | |
| phone_number = str(phone_number).strip() | |
| # Build the API URL to get ALL data from your sheet | |
| url = f"https://sheets.googleapis.com/v4/spreadsheets/{GOOGLE_SHEET_ID}/values/{SHEET_NAME}?key={API_KEY}" | |
| # Make the API request | |
| 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." | |
| # Get all rows from the sheet | |
| all_rows = data['values'] | |
| # First row contains headers - let's find column indices | |
| headers = all_rows[0] | |
| # Find the column indices based on your sheet structure | |
| # From your screenshot, your columns are: | |
| # A: "Person's Number", B: "Person's Name", C: "Match's Number", D: "Match's Name" | |
| # We'll handle it flexibly - find columns by name | |
| 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 | |
| # Check if we found all necessary columns | |
| if person_num_col == -1: | |
| return "β Could not find 'Person's Number' column in sheet. Please check column names." | |
| # Search for the phone number in all rows (skip header row) | |
| for row in all_rows[1:]: | |
| # Make sure the row has enough columns | |
| if len(row) > person_num_col: | |
| # Get the phone number from this row | |
| current_phone = str(row[person_num_col]).strip() | |
| # Check if this is the row we're looking for | |
| if current_phone == phone_number: | |
| # Found the person! Now get their match details | |
| # Get person's name if available | |
| person_name = "" | |
| if person_name_col != -1 and len(row) > person_name_col: | |
| person_name = row[person_name_col] | |
| # Get match details | |
| 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] | |
| # Format the beautiful result | |
| 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 | |
| # If we get here, the phone number wasn't found | |
| return "β Phone number not found. Please check the number and try again." | |
| except Exception as e: | |
| # Return a user-friendly error message | |
| 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]}..." | |
| # 2. BUILD THE GRADIO INTERFACE | |
| 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") | |
| # Connect the button to our function | |
| submit_btn.click(fn=get_match, inputs=phone_input, outputs=output) | |
| # Add a footer | |
| gr.Markdown("---") | |
| gr.Markdown("*If you encounter any issues, please contact the organizers.*") | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch() |