gopichandra commited on
Commit
98dbcff
ยท
verified ยท
1 Parent(s): bd49433

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -212,6 +212,11 @@ def process_image(image, mode, entry_type, quantity):
212
  if product_name:
213
  attributes["Product name"] = product_name
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
@@ -228,6 +233,57 @@ def export_to_salesforce(mode, entry_type, quantity, edited_df):
228
  except Exception as e:
229
  return f"โŒ Error exporting to Salesforce: {str(e)}"
230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  # Gradio Interface
232
  def app():
233
  with gr.Blocks() as demo:
@@ -243,6 +299,13 @@ def app():
243
  ok_button = gr.Button("OK")
244
  result_output = gr.Text(label="๐Ÿš€ Result")
245
 
 
 
 
 
 
 
 
246
  # Define button actions
247
  extract_button.click(
248
  fn=process_image,
@@ -254,6 +317,11 @@ def app():
254
  inputs=[mode_input, entry_type_input, quantity_input, editable_df_output],
255
  outputs=[result_output]
256
  )
 
 
 
 
 
257
 
258
  return demo
259
 
 
212
  if product_name:
213
  attributes["Product name"] = product_name
214
 
215
+ # Ensure fixed attributes are present
216
+ for fixed_attr in ["Stage", "H.P.", "Product name", "Model"]:
217
+ if fixed_attr not in attributes:
218
+ attributes[fixed_attr] = ""
219
+
220
  # Convert attributes to DataFrame for editing
221
  df = pd.DataFrame(list(attributes.items()), columns=["Attribute", "Value"])
222
  return f"Extracted Text:\n{extracted_text}", df, None
 
233
  except Exception as e:
234
  return f"โŒ Error exporting to Salesforce: {str(e)}"
235
 
236
+ # Function to pull structured data from Salesforce and display as a table
237
+ def pull_data_from_salesforce(data_type):
238
+ try:
239
+ sf = Salesforce(
240
+ username=SALESFORCE_USERNAME,
241
+ password=SALESFORCE_PASSWORD,
242
+ security_token=SALESFORCE_SECURITY_TOKEN
243
+ )
244
+
245
+ if data_type == "Inventory":
246
+ query = "SELECT Productname__c, Current_Stocks__c, soldstock__c FROM Inventory_Management__c LIMIT 100"
247
+ else:
248
+ query = "SELECT Productname__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 "No data found in Salesforce.", None, None, None
255
+
256
+ df = pd.DataFrame(records)
257
+ df = df.drop(columns=['attributes'], errors='ignore')
258
+
259
+ # Rename columns for better readability
260
+ df.rename(columns={
261
+ "Productname__c": "Product Name",
262
+ "Current_Stocks__c": "Current Stocks",
263
+ "Current_Stock__c": "Current Stocks",
264
+ "soldstock__c": "Sold Stock"
265
+ }, inplace=True)
266
+
267
+ excel_path = "salesforce_data.xlsx"
268
+ df.to_excel(excel_path, index=False)
269
+
270
+ # Generate interactive vertical bar graph using Matplotlib
271
+ fig, ax = plt.subplots(figsize=(12, 8))
272
+ df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
273
+ ax.set_title("Stock Distribution by Product Name")
274
+ ax.set_xlabel("Product Name")
275
+ ax.set_ylabel("Current Stocks")
276
+ plt.xticks(rotation=45, ha="right", fontsize=10)
277
+ plt.tight_layout()
278
+ buffer = BytesIO()
279
+ plt.savefig(buffer, format="png")
280
+ buffer.seek(0)
281
+ img = Image.open(buffer)
282
+
283
+ return df, excel_path, img
284
+ except Exception as e:
285
+ return f"Error fetching data: {str(e)}", None, None, None
286
+
287
  # Gradio Interface
288
  def app():
289
  with gr.Blocks() as demo:
 
299
  ok_button = gr.Button("OK")
300
  result_output = gr.Text(label="๐Ÿš€ Result")
301
 
302
+ with gr.Tab("๐Ÿ“Š Salesforce Data"):
303
+ data_type_input = gr.Dropdown(label="Select Data Type", choices=["Inventory", "Unbilling"], value="Inventory")
304
+ pull_button = gr.Button("Pull Data from Salesforce")
305
+ salesforce_data_output = gr.Dataframe(label="๐Ÿ“Š Salesforce Data")
306
+ excel_download_output = gr.File(label="๐Ÿ“ฅ Download Excel")
307
+ graph_output = gr.Image(label="๐Ÿ“ˆ Stock Distribution Graph")
308
+
309
  # Define button actions
310
  extract_button.click(
311
  fn=process_image,
 
317
  inputs=[mode_input, entry_type_input, quantity_input, editable_df_output],
318
  outputs=[result_output]
319
  )
320
+ pull_button.click(
321
+ fn=pull_data_from_salesforce,
322
+ inputs=[data_type_input],
323
+ outputs=[salesforce_data_output, excel_download_output, graph_output]
324
+ )
325
 
326
  return demo
327