Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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)
|
| 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
|
| 257 |
-
def
|
| 258 |
try:
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 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 |
-
|
|
|
|
| 277 |
except Exception as e:
|
| 278 |
-
return
|
| 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 |
-
|
| 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 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 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 |
-
|
| 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 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|