import gradio as gr import gspread from google.oauth2.service_account import Credentials import os import json from datetime import datetime # Load configuration conf1 = os.getenv('CONF') dict = json.loads(conf1) SHEET_ID = os.getenv('SHEET_ID') SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'] # Initialize Google Sheets client creds = Credentials.from_service_account_info(dict, scopes=SCOPES) gc = gspread.authorize(creds) sheet = gc.open_by_key(SHEET_ID) def verify_username(username): """Verify if username exists in user form""" scri= '''Hello, this is [Your Name] from YLsourcing. Does your company currently have any procurement plans? We’d be happy to offer you free recommendations of the best direct suppliers in China, tailored to your needs. What’s more, we’ll even pay you a bonus of 60 RMB for each inquiry we successfully match with a top supplier. Would you be open to exploring this opportunity?''' try: user_sheet = sheet.worksheet('Users') usernames = user_sheet.col_values(2) # Assuming username is in column B is_valid = username in usernames return { username_output: scri if is_valid else "Invalid username", inquiry_section: gr.update(visible=is_valid) } except Exception as e: return { username_output: f"Error: {str(e)}", inquiry_section: gr.update(visible=False) } def submit_inquiry(username, company_name, phone_number, product_desc, purchase_qty, annual_amount, deadline, export_country): """Submit inquiry information to Google Sheet""" try: # Try to get the Inquiry worksheet, create it if it doesn't exist try: inquiry_sheet = sheet.worksheet('Inquiry') except gspread.WorksheetNotFound: inquiry_sheet = sheet.add_worksheet('Inquiry', 1000, 9) # Increased columns for new fields # Add headers inquiry_sheet.append_row([ 'Timestamp', 'Username', 'Company Name', 'Phone Number', 'Product Description', 'Purchase Quantity', 'Annual Amount (USD)', 'Purchase Deadline', 'Export Country' ]) # Prepare the row data timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") row_data = [ timestamp, username, company_name, phone_number, product_desc, purchase_qty, annual_amount, deadline, export_country ] # Append the data inquiry_sheet.append_row(row_data) return "Inquiry submitted successfully" except Exception as e: return f"Error submitting inquiry: {str(e)}" # Create Gradio interface with gr.Blocks() as app: gr.Markdown( """ # Product Inquiry System ### Submit your product inquiry details --- """ ) # Username Verification Section gr.Markdown("### 1. User Verification") with gr.Group(): username_input = gr.Textbox(label="Username", placeholder="Enter your username") verify_button = gr.Button("Verify Username", variant="primary") username_output = gr.Textbox(label="Verification Status", interactive=False) # Inquiry Form Section with gr.Group(visible=False) as inquiry_section: gr.Markdown("### 2. Inquiry Details") company_name = gr.Textbox( label="Full Company Name", placeholder="Enter your company's full name" ) phone_number = gr.Textbox( label="Phone Number", placeholder="Enter your phone number" ) product_desc = gr.Textbox( label="Product Description", placeholder="Enter product description", lines=3 ) purchase_qty = gr.Textbox( label="Purchase Quantity", placeholder="Enter quantity" ) annual_amount = gr.Textbox( label="Estimated Annual Purchase Amount (USD)", placeholder="Enter amount in USD" ) deadline = gr.Textbox( label="Purchase Deadline", placeholder="Enter deadline (e.g., 2024-12-31)" ) export_country = gr.Textbox( label="Export Destination Country", placeholder="Enter country name" ) submit_button = gr.Button("Submit Inquiry", variant="primary") submit_output = gr.Textbox(label="Submission Status", interactive=False) # Event handlers verify_button.click( fn=verify_username, inputs=username_input, outputs=[username_output, inquiry_section] ) submit_button.click( fn=submit_inquiry, inputs=[ username_input, company_name, phone_number, product_desc, purchase_qty, annual_amount, deadline, export_country ], outputs=submit_output ) # Launch the app app.launch()