Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
from paddleocr import PaddleOCR
|
| 3 |
-
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
-
import
|
| 6 |
import re
|
| 7 |
-
from fuzzywuzzy import process
|
| 8 |
from simple_salesforce import Salesforce
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Attribute mappings: readable names to Salesforce API names
|
| 11 |
ATTRIBUTE_MAPPING = {
|
|
@@ -71,13 +73,6 @@ ATTRIBUTE_MAPPING = {
|
|
| 71 |
"coolingmethod": "coolingmethod__c"
|
| 72 |
}
|
| 73 |
|
| 74 |
-
# List of product names to match
|
| 75 |
-
PRODUCT_NAMES = [
|
| 76 |
-
"Centrifugal mono block pump", "SINGLE PHASE MOTOR STARTER", "EasyPact EZC 100",
|
| 77 |
-
"Openwell Submersible Pumpset", "Electric Motor", "Self Priming Pump",
|
| 78 |
-
# Add more products here
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
# Salesforce credentials
|
| 82 |
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 83 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
|
@@ -92,83 +87,131 @@ EXCEL_FILE_PATH = os.getenv("EXCEL_FILE_PATH", "DataStorage.xlsx")
|
|
| 92 |
# Function to extract text using PaddleOCR
|
| 93 |
def extract_text(image):
|
| 94 |
result = ocr.ocr(image)
|
| 95 |
-
extracted_text =
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Function to find product name from the predefined list using fuzzy matching
|
| 99 |
def match_product_name(text):
|
| 100 |
-
best_match
|
|
|
|
|
|
|
|
|
|
| 101 |
for line in text.split("\n"):
|
| 102 |
match, score = process.extractOne(line, PRODUCT_NAMES)
|
| 103 |
-
if score > best_score:
|
| 104 |
-
best_match
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
# Function to find attributes and their values
|
| 108 |
def find_attributes(text):
|
| 109 |
structured_data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
|
| 111 |
-
pattern = rf"{re.escape(readable_attr)}[:\-]?\s*(.+)"
|
| 112 |
match = re.search(pattern, text, re.IGNORECASE)
|
| 113 |
if match:
|
| 114 |
structured_data[sf_attr] = match.group(1).strip()
|
| 115 |
-
return structured_data
|
| 116 |
|
| 117 |
-
|
| 118 |
-
def process_image(image, quantity, mode, entry_type):
|
| 119 |
-
try:
|
| 120 |
-
extracted_text = extract_text(image)
|
| 121 |
-
attributes = find_attributes(extracted_text)
|
| 122 |
-
attributes["Quantity__c"] = quantity
|
| 123 |
-
numbered_output = "\n".join([f"{k}: {v}" for k, v in attributes.items()])
|
| 124 |
-
return f"Extracted Text:\n{extracted_text}\n\nAttributes:\n{numbered_output}", None
|
| 125 |
-
except Exception as e:
|
| 126 |
-
return f"Error: {str(e)}", None
|
| 127 |
|
| 128 |
-
# Function to pull
|
| 129 |
-
def
|
| 130 |
try:
|
| 131 |
sf = Salesforce(
|
| 132 |
username=SALESFORCE_USERNAME,
|
| 133 |
password=SALESFORCE_PASSWORD,
|
| 134 |
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
|
|
|
|
| 138 |
except Exception as e:
|
|
|
|
| 139 |
return f"Error: {str(e)}"
|
| 140 |
|
| 141 |
-
# Function to format Salesforce data
|
| 142 |
def format_salesforce_data():
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
gr.Interface(
|
| 152 |
fn=process_image,
|
| 153 |
inputs=[
|
| 154 |
gr.Image(type="numpy"),
|
| 155 |
-
gr.Number(label="Quantity"),
|
| 156 |
-
gr.Dropdown(label="Mode", choices=["Entry", "Exit"]),
|
| 157 |
-
gr.Radio(label="Entry Type", choices=["Sales", "Non-Sales"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
],
|
| 159 |
-
|
| 160 |
-
|
| 161 |
),
|
| 162 |
gr.Interface(
|
| 163 |
-
fn=
|
| 164 |
inputs=[],
|
| 165 |
-
outputs=
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
)
|
|
|
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
from paddleocr import PaddleOCR
|
| 3 |
+
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
import re
|
|
|
|
| 7 |
from simple_salesforce import Salesforce
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
from io import BytesIO
|
| 11 |
|
| 12 |
# Attribute mappings: readable names to Salesforce API names
|
| 13 |
ATTRIBUTE_MAPPING = {
|
|
|
|
| 73 |
"coolingmethod": "coolingmethod__c"
|
| 74 |
}
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
# Salesforce credentials
|
| 77 |
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 78 |
SALESFORCE_PASSWORD = "Venkat12345@"
|
|
|
|
| 87 |
# Function to extract text using PaddleOCR
|
| 88 |
def extract_text(image):
|
| 89 |
result = ocr.ocr(image)
|
| 90 |
+
extracted_text = []
|
| 91 |
+
for line in result[0]:
|
| 92 |
+
extracted_text.append(line[1][0])
|
| 93 |
+
extracted_text_str = "\n".join(extracted_text)
|
| 94 |
+
print("Extracted Text:", extracted_text_str) # Debug: Log extracted text
|
| 95 |
+
return extracted_text_str
|
| 96 |
|
| 97 |
# Function to find product name from the predefined list using fuzzy matching
|
| 98 |
def match_product_name(text):
|
| 99 |
+
best_match = None
|
| 100 |
+
best_score = 0
|
| 101 |
+
|
| 102 |
+
# Split text into lines for detailed matching
|
| 103 |
for line in text.split("\n"):
|
| 104 |
match, score = process.extractOne(line, PRODUCT_NAMES)
|
| 105 |
+
if score > best_score: # Retain the best match with the highest score
|
| 106 |
+
best_match = match
|
| 107 |
+
best_score = score
|
| 108 |
+
|
| 109 |
+
print(f"Best Match: {best_match}, Score: {best_score}") # Debug: Log the best matching details
|
| 110 |
+
return best_match if best_score >= 70 else None # Threshold of 70 for matching
|
| 111 |
|
| 112 |
# Function to find attributes and their values
|
| 113 |
def find_attributes(text):
|
| 114 |
structured_data = {}
|
| 115 |
+
|
| 116 |
+
# Match and add product name
|
| 117 |
+
matched_product = match_product_name(text)
|
| 118 |
+
if matched_product:
|
| 119 |
+
structured_data["Productname__c"] = matched_product
|
| 120 |
+
|
| 121 |
for readable_attr, sf_attr in ATTRIBUTE_MAPPING.items():
|
| 122 |
+
pattern = rf"{re.escape(readable_attr)}[:\-]?\s*(.+)" # Match the attribute and capture its value
|
| 123 |
match = re.search(pattern, text, re.IGNORECASE)
|
| 124 |
if match:
|
| 125 |
structured_data[sf_attr] = match.group(1).strip()
|
|
|
|
| 126 |
|
| 127 |
+
return structured_data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
# Function to pull data from Salesforce MotorDataAPI
|
| 130 |
+
def pull_data_from_motor_api():
|
| 131 |
try:
|
| 132 |
sf = Salesforce(
|
| 133 |
username=SALESFORCE_USERNAME,
|
| 134 |
password=SALESFORCE_PASSWORD,
|
| 135 |
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 136 |
)
|
| 137 |
+
|
| 138 |
+
motor_data = sf.apexecute("MotorDataAPI/", method="GET")
|
| 139 |
+
return motor_data # API returns the list of records
|
| 140 |
except Exception as e:
|
| 141 |
+
print("Error pulling data from MotorDataAPI:", e)
|
| 142 |
return f"Error: {str(e)}"
|
| 143 |
|
| 144 |
+
# Function to format Salesforce data into a DataFrame
|
| 145 |
def format_salesforce_data():
|
| 146 |
+
try:
|
| 147 |
+
data = pull_data_from_motor_api()
|
| 148 |
+
if isinstance(data, list):
|
| 149 |
+
df = pd.DataFrame(data)
|
| 150 |
+
df = df[["Product_Name__c", "Modal_Name__c", "Current_Stocks__c"]]
|
| 151 |
+
return df
|
| 152 |
+
else:
|
| 153 |
+
return None
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print("Error in format_salesforce_data:", e)
|
| 156 |
+
return None
|
| 157 |
+
|
| 158 |
+
# Function to generate a bar graph from Salesforce data
|
| 159 |
+
def generate_bar_graph(df):
|
| 160 |
+
try:
|
| 161 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 162 |
+
df.plot(kind='bar', x="Product_Name__c", y="Current_Stocks__c", ax=ax, legend=False)
|
| 163 |
+
ax.set_title("Stock Distribution by Product Name")
|
| 164 |
+
ax.set_xlabel("Product Name")
|
| 165 |
+
ax.set_ylabel("Current Stocks")
|
| 166 |
+
plt.xticks(rotation=45, ha="right")
|
| 167 |
+
buffer = BytesIO()
|
| 168 |
+
plt.savefig(buffer, format="png")
|
| 169 |
+
buffer.seek(0)
|
| 170 |
+
img = Image.open(buffer)
|
| 171 |
+
return img
|
| 172 |
+
except Exception as e:
|
| 173 |
+
print("Error generating bar graph:", e)
|
| 174 |
+
return None
|
| 175 |
+
|
| 176 |
+
# Gradio Interface
|
| 177 |
+
def app():
|
| 178 |
+
df = format_salesforce_data()
|
| 179 |
+
table_component = None
|
| 180 |
+
bar_graph_component = None
|
| 181 |
+
|
| 182 |
+
if df is not None:
|
| 183 |
+
table_component = df.to_html(index=False)
|
| 184 |
+
bar_graph_image = generate_bar_graph(df)
|
| 185 |
+
if bar_graph_image:
|
| 186 |
+
bar_graph_component = bar_graph_image
|
| 187 |
+
|
| 188 |
+
return gr.TabbedInterface([
|
| 189 |
gr.Interface(
|
| 190 |
fn=process_image,
|
| 191 |
inputs=[
|
| 192 |
gr.Image(type="numpy"),
|
| 193 |
+
gr.Number(label="Quantity", value=1, interactive=True),
|
| 194 |
+
gr.Dropdown(label="Mode", choices=["Entry", "Exit"], value="Entry"),
|
| 195 |
+
gr.Radio(label="Entry Type", choices=["Sales", "Non-Sales"], value="Sales", interactive=True)
|
| 196 |
+
],
|
| 197 |
+
outputs=[
|
| 198 |
+
gr.Text(label="Image Data Viewer"),
|
| 199 |
+
gr.File(label="Data Storage Manager")
|
| 200 |
],
|
| 201 |
+
title="Processing - VENKATA RAMANA MOTORS",
|
| 202 |
+
description="Process images for Entry (Sales/Non-Sales) or Exit (Sales/Non-Sales) mode to update stock.",
|
| 203 |
),
|
| 204 |
gr.Interface(
|
| 205 |
+
fn=lambda: (table_component, bar_graph_component),
|
| 206 |
inputs=[],
|
| 207 |
+
outputs=[
|
| 208 |
+
gr.HTML(label="Salesforce Data Table"),
|
| 209 |
+
gr.Image(type="pil", label="Stock Distribution Bar Graph")
|
| 210 |
+
],
|
| 211 |
+
title="Salesforce Data",
|
| 212 |
+
description="View structured Salesforce data as a table and bar graph."
|
| 213 |
+
)
|
| 214 |
+
], ["Processing", "Salesforce Data"])
|
| 215 |
|
| 216 |
if __name__ == "__main__":
|
| 217 |
+
app().launch(share=True)
|