File size: 5,994 Bytes
d6971e6
d8a7ff4
 
7255edb
6e741d5
ca2430d
d8a7ff4
 
7255edb
 
d6971e6
d8a7ff4
d6971e6
7255edb
d8a7ff4
 
6e741d5
d8a7ff4
6e741d5
d8a7ff4
 
6e741d5
d8a7ff4
 
 
 
6e741d5
 
 
d8a7ff4
6e741d5
d8a7ff4
 
6e741d5
d8a7ff4
 
6e741d5
d8a7ff4
 
 
6e741d5
d8a7ff4
 
 
 
 
6e741d5
d8a7ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e741d5
d8a7ff4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e741d5
d8a7ff4
 
6e741d5
7255edb
d8a7ff4
 
 
 
 
 
 
 
7255edb
6e741d5
7255edb
d8a7ff4
 
 
 
6e741d5
7255edb
 
 
6e741d5
d8a7ff4
7255edb
6e741d5
7255edb
 
6e741d5
d8a7ff4
7255edb
6e741d5
bd5645a
 
d8a7ff4
 
 
 
d6971e6
d8a7ff4
d6971e6
b2ee422
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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()