inquirycenter / app.py
EricGEGE's picture
Create app.py
d786351 verified
raw
history blame
4.28 kB
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()