Spaces:
Running
Running
| import os | |
| import json | |
| import uuid | |
| import datetime | |
| from typing import Optional, Dict, Any | |
| import gradio as gr | |
| # -------- STORAGE -------- | |
| DATA_DIR = "storage/contracts" | |
| os.makedirs(DATA_DIR, exist_ok=True) | |
| # -------- FILE UTILS ----- | |
| def _contract_path(uid:str): return os.path.join(DATA_DIR,f"{uid}.json") | |
| def save(c:dict): | |
| with open(_contract_path(c["uid"]),"w",encoding="utf-8") as f: json.dump(c,f,indent=2,ensure_ascii=False) | |
| def load(uid:str)->dict: | |
| p=_contract_path(uid) | |
| if not os.path.exists(p): raise ValueError(f"Brak kontraktu: {uid}") | |
| return json.load(open(p,"r",encoding="utf-8")) | |
| # -------- BASE CONTRACT GEN -------- | |
| def create_contract(link:str,category:str="general",uid:Optional[str]=None)->dict: | |
| uid = uid or f"{category}_{uuid.uuid4().hex[:8]}" | |
| from modules.transcriber import transcribe_from_url | |
| from modules.generate_plan import steps_from_transcript | |
| txt = transcribe_from_url(link) | |
| if txt.strip(): | |
| steps = steps_from_transcript(txt, category) | |
| else: | |
| steps = ["Obejrzyj wideo i zapisz 1 najważniejszy wniosek."]*7 | |
| def next_step(uid:str)->dict: | |
| c=load(uid) | |
| i=c["current_index"] | |
| if i>=len(c["steps"]): return {"uid":uid,"done":True,"message":"Wszystko wykonane."} | |
| return {"uid":uid,"step":c["steps"][i],"remaining":len(c["steps"])-i} | |
| def mark_done(uid:str)->dict: | |
| c=load(uid) | |
| i=c["current_index"] | |
| if i<len(c["steps"]): | |
| c["history"].append({"step":c["steps"][i],"ts":datetime.datetime.utcnow().isoformat()+"Z"}) | |
| c["current_index"]=i+1 | |
| save(c) | |
| return next_step(uid) | |
| def report(uid:str)->dict: | |
| c=load(uid); total=len(c["steps"]); done=c["current_index"] | |
| return { | |
| "uid":uid, | |
| "done":done, | |
| "total":total, | |
| "progress_pct":round(done/total*100,1) | |
| } | |
| # -------- UI + MCP -------- | |
| with gr.Blocks() as ui: | |
| gr.Markdown("# Behavior Changer – MCP") | |
| with gr.Tab("Create"): | |
| l=gr.Textbox(label="Reel URL") | |
| c=gr.Textbox(label="Category",value="career") | |
| u=gr.Textbox(label="UID optional") | |
| out=gr.JSON(label="Created") | |
| gr.Button("Create").click(create_contract,[l,c,u],[out]) | |
| with gr.Tab("Next"): | |
| uid=gr.Textbox(label="UID"); out=gr.JSON(); gr.Button("Next step").click(next_step,[uid],[out]) | |
| with gr.Tab("Done"): | |
| uid=gr.Textbox(label="UID"); out=gr.JSON(); gr.Button("Mark done").click(mark_done,[uid],[out]) | |
| with gr.Tab("Report"): | |
| uid=gr.Textbox(label="UID"); out=gr.JSON(); gr.Button("Report").click(report,[uid],[out]) | |
| gr.api(create_contract) | |
| gr.api(next_step) | |
| gr.api(mark_done) | |
| gr.api(report) | |
| ui.launch(mcp_server=True) |