Spaces:
Sleeping
Sleeping
Update app.py
Browse filesMade the Huggingface version filename-agnostic.
app.py
CHANGED
|
@@ -1,74 +1,133 @@
|
|
| 1 |
import sys
|
|
|
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
ROOT = Path(__file__).resolve().parent
|
| 5 |
SRC = ROOT / "src"
|
| 6 |
-
|
| 7 |
sys.path.insert(0, str(SRC))
|
| 8 |
|
| 9 |
-
import gradio as gr
|
| 10 |
-
from pathlib import Path
|
| 11 |
-
import shutil
|
| 12 |
-
|
| 13 |
from ig_story_audit.main import main as run_audit
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
f.unlink()
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
config = load_config()
|
|
|
|
|
|
|
| 32 |
config["general"]["language"] = language
|
| 33 |
|
| 34 |
-
# Run
|
| 35 |
run_audit()
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
audit_dir = Path("
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
return latest.read_text(encoding="utf-8")
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
-
gr.Markdown("## Instagram Story Narrative Audit")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
file_types=["image"],
|
| 49 |
file_count="multiple",
|
| 50 |
-
label="Upload
|
| 51 |
)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
label="
|
| 55 |
placeholder="e.g. Reinforce the feeling of tiredness through repetition",
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
choices=["id", "en"],
|
| 60 |
value="id",
|
| 61 |
label="Output language",
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
inputs=[
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
)
|
| 73 |
|
| 74 |
demo.launch()
|
|
|
|
| 1 |
import sys
|
| 2 |
+
import shutil
|
| 3 |
from pathlib import Path
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
|
| 8 |
+
# --- Ensure src/ is importable (HF-only fix for src layout) ---
|
| 9 |
ROOT = Path(__file__).resolve().parent
|
| 10 |
SRC = ROOT / "src"
|
|
|
|
| 11 |
sys.path.insert(0, str(SRC))
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
from ig_story_audit.main import main as run_audit
|
| 14 |
+
from ig_story_audit.utils.config import load_config
|
| 15 |
|
| 16 |
|
| 17 |
+
RAW_DIR = Path("data/raw_stories")
|
| 18 |
+
RAW_DIR.mkdir(parents=True, exist_ok=True)
|
| 19 |
|
| 20 |
|
| 21 |
+
def normalise_uploaded_files(files, run_date: str):
|
| 22 |
+
"""
|
| 23 |
+
Hugging Face–only preprocessing:
|
| 24 |
+
- Clears any existing files for the run date
|
| 25 |
+
- Renames uploaded files into YYYY-MM-DD_story_N.ext
|
| 26 |
+
"""
|
| 27 |
+
# Remove existing files for this date
|
| 28 |
+
for f in RAW_DIR.glob(f"{run_date}_story_*"):
|
| 29 |
f.unlink()
|
| 30 |
|
| 31 |
+
for idx, file in enumerate(files, start=1):
|
| 32 |
+
src = Path(file.name)
|
| 33 |
+
ext = src.suffix.lower() or ".jpg"
|
| 34 |
+
target = RAW_DIR / f"{run_date}_story_{idx}{ext}"
|
| 35 |
+
shutil.copy(src, target)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def run_hf_audit(files, goal_text, language, is_today, selected_date):
|
| 39 |
+
if not files:
|
| 40 |
+
return "No files uploaded."
|
| 41 |
+
|
| 42 |
+
# Determine run date
|
| 43 |
+
if is_today:
|
| 44 |
+
run_date = datetime.now().strftime("%Y-%m-%d")
|
| 45 |
+
else:
|
| 46 |
+
run_date = selected_date.strip()
|
| 47 |
|
| 48 |
+
# Prepare files
|
| 49 |
+
normalise_uploaded_files(files, run_date)
|
| 50 |
+
|
| 51 |
+
# Override language in config (runtime-only)
|
| 52 |
config = load_config()
|
| 53 |
+
if "general" not in config:
|
| 54 |
+
config["general"] = {}
|
| 55 |
config["general"]["language"] = language
|
| 56 |
|
| 57 |
+
# Run core pipeline (unchanged)
|
| 58 |
run_audit()
|
| 59 |
|
| 60 |
+
# Fetch latest audit output
|
| 61 |
+
audit_dir = Path(config["paths"]["audit_logs_dir"])
|
| 62 |
+
if not audit_dir.exists():
|
| 63 |
+
return "Audit completed, but no audit logs were generated."
|
| 64 |
+
|
| 65 |
+
latest = max(
|
| 66 |
+
audit_dir.glob("*_narrative_audit.txt"),
|
| 67 |
+
key=lambda p: p.stat().st_mtime,
|
| 68 |
+
default=None,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if not latest:
|
| 72 |
+
return "Audit completed, but no narrative audit file found."
|
| 73 |
|
| 74 |
return latest.read_text(encoding="utf-8")
|
| 75 |
|
| 76 |
|
| 77 |
+
# ----------------- Gradio UI -----------------
|
| 78 |
+
|
| 79 |
with gr.Blocks() as demo:
|
| 80 |
+
gr.Markdown("## Instagram Story Narrative Audit (Manual Upload)")
|
| 81 |
+
gr.Markdown(
|
| 82 |
+
"Upload Instagram Story images manually. "
|
| 83 |
+
"Files will be analysed as a story sequence for the selected date."
|
| 84 |
+
)
|
| 85 |
|
| 86 |
+
files_input = gr.File(
|
| 87 |
file_types=["image"],
|
| 88 |
file_count="multiple",
|
| 89 |
+
label="Upload Story Images",
|
| 90 |
)
|
| 91 |
|
| 92 |
+
goal_input = gr.Textbox(
|
| 93 |
+
label="Narrative goal",
|
| 94 |
placeholder="e.g. Reinforce the feeling of tiredness through repetition",
|
| 95 |
)
|
| 96 |
|
| 97 |
+
language_input = gr.Dropdown(
|
| 98 |
choices=["id", "en"],
|
| 99 |
value="id",
|
| 100 |
label="Output language",
|
| 101 |
)
|
| 102 |
|
| 103 |
+
is_today_input = gr.Checkbox(
|
| 104 |
+
value=True,
|
| 105 |
+
label="Use today's date",
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
date_input = gr.Textbox(
|
| 109 |
+
label="Date (YYYY-MM-DD)",
|
| 110 |
+
placeholder="2025-12-14",
|
| 111 |
+
visible=True,
|
| 112 |
+
)
|
| 113 |
|
| 114 |
+
run_button = gr.Button("Run Audit")
|
| 115 |
+
|
| 116 |
+
output_box = gr.Textbox(
|
| 117 |
+
label="Audit Result",
|
| 118 |
+
lines=25,
|
| 119 |
+
)
|
| 120 |
|
| 121 |
+
run_button.click(
|
| 122 |
+
fn=run_hf_audit,
|
| 123 |
+
inputs=[
|
| 124 |
+
files_input,
|
| 125 |
+
goal_input,
|
| 126 |
+
language_input,
|
| 127 |
+
is_today_input,
|
| 128 |
+
date_input,
|
| 129 |
+
],
|
| 130 |
+
outputs=output_box,
|
| 131 |
)
|
| 132 |
|
| 133 |
demo.launch()
|