File size: 4,280 Bytes
d786351
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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"""
    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: "Username verified successfully" 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, 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, 7)
            # Add headers
            inquiry_sheet.append_row([
                'Timestamp', 'Username', '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, 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")
        product_desc = gr.Textbox(
            label="Product Description",
            placeholder="Enter product description",
            lines=3
        )
        purchase_qty = gr.Number(
            label="Purchase Quantity",
            placeholder="Enter quantity"
        )
        annual_amount = gr.Number(
            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,
            product_desc,
            purchase_qty,
            annual_amount,
            deadline,
            export_country
        ],
        outputs=submit_output
    )

# Launch the app
app.launch()