gopichandra commited on
Commit
c79235e
Β·
verified Β·
1 Parent(s): 4f93080

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -11
app.py CHANGED
@@ -223,13 +223,14 @@ import matplotlib.pyplot as plt
223
  from io import BytesIO
224
  from PIL import Image
225
  from simple_salesforce import Salesforce
 
226
 
227
  # πŸ“Œ Salesforce Credentials
228
  SALESFORCE_USERNAME = "venkatramana@sandbox.com"
229
  SALESFORCE_PASSWORD = "Venkat12345@"
230
  SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
231
 
232
- # Function to Fetch and Display Salesforce Data
233
  def fetch_salesforce_data():
234
  try:
235
  sf = Salesforce(
@@ -238,40 +239,76 @@ def fetch_salesforce_data():
238
  security_token=SALESFORCE_SECURITY_TOKEN
239
  )
240
 
 
241
  query = "SELECT Product_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
242
  response = sf.query(query)
243
  records = response.get('records', [])
244
 
245
  if not records:
246
- return "<p>No Data Found</p>", None
247
 
248
- # Convert to DataFrame
249
  df = pd.DataFrame(records)
250
  df.rename(columns={'Product_Name__c': 'Product Name', 'Current_Stocks__c': 'Current Stocks'}, inplace=True)
251
  df.drop(columns=['attributes'], inplace=True, errors='ignore')
252
 
253
- return df.to_html(index=False), generate_inventory_graph(df)
 
 
 
 
 
 
254
 
255
  except Exception as e:
256
- return f"Error fetching data: {str(e)}", None
257
 
258
- # Function to Generate Inventory Graph
259
  def generate_inventory_graph(df):
260
  try:
261
  fig, ax = plt.subplots(figsize=(12, 6))
262
- df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
263
- ax.set_title("Inventory Stock Distribution")
264
- ax.set_xlabel("Product Name")
265
- ax.set_ylabel("Stock Quantity")
266
  plt.xticks(rotation=45, ha="right", fontsize=10)
267
  plt.tight_layout()
 
 
268
  buffer = BytesIO()
269
  plt.savefig(buffer, format="png")
270
  buffer.seek(0)
 
271
  return Image.open(buffer)
272
 
273
  except Exception as e:
274
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
  # Unified function to handle image processing and Salesforce interaction
277
  def process_image(image, mode, entry_type, quantity):
 
223
  from io import BytesIO
224
  from PIL import Image
225
  from simple_salesforce import Salesforce
226
+ import gradio as gr
227
 
228
  # πŸ“Œ Salesforce Credentials
229
  SALESFORCE_USERNAME = "venkatramana@sandbox.com"
230
  SALESFORCE_PASSWORD = "Venkat12345@"
231
  SALESFORCE_SECURITY_TOKEN = "GhcJJmjBEefdnukJoz4CAQlR"
232
 
233
+ # πŸ“Œ Function to Fetch and Display Salesforce Data (Stock Table & Graph)
234
  def fetch_salesforce_data():
235
  try:
236
  sf = Salesforce(
 
239
  security_token=SALESFORCE_SECURITY_TOKEN
240
  )
241
 
242
+ # SOQL Query to fetch stock details
243
  query = "SELECT Product_Name__c, Current_Stocks__c FROM Inventory_Management__c LIMIT 100"
244
  response = sf.query(query)
245
  records = response.get('records', [])
246
 
247
  if not records:
248
+ return "<p>No Data Found</p>", None # Return empty result if no records found
249
 
250
+ # Convert Salesforce Data into Pandas DataFrame
251
  df = pd.DataFrame(records)
252
  df.rename(columns={'Product_Name__c': 'Product Name', 'Current_Stocks__c': 'Current Stocks'}, inplace=True)
253
  df.drop(columns=['attributes'], inplace=True, errors='ignore')
254
 
255
+ # Convert DataFrame to HTML Table
256
+ table_html = df.to_html(index=False)
257
+
258
+ # Generate Inventory Graph
259
+ graph_image = generate_inventory_graph(df)
260
+
261
+ return table_html, graph_image
262
 
263
  except Exception as e:
264
+ return f"❌ Error fetching data: {str(e)}", None
265
 
266
+ # πŸ“Š Function to Generate Inventory Stock Graph
267
  def generate_inventory_graph(df):
268
  try:
269
  fig, ax = plt.subplots(figsize=(12, 6))
270
+ df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False, color="royalblue")
271
+ ax.set_title("Inventory Stock Distribution", fontsize=14, fontweight="bold")
272
+ ax.set_xlabel("Product Name", fontsize=12)
273
+ ax.set_ylabel("Stock Quantity", fontsize=12)
274
  plt.xticks(rotation=45, ha="right", fontsize=10)
275
  plt.tight_layout()
276
+
277
+ # Save the graph to a buffer
278
  buffer = BytesIO()
279
  plt.savefig(buffer, format="png")
280
  buffer.seek(0)
281
+
282
  return Image.open(buffer)
283
 
284
  except Exception as e:
285
+ return None # Return None if graph generation fails
286
+
287
+ # πŸ“Œ Gradio UI for Viewing Salesforce Data (Table & Graph)
288
+ def app():
289
+ with gr.Blocks() as interface:
290
+ gr.Markdown("<h1>πŸ“Š Salesforce Stock & Inventory Overview</h1>")
291
+
292
+ with gr.Row():
293
+ with gr.Column():
294
+ gr.Markdown("<h3>πŸ“‹ Stock Data Table</h3>")
295
+ salesforce_table = gr.HTML(label="πŸ“¦ Inventory Table")
296
+
297
+ with gr.Column():
298
+ gr.Markdown("<h3>πŸ“ˆ Inventory Stock Graph</h3>")
299
+ salesforce_graph = gr.Image(type="pil", label="πŸ“‰ Stock Graph")
300
+
301
+ generate_button = gr.Button("⚑ Fetch Data")
302
+
303
+ # Clicking "Fetch Data" will display the table & graph
304
+ generate_button.click(fn=fetch_salesforce_data, inputs=[], outputs=[salesforce_table, salesforce_graph])
305
+
306
+ return interface
307
+
308
+ # πŸš€ Launch the Gradio App
309
+ if __name__ == "__main__":
310
+ interface = app()
311
+ interface.launch(share=True)
312
 
313
  # Unified function to handle image processing and Salesforce interaction
314
  def process_image(image, mode, entry_type, quantity):