Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,21 +50,21 @@ def handle_csv_upload(file):
|
|
| 50 |
df = pd.read_csv(file.name)
|
| 51 |
required_columns = {'content_id', 'kkn_tags', 'image-link#1', 'image-link#2', 'image-link#3'}
|
| 52 |
if not required_columns.issubset(df.columns):
|
| 53 |
-
return "Missing required columns in CSV.", None
|
| 54 |
-
return "CSV uploaded successfully.", df
|
| 55 |
except Exception as e:
|
| 56 |
-
return f"CSV upload failed: {type(e).__name__} - {str(e)}", None
|
| 57 |
|
| 58 |
# Step 2: Validate content_id
|
| 59 |
def validate_content_id(content_id, df):
|
| 60 |
try:
|
| 61 |
match = df[df['content_id'].astype(str) == content_id]
|
| 62 |
if match.empty:
|
| 63 |
-
return "Content ID not found in CSV.", None
|
| 64 |
row = match.iloc[0]
|
| 65 |
-
return "Content ID found. Now paste HTML code.", row
|
| 66 |
except Exception as e:
|
| 67 |
-
return f"Error: {type(e).__name__} - {str(e)}", None
|
| 68 |
|
| 69 |
# Step 3: Inject images
|
| 70 |
def inject_and_display(html_code, row):
|
|
@@ -75,32 +75,32 @@ def inject_and_display(html_code, row):
|
|
| 75 |
row['image-link#2'],
|
| 76 |
row['image-link#3']
|
| 77 |
)
|
| 78 |
-
return "Images injected successfully.", injected_html, row['kkn_tags']
|
| 79 |
except Exception as e:
|
| 80 |
-
return f"Injection failed: {type(e).__name__} - {str(e)}", "", ""
|
| 81 |
|
| 82 |
# Build UI
|
| 83 |
-
with gr.Blocks() as demo:
|
| 84 |
-
gr.Markdown("##
|
| 85 |
|
| 86 |
csv_state = gr.State()
|
| 87 |
row_state = gr.State()
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
-
csv_input = gr.File(label="Step 1: Upload CSV", file_types=[".csv"])
|
| 91 |
-
csv_status = gr.Textbox(label="Status", interactive=False)
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
-
content_id_input = gr.Textbox(label="Step 2: Enter content_id")
|
| 95 |
-
content_status = gr.Textbox(label="Status", interactive=False)
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
-
html_input = gr.Textbox(label="Step 3: Paste HTML here", lines=10)
|
| 99 |
-
html_status = gr.Textbox(label="Status", interactive=False)
|
| 100 |
|
| 101 |
-
submit_btn = gr.Button("Step 4: Inject Images")
|
| 102 |
-
output_html = gr.
|
| 103 |
-
output_tags = gr.Textbox(label="Extracted kkn_tags")
|
| 104 |
|
| 105 |
# Step 1 action
|
| 106 |
csv_input.change(
|
|
|
|
| 50 |
df = pd.read_csv(file.name)
|
| 51 |
required_columns = {'content_id', 'kkn_tags', 'image-link#1', 'image-link#2', 'image-link#3'}
|
| 52 |
if not required_columns.issubset(df.columns):
|
| 53 |
+
return "β Missing required columns in CSV.", None
|
| 54 |
+
return "β
CSV uploaded successfully.", df
|
| 55 |
except Exception as e:
|
| 56 |
+
return f"β CSV upload failed: {type(e).__name__} - {str(e)}", None
|
| 57 |
|
| 58 |
# Step 2: Validate content_id
|
| 59 |
def validate_content_id(content_id, df):
|
| 60 |
try:
|
| 61 |
match = df[df['content_id'].astype(str) == content_id]
|
| 62 |
if match.empty:
|
| 63 |
+
return "β Content ID not found in CSV.", None
|
| 64 |
row = match.iloc[0]
|
| 65 |
+
return "β
Content ID found. Now paste HTML code.", row
|
| 66 |
except Exception as e:
|
| 67 |
+
return f"β Error: {type(e).__name__} - {str(e)}", None
|
| 68 |
|
| 69 |
# Step 3: Inject images
|
| 70 |
def inject_and_display(html_code, row):
|
|
|
|
| 75 |
row['image-link#2'],
|
| 76 |
row['image-link#3']
|
| 77 |
)
|
| 78 |
+
return "β
Images injected successfully.", injected_html, row['kkn_tags']
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"β Injection failed: {type(e).__name__} - {str(e)}", "", ""
|
| 81 |
|
| 82 |
# Build UI
|
| 83 |
+
with gr.Blocks(css="#inject-btn {background-color: #22c55e; color: white; font-weight: bold; border-radius: 8px;}") as demo:
|
| 84 |
+
gr.Markdown("## πΌοΈ HTML Image Injector - Easy Steps")
|
| 85 |
|
| 86 |
csv_state = gr.State()
|
| 87 |
row_state = gr.State()
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
csv_input = gr.File(label="π€ Step 1: Upload CSV", file_types=[".csv"])
|
| 91 |
+
csv_status = gr.Textbox(label="π Status", interactive=False)
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
+
content_id_input = gr.Textbox(label="π Step 2: Enter content_id")
|
| 95 |
+
content_status = gr.Textbox(label="π Status", interactive=False)
|
| 96 |
|
| 97 |
with gr.Row():
|
| 98 |
+
html_input = gr.Textbox(label="π§Ύ Step 3: Paste HTML here", lines=10)
|
| 99 |
+
html_status = gr.Textbox(label="π Status", interactive=False)
|
| 100 |
|
| 101 |
+
submit_btn = gr.Button("β¨ Step 4: Inject Images", elem_id="inject-btn")
|
| 102 |
+
output_html = gr.Textbox(label="π§Ύ Injected HTML (Raw Output)", lines=10, interactive=False)
|
| 103 |
+
output_tags = gr.Textbox(label="π·οΈ Extracted kkn_tags", interactive=False)
|
| 104 |
|
| 105 |
# Step 1 action
|
| 106 |
csv_input.change(
|