Spaces:
Sleeping
Sleeping
File size: 2,700 Bytes
00f98d2 b019af3 00f98d2 e658037 00f98d2 e658037 00f98d2 e658037 00f98d2 e658037 5296ac8 00f98d2 e658037 fc6f959 00f98d2 e658037 00f98d2 e658037 00f98d2 e658037 00f98d2 e658037 7271b67 b019af3 e658037 00f98d2 e658037 b019af3 00f98d2 b019af3 e658037 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 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) |