SuriRaja commited on
Commit
a40427a
·
verified ·
1 Parent(s): a147885

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -21
app.py CHANGED
@@ -196,11 +196,28 @@ def interact_with_salesforce(mode, entry_type, quantity, attributes):
196
  except Exception as e:
197
  return f"❌ Error interacting with Salesforce: {str(e)}"
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  # Function to process image, extract attributes, and allow editing
200
  def process_image(image, mode, entry_type, quantity):
201
  extracted_text = extract_text(image)
202
  if not extracted_text:
203
- return "No text detected in the image.", None, None
 
204
 
205
  product_name = match_product_name(extracted_text)
206
  attributes = extract_attributes(extracted_text)
@@ -214,19 +231,7 @@ def process_image(image, mode, entry_type, quantity):
214
 
215
  # Convert attributes to DataFrame for editing
216
  df = pd.DataFrame(list(attributes.items()), columns=["Attribute", "Value"])
217
- return f"Extracted Text:\n{extracted_text}", df, None
218
-
219
- # Function to handle edited attributes and export to Salesforce
220
- def export_to_salesforce(mode, entry_type, quantity, edited_df):
221
- try:
222
- # Convert edited DataFrame back to dictionary
223
- edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
224
-
225
- # Export to Salesforce
226
- message = interact_with_salesforce(mode, entry_type, quantity, edited_attributes)
227
- return message
228
- except Exception as e:
229
- return f"❌ Error exporting to Salesforce: {str(e)}"
230
 
231
  # Function to pull structured data from Salesforce and display as a table
232
  def pull_data_from_salesforce(data_type):
@@ -236,21 +241,23 @@ def pull_data_from_salesforce(data_type):
236
  password=SALESFORCE_PASSWORD,
237
  security_token=SALESFORCE_SECURITY_TOKEN
238
  )
239
-
240
  if data_type == "Inventory":
241
  query = "SELECT Productname__c,Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c FROM Inventory_Management__c LIMIT 100"
242
  else:
243
  query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c FROM Un_Billable__c LIMIT 100"
244
-
245
  response = sf.query_all(query)
246
  records = response.get("records", [])
247
-
248
  if not records:
249
- return "No data found in Salesforce.", None, None, None
250
-
 
 
251
  df = pd.DataFrame(records)
252
  df = df.drop(columns=['attributes'], errors='ignore')
253
-
254
  # Rename columns for better readability
255
  df.rename(columns={
256
  "Productname__c": "Product Name",
@@ -261,9 +268,33 @@ def pull_data_from_salesforce(data_type):
261
  "Current_Stock__c": "Current Stocks",
262
  "soldstock__c": "Sold Stock"
263
  }, inplace=True)
264
-
265
  excel_path = "salesforce_data.xlsx"
266
  df.to_excel(excel_path, index=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
  # Generate interactive vertical bar graph using Matplotlib
269
  fig, ax = plt.subplots(figsize=(12, 8))
 
196
  except Exception as e:
197
  return f"❌ Error interacting with Salesforce: {str(e)}"
198
 
199
+
200
+
201
+ # Function to handle edited attributes and export to Salesforce
202
+ def export_to_salesforce(mode, entry_type, quantity, edited_df):
203
+ try:
204
+ # Convert edited DataFrame back to dictionary
205
+ edited_attributes = dict(zip(edited_df["Attribute"], edited_df["Value"]))
206
+
207
+ # Export to Salesforce
208
+ message = interact_with_salesforce(mode, entry_type, quantity, edited_attributes)
209
+ return message
210
+ except Exception as e:
211
+ return f"❌ Error exporting to Salesforce: {str(e)}"
212
+
213
+ # ... [unchanged imports and setup code above]
214
+
215
  # Function to process image, extract attributes, and allow editing
216
  def process_image(image, mode, entry_type, quantity):
217
  extracted_text = extract_text(image)
218
  if not extracted_text:
219
+ # Return defaults matching: Text, Dataframe, Text
220
+ return "No text detected in the image.", pd.DataFrame(columns=["Attribute", "Value"]), ""
221
 
222
  product_name = match_product_name(extracted_text)
223
  attributes = extract_attributes(extracted_text)
 
231
 
232
  # Convert attributes to DataFrame for editing
233
  df = pd.DataFrame(list(attributes.items()), columns=["Attribute", "Value"])
234
+ return f"Extracted Text:\n{extracted_text}", df, ""
 
 
 
 
 
 
 
 
 
 
 
 
235
 
236
  # Function to pull structured data from Salesforce and display as a table
237
  def pull_data_from_salesforce(data_type):
 
241
  password=SALESFORCE_PASSWORD,
242
  security_token=SALESFORCE_SECURITY_TOKEN
243
  )
244
+
245
  if data_type == "Inventory":
246
  query = "SELECT Productname__c,Model__c, H_p__c, Stage__c, Current_Stocks__c, soldstock__c FROM Inventory_Management__c LIMIT 100"
247
  else:
248
  query = "SELECT Productname__c, Model__c, H_p__c, Stage__c, Current_Stock__c, soldstock__c FROM Un_Billable__c LIMIT 100"
249
+
250
  response = sf.query_all(query)
251
  records = response.get("records", [])
252
+
253
  if not records:
254
+ # Return defaults: Dataframe, FilePath, Image
255
+ empty_df = pd.DataFrame(columns=["Product Name", "Model", "H.P", "Stage", "Current Stocks", "Sold Stock"])
256
+ return empty_df, "", None
257
+
258
  df = pd.DataFrame(records)
259
  df = df.drop(columns=['attributes'], errors='ignore')
260
+
261
  # Rename columns for better readability
262
  df.rename(columns={
263
  "Productname__c": "Product Name",
 
268
  "Current_Stock__c": "Current Stocks",
269
  "soldstock__c": "Sold Stock"
270
  }, inplace=True)
271
+
272
  excel_path = "salesforce_data.xlsx"
273
  df.to_excel(excel_path, index=False)
274
+
275
+ # Generate vertical bar graph using Matplotlib
276
+ fig, ax = plt.subplots(figsize=(12, 8))
277
+ df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
278
+ ax.set_title("Stock Distribution by Product Name")
279
+ ax.set_xlabel("Product Name")
280
+ ax.set_ylabel("Current Stocks")
281
+ plt.xticks(rotation=45, ha="right", fontsize=10)
282
+ plt.tight_layout()
283
+ buffer = BytesIO()
284
+ plt.savefig(buffer, format="png")
285
+ buffer.seek(0)
286
+ img = Image.open(buffer)
287
+
288
+ return df, excel_path, img
289
+
290
+ except Exception as e:
291
+ # Return safe defaults if something breaks
292
+ empty_df = pd.DataFrame(columns=["Product Name", "Model", "H.P", "Stage", "Current Stocks", "Sold Stock"])
293
+ return empty_df, "", None
294
+
295
+ # Rest of your code remains unchanged below
296
+ # (export_to_salesforce, app(), __main__)
297
+
298
 
299
  # Generate interactive vertical bar graph using Matplotlib
300
  fig, ax = plt.subplots(figsize=(12, 8))