Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -253,35 +253,44 @@ def format_salesforce_data():
|
|
| 253 |
print(f"Error fetching data: {e}")
|
| 254 |
return None
|
| 255 |
|
| 256 |
-
# Function to generate a bar graph
|
| 257 |
def generate_dynamic_bar_graph(df, x_column, y_column):
|
| 258 |
-
"""
|
| 259 |
-
Generates a dynamic bar graph from a DataFrame based on the specified x and y columns.
|
| 260 |
-
The function will automatically adjust the graph based on the column names.
|
| 261 |
-
"""
|
| 262 |
try:
|
| 263 |
-
# Check if the DataFrame has the specified columns
|
| 264 |
if x_column not in df.columns or y_column not in df.columns:
|
| 265 |
return "Error: Specified columns not found in the DataFrame."
|
| 266 |
-
|
| 267 |
-
#
|
| 268 |
-
fig, ax = plt.subplots(figsize=(
|
| 269 |
-
|
| 270 |
-
ax.set_title(f"{y_column}
|
| 271 |
-
ax.set_xlabel(x_column)
|
| 272 |
-
ax.set_ylabel(y_column)
|
| 273 |
-
plt.xticks(rotation=
|
|
|
|
| 274 |
plt.tight_layout()
|
| 275 |
|
| 276 |
-
# Save
|
| 277 |
buffer = BytesIO()
|
| 278 |
plt.savefig(buffer, format="png")
|
| 279 |
buffer.seek(0)
|
| 280 |
-
|
| 281 |
-
return img
|
| 282 |
except Exception as e:
|
| 283 |
return f"Error generating graph: {e}"
|
| 284 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
# Unified function to handle image processing and Salesforce interaction
|
| 286 |
def process_image(image, mode, entry_type, quantity):
|
| 287 |
extracted_text = extract_text(image)
|
|
|
|
| 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):
|
| 296 |
extracted_text = extract_text(image)
|