nagasurendra's picture
Update app.py
acb1b23 verified
import gradio as gr
from simple_salesforce import Salesforce
import os
import base64
from dotenv import load_dotenv
# Load environment variables from a .env file
load_dotenv()
# Salesforce login details
SF_USERNAME = os.getenv("SF_USERNAME")
SF_PASSWORD = os.getenv("SF_PASSWORD")
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN")
# Connect to Salesforce using Simple Salesforce
sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN)
# Function to create a record in Salesforce with the uploaded file
def create_vendor_scorecard(uploaded_file):
try:
# Get the file path and read the file content
file_path = uploaded_file.name # This is the file path
with open(file_path, "rb") as file:
file_data = file.read()
# Encode the file content to base64
encoded_file = base64.b64encode(file_data).decode('utf-8')
# Create a new record in the Vendor_Scorecard__c object with the file
record = sf.Vendor_Scorecard__c.create({
'Uploaded_File__c': {
'filename': uploaded_file.name,
'body': encoded_file,
'contentType': uploaded_file.type
}
})
return f"File uploaded successfully with ID: {record['id']}"
except Exception as e:
return f"Error: {str(e)}"
# Gradio interface function triggered by the submit button
def submit_button(uploaded_file):
return create_vendor_scorecard(uploaded_file)
# Create the file upload input
file_input = gr.File(label="Upload File")
# Create the Gradio interface with the submit button
iface = gr.Interface(
fn=submit_button,
inputs=[file_input],
outputs="text",
live=False, # Disable live updating; it only triggers when submit button is clicked
title="Salesforce Vendor Scorecard File Upload",
allow_flagging="never"
)
if __name__ == "__main__":
iface.launch()