EricGEGE commited on
Commit
d786351
·
verified ·
1 Parent(s): 50f7a79

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -0
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import gspread
3
+ from google.oauth2.service_account import Credentials
4
+ import os
5
+ import json
6
+ from datetime import datetime
7
+
8
+ # Load configuration
9
+ conf1 = os.getenv('CONF')
10
+ dict = json.loads(conf1)
11
+ SHEET_ID = os.getenv('SHEET_ID')
12
+
13
+ SCOPES = ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive']
14
+
15
+ # Initialize Google Sheets client
16
+ creds = Credentials.from_service_account_info(dict, scopes=SCOPES)
17
+ gc = gspread.authorize(creds)
18
+ sheet = gc.open_by_key(SHEET_ID)
19
+
20
+ def verify_username(username):
21
+ """Verify if username exists in user form"""
22
+ try:
23
+ user_sheet = sheet.worksheet('Users')
24
+ usernames = user_sheet.col_values(2) # Assuming username is in column B
25
+ is_valid = username in usernames
26
+
27
+ return {
28
+ username_output: "Username verified successfully" if is_valid else "Invalid username",
29
+ inquiry_section: gr.update(visible=is_valid)
30
+ }
31
+ except Exception as e:
32
+ return {
33
+ username_output: f"Error: {str(e)}",
34
+ inquiry_section: gr.update(visible=False)
35
+ }
36
+
37
+ def submit_inquiry(username, product_desc, purchase_qty, annual_amount, deadline, export_country):
38
+ """Submit inquiry information to Google Sheet"""
39
+ try:
40
+ # Try to get the Inquiry worksheet, create it if it doesn't exist
41
+ try:
42
+ inquiry_sheet = sheet.worksheet('Inquiry')
43
+ except gspread.WorksheetNotFound:
44
+ inquiry_sheet = sheet.add_worksheet('Inquiry', 1000, 7)
45
+ # Add headers
46
+ inquiry_sheet.append_row([
47
+ 'Timestamp', 'Username', 'Product Description',
48
+ 'Purchase Quantity', 'Annual Amount (USD)',
49
+ 'Purchase Deadline', 'Export Country'
50
+ ])
51
+
52
+ # Prepare the row data
53
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
54
+ row_data = [
55
+ timestamp, username, product_desc,
56
+ purchase_qty, annual_amount,
57
+ deadline, export_country
58
+ ]
59
+
60
+ # Append the data
61
+ inquiry_sheet.append_row(row_data)
62
+
63
+ return "Inquiry submitted successfully"
64
+ except Exception as e:
65
+ return f"Error submitting inquiry: {str(e)}"
66
+
67
+ # Create Gradio interface
68
+ with gr.Blocks() as app:
69
+ gr.Markdown(
70
+ """
71
+ # Product Inquiry System
72
+ ### Submit your product inquiry details
73
+ ---
74
+ """
75
+ )
76
+
77
+ # Username Verification Section
78
+ gr.Markdown("### 1. User Verification")
79
+ with gr.Group():
80
+ username_input = gr.Textbox(label="Username", placeholder="Enter your username")
81
+ verify_button = gr.Button("Verify Username", variant="primary")
82
+ username_output = gr.Textbox(label="Verification Status", interactive=False)
83
+
84
+ # Inquiry Form Section
85
+ with gr.Group(visible=False) as inquiry_section:
86
+ gr.Markdown("### 2. Inquiry Details")
87
+ product_desc = gr.Textbox(
88
+ label="Product Description",
89
+ placeholder="Enter product description",
90
+ lines=3
91
+ )
92
+ purchase_qty = gr.Number(
93
+ label="Purchase Quantity",
94
+ placeholder="Enter quantity"
95
+ )
96
+ annual_amount = gr.Number(
97
+ label="Estimated Annual Purchase Amount (USD)",
98
+ placeholder="Enter amount in USD"
99
+ )
100
+ deadline = gr.Textbox(
101
+ label="Purchase Deadline",
102
+ placeholder="Enter deadline (e.g., 2024-12-31)"
103
+ )
104
+ export_country = gr.Textbox(
105
+ label="Export Destination Country",
106
+ placeholder="Enter country name"
107
+ )
108
+
109
+ submit_button = gr.Button("Submit Inquiry", variant="primary")
110
+ submit_output = gr.Textbox(label="Submission Status", interactive=False)
111
+
112
+ # Event handlers
113
+ verify_button.click(
114
+ fn=verify_username,
115
+ inputs=username_input,
116
+ outputs=[username_output, inquiry_section]
117
+ )
118
+
119
+ submit_button.click(
120
+ fn=submit_inquiry,
121
+ inputs=[
122
+ username_input,
123
+ product_desc,
124
+ purchase_qty,
125
+ annual_amount,
126
+ deadline,
127
+ export_country
128
+ ],
129
+ outputs=submit_output
130
+ )
131
+
132
+ # Launch the app
133
+ app.launch()