gopichandra commited on
Commit
a48545d
Β·
verified Β·
1 Parent(s): 2902c91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -197
app.py CHANGED
@@ -236,7 +236,7 @@ def pull_data_from_motor_api():
236
  def format_salesforce_data():
237
  try:
238
  data = pull_data_from_motor_api()
239
- if isinstance(data, list) and len(data) > 0:
240
  df = pd.DataFrame(data)
241
  df = df.rename(columns={
242
  "Product_Name__c": "Product Name",
@@ -247,49 +247,27 @@ def format_salesforce_data():
247
  df = df[["Product Name", "Model Name", "Current Stocks"]]
248
  return df
249
  else:
250
- print("No data or empty list returned from API.")
251
  return None
252
  except Exception as e:
253
- print(f"Error fetching data: {e}")
254
  return None
255
 
256
- # Function to generate a dynamic bar graph
257
- def generate_dynamic_bar_graph(df, x_column, y_column):
258
  try:
259
- if x_column not in df.columns or y_column not in df.columns:
260
- return "Error: Specified columns not found in the DataFrame."
261
-
262
- # Create a bar graph with simple visualization
263
- fig, ax = plt.subplots(figsize=(10, 6))
264
- ax.bar(df[x_column], df[y_column], color='royalblue', edgecolor='black')
265
- ax.set_title(f"{y_column} by {x_column}", fontsize=14, fontweight='bold')
266
- ax.set_xlabel(x_column, fontsize=12)
267
- ax.set_ylabel(y_column, fontsize=12)
268
- plt.xticks(rotation=30, ha="right", fontsize=10)
269
- plt.grid(axis='y', linestyle='--', alpha=0.7)
270
  plt.tight_layout()
271
-
272
- # Save and return image
273
  buffer = BytesIO()
274
  plt.savefig(buffer, format="png")
275
  buffer.seek(0)
276
- return Image.open(buffer)
 
277
  except Exception as e:
278
- return f"Error generating graph: {e}"
279
-
280
- # Function to fetch Salesforce data and return table and graph
281
- def generate_salesforce_data():
282
- df = pd.DataFrame({
283
- "Category": ["A", "B", "C", "D"],
284
- "Stock": [150, 200, 100, 80]
285
- }) # Simulated data; replace with real Salesforce data
286
-
287
- if df is not None:
288
- table_component = df.to_html(index=False)
289
- bar_graph_image = generate_dynamic_bar_graph(df, "Category", "Stock")
290
- return table_component, bar_graph_image
291
- return "<p>No Data Available</p>", None
292
-
293
 
294
  # Unified function to handle image processing and Salesforce interaction
295
  def process_image(image, mode, entry_type, quantity):
@@ -308,172 +286,147 @@ def process_image(image, mode, entry_type, quantity):
308
  numbered_output = "\n".join([f"{key}: {value}" for key, value in attributes.items()])
309
  return f"Extracted Text:\n{extracted_text}\n\nAttributes and Values:\n{numbered_output}", message
310
 
311
-
312
- import gradio as gr
313
- import numpy as np
314
- from PIL import Image
315
-
316
- # Function to fetch and return Salesforce data
317
- def generate_salesforce_data():
318
  df = format_salesforce_data()
 
 
 
319
  if df is not None:
320
  table_component = df.to_html(index=False)
321
  bar_graph_image = generate_bar_graph(df)
322
- return table_component, bar_graph_image
323
- return "<p>No Data Available</p>", None
324
-
325
- def app():
326
- with gr.Blocks(css="""
327
- /* General Styling */
328
- .gradio-container {
329
- background: #0b0f29; /* Deep Dark Blue */
330
- font-family: 'Poppins', sans-serif;
331
- color: white;
332
- padding: 10px;
333
- }
334
-
335
- h1, h2, h3 {
336
- text-align: center;
337
- color: white;
338
- font-weight: bold;
339
- text-shadow: 0px 0px 10px rgba(255,255,255,0.8);
340
- }
341
-
342
- /* Cards & Elements */
343
- .card {
344
- background: rgba(255, 255, 255, 0.1);
345
- border-radius: 15px;
346
- padding: 20px;
347
- width: 100%;
348
- max-width: 900px; /* Prevents extra-wide tables */
349
- margin: auto; /* Centers the table */
350
- box-shadow: 0px 0px 25px rgba(0, 255, 255, 0.6);
351
- transition: 0.3s;
352
- }
353
-
354
- .card:hover {
355
- box-shadow: 0px 0px 30px rgba(0, 255, 255, 0.9);
356
- }
357
-
358
- /* Buttons */
359
- .gradio-button {
360
- background: linear-gradient(to right, #ff416c, #ff4b2b);
361
- color: white !important;
362
- border-radius: 10px !important;
363
- font-weight: bold;
364
- padding: 12px;
365
- transition: all 0.3s ease-in-out;
366
- box-shadow: 0px 0px 20px rgba(255, 0, 255, 0.6);
367
- }
368
-
369
- .gradio-button:hover {
370
- background: linear-gradient(to right, #ff4b2b, #ff416c);
371
- box-shadow: 0px 0px 30px rgba(255, 0, 255, 0.9);
372
- }
373
-
374
- /* Tabs */
375
- .gradio-tab {
376
- font-size: 16px;
377
- color: white;
378
- font-weight: bold;
379
- text-shadow: 0px 0px 10px rgba(255, 255, 255, 0.6);
380
- }
381
-
382
- /* Scrollable Table */
383
- .table-container {
384
- width: 100%;
385
- max-width: 900px;
386
- max-height: 400px; /* Controls table height */
387
- overflow-y: auto; /* Enables vertical scrolling */
388
- overflow-x: auto; /* Enables horizontal scrolling if needed */
389
- display: flex;
390
- justify-content: center;
391
- margin: auto;
392
- }
393
-
394
- table {
395
- width: 100%;
396
- border-collapse: collapse;
397
- background: rgba(255, 255, 255, 0.1);
398
- color: white;
399
- min-width: 600px;
400
- }
401
-
402
- th, td {
403
- padding: 12px;
404
- text-align: left;
405
- border-bottom: 1px solid rgba(255, 255, 255, 0.2);
406
- font-size: 14px;
407
- }
408
-
409
- th {
410
- background: rgba(0, 255, 255, 0.2);
411
  }
412
-
413
- /* Mobile View */
414
- @media (max-width: 768px) {
415
- .gradio-container {
416
- padding: 10px;
417
- }
418
-
419
- .card {
420
- width: 100%;
421
- max-width: 100%;
422
- padding: 10px;
423
- }
424
-
425
- .table-container {
426
- max-height: 350px; /* Adjusts for mobile */
427
- overflow-y: scroll; /* Ensures vertical scrolling */
428
- }
429
-
430
- table {
431
- width: 100%;
432
- min-width: 100%;
433
- }
434
-
435
- th, td {
436
- font-size: 12px;
437
- padding: 8px;
438
- }
439
  }
440
-
441
- """) as interface:
442
-
443
- gr.Markdown("<h1>🏒 VENKATARAMANA MOTORS</h1>")
444
-
445
- with gr.Tab("πŸ“₯ Stock Entry & Processing"):
446
- with gr.Row():
447
- with gr.Column():
448
- gr.Markdown("<h3>πŸ“Œ Upload & Process</h3>")
449
- image_input = gr.Image(type="numpy", label="πŸ“„ Upload Image", elem_classes="card")
450
- mode_dropdown = gr.Dropdown(label="πŸ“Œ Mode", choices=["Entry", "Exit"], value="Entry", elem_classes="card")
451
- entry_type_radio = gr.Radio(label="πŸ“¦ Entry Type", choices=["Sales", "Non-Sales"], value="Sales", elem_classes="card")
452
- quantity_input = gr.Number(label="πŸ”’ Quantity", value=1, interactive=True, elem_classes="card")
453
-
454
- with gr.Column():
455
- gr.Markdown("<h3>πŸ“Š Processed Data</h3>")
456
- image_view = gr.Text(label="πŸ“œ Image Data Viewer", interactive=False, elem_classes="card")
457
- result_output = gr.Text(label="πŸ“ Processed Result", interactive=False, elem_classes="card")
458
- submit_button = gr.Button("πŸ” Process Image", elem_id="process-btn", elem_classes="gradio-button")
459
-
460
- with gr.Tab("πŸ“Š Salesforce Data Overview"):
461
- gr.Markdown("<h3>πŸ“‘ Stock Table</h3>")
462
- with gr.Row(elem_classes="table-container"):
463
- salesforce_table = gr.HTML(label="πŸ“¦ Salesforce Data Table", elem_classes="card")
464
-
465
- gr.Markdown("<h3>πŸ“ˆ Inventory Analytics</h3>")
466
- with gr.Row():
467
- salesforce_graph = gr.Image(type="pil", label="πŸ“‰ Stock Distribution Bar Graph", elem_classes="card")
468
-
469
- generate_button = gr.Button("⚑ Generate Data", elem_classes="gradio-button")
470
-
471
- # Clicking "Generate Data" fetches table & graph
472
- generate_button.click(fn=generate_salesforce_data, inputs=[], outputs=[salesforce_table, salesforce_graph])
473
-
474
- submit_button.click(fn=process_image, inputs=[image_input, mode_dropdown, entry_type_radio, quantity_input], outputs=[image_view, result_output])
475
-
476
- return interface
477
-
478
- interface = app()
479
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  def format_salesforce_data():
237
  try:
238
  data = pull_data_from_motor_api()
239
+ if isinstance(data, list):
240
  df = pd.DataFrame(data)
241
  df = df.rename(columns={
242
  "Product_Name__c": "Product Name",
 
247
  df = df[["Product Name", "Model Name", "Current Stocks"]]
248
  return df
249
  else:
 
250
  return None
251
  except Exception as e:
 
252
  return None
253
 
254
+ # Function to generate a bar graph from Salesforce data
255
+ def generate_bar_graph(df):
256
  try:
257
+ fig, ax = plt.subplots(figsize=(12, 8))
258
+ df.plot(kind='bar', x="Product Name", y="Current Stocks", ax=ax, legend=False)
259
+ ax.set_title("Stock Distribution by Product Name")
260
+ ax.set_xlabel("Product Name")
261
+ ax.set_ylabel("Current Stocks")
262
+ plt.xticks(rotation=45, ha="right", fontsize=10)
 
 
 
 
 
263
  plt.tight_layout()
 
 
264
  buffer = BytesIO()
265
  plt.savefig(buffer, format="png")
266
  buffer.seek(0)
267
+ img = Image.open(buffer)
268
+ return img
269
  except Exception as e:
270
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
 
272
  # Unified function to handle image processing and Salesforce interaction
273
  def process_image(image, mode, entry_type, quantity):
 
286
  numbered_output = "\n".join([f"{key}: {value}" for key, value in attributes.items()])
287
  return f"Extracted Text:\n{extracted_text}\n\nAttributes and Values:\n{numbered_output}", message
288
 
289
+ # Gradio Interface
290
+ def app():
 
 
 
 
 
291
  df = format_salesforce_data()
292
+ table_component = None
293
+ bar_graph_component = None
294
+
295
  if df is not None:
296
  table_component = df.to_html(index=False)
297
  bar_graph_image = generate_bar_graph(df)
298
+ if bar_graph_image:
299
+ bar_graph_component = bar_graph_image
300
+
301
+ return gr.TabbedInterface([
302
+ gr.Interface(
303
+ fn=process_image,
304
+ inputs=[
305
+ gr.Image(type="numpy", label="πŸ“„α΄œα΄˜ΚŸα΄α΄€α΄… Ιͺᴍᴀɒᴇ"),
306
+ gr.Dropdown(label="πŸ“ŒMode", choices=["ᴇɴᴛʀʏ", "ᴇxΙͺα΄›"], value="Entry"),
307
+ gr.Radio(label="πŸ“¦Entry Type", choices=["Sales", "Non-Sales"], value="Sales"),
308
+ gr.Number(label="πŸ”’Quantity", value=1, interactive=True),
309
+ ],
310
+ outputs=[
311
+ gr.Text(label="Ιͺᴍᴀɒᴇ α΄…α΄€α΄›α΄€ α΄ Ιͺᴇᴑᴇʀ"),
312
+ gr.Text(label="πŸ“οΌ²οΌ₯οΌ³οΌ΅οΌ¬οΌ΄")
313
+ ],
314
+ title="𝑽𝑬𝑡𝑲𝑨𝑻𝑨𝑹𝑨𝑴𝑨𝑡𝑨 𝑴𝑢𝑻𝑢𝑹𝑺",
315
+ description="πŸ“¦πˆππ•π„ππ“πŽπ‘π˜ πŒπ€ππ€π†π„πŒπ„ππ“"
316
+ ),
317
+ gr.Interface(
318
+ fn=lambda: (table_component, bar_graph_component),
319
+ inputs=[],
320
+ outputs=[
321
+ gr.HTML(label="Salesforce Data Table"),
322
+ gr.Image(type="pil", label="Stock Distribution Bar Graph")
323
+ ],
324
+ title="Salesforce Data",
325
+ description="View structured Salesforce data as a table and bar graph."
326
+ )
327
+ ], ["Processing", "Salesforce Data"])
328
+ # 🎨 Bright and Gradient UI with Updated Coloring
329
+ css = """
330
+ body {
331
+ background: linear-gradient(135deg, #282c34, #4b79a1);
332
+ font-family: 'Poppins', sans-serif;
333
+ }
334
+
335
+ .gradio-container {
336
+ border-radius: 20px;
337
+ padding: 30px;
338
+ background: rgba(255, 255, 255, 0.2);
339
+ backdrop-filter: blur(10px);
340
+ box-shadow: 0px 8px 30px rgba(0, 123, 255, 0.4);
341
+ border: 2px solid rgba(255, 255, 255, 0.4);
342
+ }
343
+
344
+ .gradio-title {
345
+ font-size: 48px;
346
+ font-weight: bold;
347
+ text-align: center;
348
+ background: linear-gradient(90deg, #ff7eb3, #ff758c);
349
+ -webkit-background-clip: text;
350
+ -webkit-text-fill-color: transparent;
351
+ text-shadow: 5px 5px 20px rgba(255, 105, 180, 0.6);
352
+ margin-bottom: 20px;
353
+ animation: glow 2s infinite alternate;
354
+ }
355
+
356
+ @keyframes glow {
357
+ from {
358
+ text-shadow: 5px 5px 30px rgba(255, 87, 134, 0.6);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  }
360
+ to {
361
+ text-shadow: 6px 6px 40px rgba(255, 54, 90, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  }
363
+ }
364
+
365
+ .gradio-box {
366
+ border-radius: 15px;
367
+ padding: 25px;
368
+ background: linear-gradient(135deg, #6a11cb, #2575fc);
369
+ box-shadow: 0px 6px 25px rgba(30, 144, 255, 0.6);
370
+ border: 2px solid #6a5acd;
371
+ color: white;
372
+ font-size: 18px;
373
+ }
374
+
375
+ .gradio-button {
376
+ border-radius: 12px;
377
+ padding: 18px 36px;
378
+ font-size: 20px;
379
+ font-weight: bold;
380
+ color: #fff;
381
+ background: linear-gradient(135deg, #1e90ff, #00bfff);
382
+ box-shadow: 0px 6px 25px rgba(0, 191, 255, 0.6);
383
+ transition: all 0.3s ease-in-out;
384
+ }
385
+
386
+ .gradio-button:hover {
387
+ background: linear-gradient(135deg, #00bfff, #1e90ff);
388
+ box-shadow: 0px 10px 35px rgba(0, 191, 255, 0.9);
389
+ transform: scale(1.1);
390
+ }
391
+
392
+ .gradio-input {
393
+ border-radius: 10px;
394
+ padding: 16px;
395
+ font-size: 18px;
396
+ background: rgba(255, 255, 255, 0.3);
397
+ border: 2px solid rgba(0, 123, 255, 0.5);
398
+ color: #fff;
399
+ transition: 0.3s;
400
+ }
401
+
402
+ .gradio-input:focus {
403
+ border: 2px solid #1e90ff;
404
+ outline: none;
405
+ box-shadow: 0px 5px 20px rgba(30, 144, 255, 0.6);
406
+ }
407
+
408
+ .gradio-output {
409
+ background: linear-gradient(135deg, #f39c12, #e74c3c);
410
+ padding: 22px;
411
+ border-radius: 15px;
412
+ color: white;
413
+ font-size: 20px;
414
+ text-align: center;
415
+ border: 2px solid #f39c12;
416
+ }
417
+
418
+ .gradio-file {
419
+ background: linear-gradient(135deg, #8e44ad, #3498db);
420
+ color: white;
421
+ padding: 16px;
422
+ border-radius: 15px;
423
+ text-align: center;
424
+ font-size: 18px;
425
+ }
426
+ """
427
+
428
+
429
+
430
+
431
+ if __name__ == "__main__":
432
+ app().launch(share=True)