Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,16 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
import os
|
| 4 |
import logging
|
|
|
|
|
|
|
| 5 |
import base64
|
| 6 |
import pdfkit
|
| 7 |
import shutil
|
| 8 |
from graphviz import Digraph
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Set up logging for debugging
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
|
|
@@ -20,11 +24,6 @@ def get_wkhtmltopdf_path():
|
|
| 20 |
return None
|
| 21 |
|
| 22 |
def process_uploaded_file(file):
|
| 23 |
-
"""
|
| 24 |
-
Process the file input from Gradio.
|
| 25 |
-
If the file is a dict, write its "data" content to a temporary file.
|
| 26 |
-
If it's a string, ensure it is not a directory.
|
| 27 |
-
"""
|
| 28 |
logging.info(f"Processing uploaded file: {file}")
|
| 29 |
if isinstance(file, str):
|
| 30 |
if os.path.abspath(file) == os.getcwd():
|
|
@@ -77,39 +76,37 @@ def generate_chart(file, title):
|
|
| 77 |
logging.error(f"Error reading CSV: {e}")
|
| 78 |
return None, f"Error reading CSV: {e}"
|
| 79 |
|
| 80 |
-
# Validate required columns
|
| 81 |
expected_columns = {"Name", "Role", "Reporting To"}
|
| 82 |
if not expected_columns.issubset(set(df.columns)):
|
| 83 |
return None, "CSV must contain Name, Role, and Reporting To columns."
|
| 84 |
|
| 85 |
-
# Create a Graphviz Digraph for the
|
| 86 |
dot = Digraph(comment=title, format='png')
|
| 87 |
-
dot.attr(rankdir='TB')
|
| 88 |
dot.attr('node', shape='box', style='rounded,filled', fillcolor='lightblue',
|
| 89 |
fontsize='10', fontname='Helvetica', margin='0.1')
|
| 90 |
|
| 91 |
-
# Add nodes
|
| 92 |
for idx, row in df.iterrows():
|
| 93 |
-
node_id = row['Name'] # Assumes names are unique
|
| 94 |
label = f"{row['Name']}\n({row['Role']})"
|
| 95 |
dot.node(node_id, label=label)
|
| 96 |
|
| 97 |
-
# Add edges
|
| 98 |
for idx, row in df.iterrows():
|
| 99 |
if pd.notna(row['Reporting To']) and row['Reporting To'] != "":
|
| 100 |
manager = row['Reporting To']
|
| 101 |
subordinate = row['Name']
|
| 102 |
-
# If the manager is not in the CSV, add them as a node with just their name.
|
| 103 |
if manager not in df['Name'].values:
|
| 104 |
dot.node(manager, label=manager)
|
| 105 |
dot.edge(manager, subordinate)
|
| 106 |
|
| 107 |
-
# Render
|
| 108 |
output_path = "/tmp/chart"
|
| 109 |
dot.render(output_path, cleanup=True)
|
| 110 |
chart_path = output_path + ".png"
|
| 111 |
|
| 112 |
-
#
|
| 113 |
with open(chart_path, "rb") as img_file:
|
| 114 |
encoded = base64.b64encode(img_file.read()).decode()
|
| 115 |
img_html = f"<img src='data:image/png;base64,{encoded}' style='width:100%;'/>"
|
|
@@ -126,7 +123,7 @@ def generate_chart(file, title):
|
|
| 126 |
</html>
|
| 127 |
"""
|
| 128 |
|
| 129 |
-
# Generate PDF
|
| 130 |
wkhtml_path = get_wkhtmltopdf_path()
|
| 131 |
if wkhtml_path is None:
|
| 132 |
logging.error("wkhtmltopdf not found, creating dummy PDF file.")
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import logging
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pandas as pd
|
| 5 |
import base64
|
| 6 |
import pdfkit
|
| 7 |
import shutil
|
| 8 |
from graphviz import Digraph
|
| 9 |
|
| 10 |
+
# Debug prints for environment variables
|
| 11 |
+
print("PATH =", os.environ.get("PATH"))
|
| 12 |
+
print("GRAPHVIZ_DOT =", os.environ.get("GRAPHVIZ_DOT"))
|
| 13 |
+
|
| 14 |
# Set up logging for debugging
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
|
|
|
|
| 24 |
return None
|
| 25 |
|
| 26 |
def process_uploaded_file(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
logging.info(f"Processing uploaded file: {file}")
|
| 28 |
if isinstance(file, str):
|
| 29 |
if os.path.abspath(file) == os.getcwd():
|
|
|
|
| 76 |
logging.error(f"Error reading CSV: {e}")
|
| 77 |
return None, f"Error reading CSV: {e}"
|
| 78 |
|
|
|
|
| 79 |
expected_columns = {"Name", "Role", "Reporting To"}
|
| 80 |
if not expected_columns.issubset(set(df.columns)):
|
| 81 |
return None, "CSV must contain Name, Role, and Reporting To columns."
|
| 82 |
|
| 83 |
+
# Create a Graphviz Digraph for the org chart.
|
| 84 |
dot = Digraph(comment=title, format='png')
|
| 85 |
+
dot.attr(rankdir='TB')
|
| 86 |
dot.attr('node', shape='box', style='rounded,filled', fillcolor='lightblue',
|
| 87 |
fontsize='10', fontname='Helvetica', margin='0.1')
|
| 88 |
|
| 89 |
+
# Add nodes.
|
| 90 |
for idx, row in df.iterrows():
|
| 91 |
+
node_id = row['Name'] # Assumes names are unique.
|
| 92 |
label = f"{row['Name']}\n({row['Role']})"
|
| 93 |
dot.node(node_id, label=label)
|
| 94 |
|
| 95 |
+
# Add edges.
|
| 96 |
for idx, row in df.iterrows():
|
| 97 |
if pd.notna(row['Reporting To']) and row['Reporting To'] != "":
|
| 98 |
manager = row['Reporting To']
|
| 99 |
subordinate = row['Name']
|
|
|
|
| 100 |
if manager not in df['Name'].values:
|
| 101 |
dot.node(manager, label=manager)
|
| 102 |
dot.edge(manager, subordinate)
|
| 103 |
|
| 104 |
+
# Render diagram to PNG.
|
| 105 |
output_path = "/tmp/chart"
|
| 106 |
dot.render(output_path, cleanup=True)
|
| 107 |
chart_path = output_path + ".png"
|
| 108 |
|
| 109 |
+
# Build HTML content with embedded chart image.
|
| 110 |
with open(chart_path, "rb") as img_file:
|
| 111 |
encoded = base64.b64encode(img_file.read()).decode()
|
| 112 |
img_html = f"<img src='data:image/png;base64,{encoded}' style='width:100%;'/>"
|
|
|
|
| 123 |
</html>
|
| 124 |
"""
|
| 125 |
|
| 126 |
+
# Generate PDF.
|
| 127 |
wkhtml_path = get_wkhtmltopdf_path()
|
| 128 |
if wkhtml_path is None:
|
| 129 |
logging.error("wkhtmltopdf not found, creating dummy PDF file.")
|