Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -81,14 +81,10 @@ PRODUCT_NAMES = [
|
|
| 81 |
"Fusion", "Agroking", "CG commercial motors", "Jaguar", "Gaurav"
|
| 82 |
]
|
| 83 |
|
| 84 |
-
# Salesforce credentials
|
| 85 |
-
SALESFORCE_USERNAME =
|
| 86 |
-
SALESFORCE_PASSWORD =
|
| 87 |
-
SALESFORCE_SECURITY_TOKEN =
|
| 88 |
-
|
| 89 |
-
# Validate credentials
|
| 90 |
-
if not SALESFORCE_USERNAME or not SALESFORCE_PASSWORD or not SALESFORCE_SECURITY_TOKEN:
|
| 91 |
-
raise ValueError("Salesforce credentials are missing. Please set the environment variables.")
|
| 92 |
|
| 93 |
# Initialize PaddleOCR
|
| 94 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
@@ -126,19 +122,80 @@ def extract_attributes(extracted_text):
|
|
| 126 |
|
| 127 |
return attributes
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# Gradio Interface
|
| 130 |
def app():
|
| 131 |
with gr.Blocks() as demo:
|
| 132 |
with gr.Tab("π₯ OCR Processing"):
|
| 133 |
with gr.Row():
|
| 134 |
image_input = gr.Image(type="numpy", label="π Upload Image")
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
extract_button.click(
|
| 139 |
-
fn=
|
| 140 |
-
inputs=[image_input],
|
| 141 |
-
outputs=[extracted_text_output]
|
| 142 |
)
|
| 143 |
|
| 144 |
return demo
|
|
|
|
| 81 |
"Fusion", "Agroking", "CG commercial motors", "Jaguar", "Gaurav"
|
| 82 |
]
|
| 83 |
|
| 84 |
+
# Salesforce credentials
|
| 85 |
+
SALESFORCE_USERNAME = "Venkatramana@sandbox.com"
|
| 86 |
+
SALESFORCE_PASSWORD = "Seta12345@"
|
| 87 |
+
SALESFORCE_SECURITY_TOKEN = "Drl0jchCwLBfvX4ODMeFDksP"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
# Initialize PaddleOCR
|
| 90 |
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
|
|
|
| 122 |
|
| 123 |
return attributes
|
| 124 |
|
| 125 |
+
# Function to interact with Salesforce
|
| 126 |
+
def interact_with_salesforce(mode, entry_type, quantity, attributes):
|
| 127 |
+
try:
|
| 128 |
+
sf = Salesforce(
|
| 129 |
+
username=SALESFORCE_USERNAME,
|
| 130 |
+
password=SALESFORCE_PASSWORD,
|
| 131 |
+
security_token=SALESFORCE_SECURITY_TOKEN
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Define object and field names based on mode and entry type
|
| 135 |
+
if mode == "Entry":
|
| 136 |
+
if entry_type == "Sales":
|
| 137 |
+
object_name = "VENKATA_RAMANA_MOTORS__c"
|
| 138 |
+
field_name = "Quantity__c"
|
| 139 |
+
elif entry_type == "Non-Sales":
|
| 140 |
+
object_name = "UNBILLING_DATA__c"
|
| 141 |
+
field_name = "TotalQuantity__c"
|
| 142 |
+
elif mode == "Exit":
|
| 143 |
+
if entry_type == "Sales":
|
| 144 |
+
object_name = "Inventory_Management__c"
|
| 145 |
+
field_name = "Quantity_Sold__c"
|
| 146 |
+
elif entry_type == "Non-Sales":
|
| 147 |
+
object_name = "Un_Billable__c"
|
| 148 |
+
field_name = "Sold_Out__c"
|
| 149 |
+
else:
|
| 150 |
+
return "Invalid mode or entry type."
|
| 151 |
+
|
| 152 |
+
# Prepare data for Salesforce
|
| 153 |
+
sf_object = sf.__getattr__(object_name)
|
| 154 |
+
attributes[field_name] = quantity
|
| 155 |
+
sf_object.create(attributes)
|
| 156 |
+
|
| 157 |
+
return f"β
Data successfully exported to Salesforce object {object_name}."
|
| 158 |
+
except Exception as e:
|
| 159 |
+
return f"β Error interacting with Salesforce: {str(e)}"
|
| 160 |
+
|
| 161 |
+
# Function to process image, extract attributes, and allow editing
|
| 162 |
+
def process_image(image, mode, entry_type, quantity):
|
| 163 |
+
extracted_text = extract_text(image)
|
| 164 |
+
if not extracted_text:
|
| 165 |
+
return "No text detected in the image.", None, None
|
| 166 |
+
|
| 167 |
+
product_name = match_product_name(extracted_text)
|
| 168 |
+
attributes = extract_attributes(extracted_text)
|
| 169 |
+
if product_name:
|
| 170 |
+
attributes["Product name"] = product_name
|
| 171 |
+
|
| 172 |
+
# Ensure fixed attributes are present
|
| 173 |
+
for fixed_attr in ["Stage", "H.P.", "Product name", "Model"]:
|
| 174 |
+
if fixed_attr not in attributes:
|
| 175 |
+
attributes[fixed_attr] = ""
|
| 176 |
+
|
| 177 |
+
# Convert attributes to DataFrame for editing
|
| 178 |
+
df = pd.DataFrame(list(attributes.items()), columns=["Attribute", "Value"])
|
| 179 |
+
return f"Extracted Text:\n{extracted_text}", df, None
|
| 180 |
+
|
| 181 |
# Gradio Interface
|
| 182 |
def app():
|
| 183 |
with gr.Blocks() as demo:
|
| 184 |
with gr.Tab("π₯ OCR Processing"):
|
| 185 |
with gr.Row():
|
| 186 |
image_input = gr.Image(type="numpy", label="π Upload Image")
|
| 187 |
+
mode_input = gr.Dropdown(label="π Mode", choices=["Entry", "Exit"], value="Entry")
|
| 188 |
+
entry_type_input = gr.Radio(label="π¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales")
|
| 189 |
+
quantity_input = gr.Number(label="π’ Quantity", value=1, interactive=True)
|
| 190 |
+
extract_button = gr.Button("Extract Text and Attributes")
|
| 191 |
+
extracted_text_output = gr.Text(label="π Extracted Image Data")
|
| 192 |
+
editable_df_output = gr.Dataframe(label="βοΈ Edit Attributes (Key-Value Pairs)", interactive=True)
|
| 193 |
+
result_output = gr.Text(label="π Result")
|
| 194 |
|
| 195 |
extract_button.click(
|
| 196 |
+
fn=process_image,
|
| 197 |
+
inputs=[image_input, mode_input, entry_type_input, quantity_input],
|
| 198 |
+
outputs=[extracted_text_output, editable_df_output, result_output]
|
| 199 |
)
|
| 200 |
|
| 201 |
return demo
|