-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·53 lines (45 loc) · 1.99 KB
/
Copy pathcli.py
File metadata and controls
executable file
·53 lines (45 loc) · 1.99 KB
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
#!/usr/bin/env python3
import typer
import httpx
import json
import asyncio
from typing import Optional
app = typer.Typer(add_completion=False)
API_URL = "http://127.0.0.1:8000"
async def stream_events(project_id: str):
async with httpx.AsyncClient(timeout=None) as client:
async with client.stream("GET", f"{API_URL}/projects/{project_id}/events") as response:
async for line in response.aiter_lines():
if line.startswith("data: "):
data = json.loads(line[6:])
status = data.get("status")
current_step = data.get("current_step")
if current_step:
step_info = data["steps"][current_step]
typer.echo(f"[{status.upper()}] Step: {current_step} - {step_info['message']} ({int(step_info['progress']*100)}%)")
else:
typer.echo(f"Project Status: {status}")
if status in ["completed", "failed"]:
break
@app.command()
def start(topic: str, style: str, quality: str = "medium"):
"""Start a new music video project."""
async def run():
async with httpx.AsyncClient() as client:
try:
response = await client.post(
f"{API_URL}/projects",
json={"topic": topic, "style": style, "quality": quality.lower()}
)
response.raise_for_status()
project_id = response.json()["project_id"]
typer.echo(f"Project started! ID: {project_id}")
typer.echo("Waiting for updates...")
await stream_events(project_id)
except httpx.ConnectError:
typer.echo("Error: Could not connect to the API. Make sure the server is running.")
except Exception as e:
typer.echo(f"An error occurred: {e}")
asyncio.run(run())
if __name__ == "__main__":
app()