Refactor feedback collection flow to simplify UI integration and improve error handling
Browse files
app.py
CHANGED
|
@@ -1,11 +1,8 @@
|
|
| 1 |
import asyncio
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
| 4 |
from prefect import flow, get_run_logger, pause_flow_run, settings
|
| 5 |
from prefect.context import get_run_context
|
| 6 |
from prefect.input import RunInput
|
| 7 |
-
from prefect.deployments import Deployment
|
| 8 |
-
from prefect.server.schemas.schedules import IntervalSchedule
|
| 9 |
|
| 10 |
|
| 11 |
class UserFeedback(RunInput):
|
|
@@ -18,47 +15,19 @@ class UserFeedback(RunInput):
|
|
| 18 |
|
| 19 |
@flow(name="interactive-feedback-flow")
|
| 20 |
async def collect_feedback(username: str):
|
| 21 |
-
"""A flow that pauses to collect user feedback via the Prefect
|
| 22 |
logger = get_run_logger()
|
| 23 |
|
| 24 |
-
# Ensure we're using Prefect Cloud
|
| 25 |
-
logger.info("Running with Prefect Cloud configuration")
|
| 26 |
-
|
| 27 |
logger.info(f"Started feedback collection for user: {username}")
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
message = (
|
| 31 |
-
f"Collecting feedback from user: {username}" # Get flow run context for UI URL
|
| 32 |
-
)
|
| 33 |
flow_run = get_run_context().flow_run
|
| 34 |
flow_run_url = ""
|
| 35 |
|
| 36 |
-
if flow_run:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
):
|
| 41 |
-
# Extract account_id and workspace_name from the API URL
|
| 42 |
-
# Format: https://api.prefect.cloud/api/accounts/{account_id}/workspaces/{workspace_name}/
|
| 43 |
-
api_url = os.environ["PREFECT_API_URL"]
|
| 44 |
-
parts = api_url.split("/")
|
| 45 |
-
|
| 46 |
-
if (
|
| 47 |
-
len(parts) >= 7
|
| 48 |
-
): # Ensure we have enough parts to extract account_id and workspace_name
|
| 49 |
-
account_id = parts[5]
|
| 50 |
-
workspace_name = parts[7]
|
| 51 |
-
cloud_ui_url = f"https://app.prefect.cloud/account/{account_id}/workspace/{workspace_name}"
|
| 52 |
-
flow_run_url = f"{cloud_ui_url}/flow-runs/flow-run/{flow_run.id}"
|
| 53 |
-
else:
|
| 54 |
-
# Fallback to standard URL formation if we can't parse the API URL
|
| 55 |
-
if settings.PREFECT_UI_URL:
|
| 56 |
-
flow_run_url = f"{settings.PREFECT_UI_URL.value()}/flow-runs/flow-run/{flow_run.id}"
|
| 57 |
-
else:
|
| 58 |
-
# Fallback for local Prefect or other setups
|
| 59 |
-
if settings.PREFECT_UI_URL:
|
| 60 |
-
flow_run_url = f"{settings.PREFECT_UI_URL.value()}/flow-runs/flow-run/{flow_run.id}"
|
| 61 |
-
|
| 62 |
logger.info(f"Flow run UI available at: {flow_run_url}")
|
| 63 |
|
| 64 |
# Pause the flow and wait for input
|
|
@@ -70,7 +39,7 @@ async def collect_feedback(username: str):
|
|
| 70 |
comment="",
|
| 71 |
improve_feature=False,
|
| 72 |
),
|
| 73 |
-
timeout=
|
| 74 |
)
|
| 75 |
|
| 76 |
# Process the input received after the flow is resumed
|
|
@@ -79,17 +48,13 @@ async def collect_feedback(username: str):
|
|
| 79 |
logger.info(f"Comment: {user_feedback.comment}")
|
| 80 |
logger.info(f"Wants feature improvement: {user_feedback.improve_feature}")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
result = {
|
| 84 |
"username": username,
|
| 85 |
"rating": user_feedback.rating,
|
| 86 |
"comment": user_feedback.comment,
|
| 87 |
"improve_feature": user_feedback.improve_feature,
|
| 88 |
-
"flow_run_url": flow_run_url,
|
| 89 |
}
|
| 90 |
|
| 91 |
-
return result
|
| 92 |
-
|
| 93 |
|
| 94 |
def run_feedback_flow(username):
|
| 95 |
"""Run the Prefect flow and handle the UI interaction."""
|
|
@@ -99,75 +64,29 @@ def run_feedback_flow(username):
|
|
| 99 |
|
| 100 |
print(f"Starting feedback collection for user: {username}")
|
| 101 |
|
| 102 |
-
#
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
# Create or update the deployment
|
| 106 |
-
deployment = Deployment.build_from_flow(
|
| 107 |
-
flow=collect_feedback,
|
| 108 |
-
name="interactive-feedback-deployment",
|
| 109 |
-
version=None, # Auto-version
|
| 110 |
-
work_queue_name="default",
|
| 111 |
-
tags=["interactive", "feedback"],
|
| 112 |
-
parameters={"username": username},
|
| 113 |
-
enforce_parameter_schema=False,
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# Apply the deployment
|
| 117 |
-
deployment_id = deployment.apply()
|
| 118 |
-
print(f"Created deployment with ID: {deployment_id}")
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
name="interactive-feedback-flow/interactive-feedback-deployment",
|
| 125 |
-
parameters={"username": username},
|
| 126 |
-
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# Get the URL
|
| 131 |
-
flow_run_url = ""
|
| 132 |
-
if settings.PREFECT_UI_URL:
|
| 133 |
-
flow_run_url = (
|
| 134 |
-
f"{settings.PREFECT_UI_URL.value()}/flow-runs/flow-run/{flow_run.id}"
|
| 135 |
-
)
|
| 136 |
-
elif os.environ.get(
|
| 137 |
-
"PREFECT_API_URL"
|
| 138 |
-
) and "api.prefect.cloud" in os.environ.get("PREFECT_API_URL", ""):
|
| 139 |
-
# Extract account_id and workspace_name from the API URL
|
| 140 |
-
api_url = os.environ["PREFECT_API_URL"]
|
| 141 |
-
parts = api_url.split("/")
|
| 142 |
-
|
| 143 |
-
if len(parts) >= 7:
|
| 144 |
-
account_id = parts[5]
|
| 145 |
-
workspace_name = parts[7]
|
| 146 |
-
flow_run_url = f"https://app.prefect.cloud/account/{account_id}/workspace/{workspace_name}/flow-runs/flow-run/{flow_run.id}"
|
| 147 |
-
|
| 148 |
-
print(f"Flow run UI available at: {flow_run_url}")
|
| 149 |
|
| 150 |
return f"""
|
| 151 |
Process started for {username}!
|
| 152 |
|
| 153 |
IMPORTANT INSTRUCTIONS:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
{flow_run_url}
|
| 157 |
-
|
| 158 |
-
1. Click the "Resume" button in the Prefect UI
|
| 159 |
-
2. Fill out the feedback form and submit
|
| 160 |
-
3. Your input will be processed by the workflow
|
| 161 |
-
|
| 162 |
-
Note: This window will not automatically update with the results.
|
| 163 |
-
The complete results will be available in the Prefect UI and terminal.
|
| 164 |
"""
|
| 165 |
-
|
| 166 |
except Exception as e:
|
| 167 |
-
import traceback
|
| 168 |
-
|
| 169 |
-
print(f"Error: {str(e)}")
|
| 170 |
-
print(traceback.format_exc())
|
| 171 |
return f"An error occurred: {str(e)}"
|
| 172 |
|
| 173 |
|
|
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from prefect import flow, get_run_logger, pause_flow_run, settings
|
| 4 |
from prefect.context import get_run_context
|
| 5 |
from prefect.input import RunInput
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
class UserFeedback(RunInput):
|
|
|
|
| 15 |
|
| 16 |
@flow(name="interactive-feedback-flow")
|
| 17 |
async def collect_feedback(username: str):
|
| 18 |
+
"""A flow that pauses to collect user feedback via the Prefect UI."""
|
| 19 |
logger = get_run_logger()
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
logger.info(f"Started feedback collection for user: {username}")
|
| 22 |
|
| 23 |
+
# Get flow run context for UI URL
|
|
|
|
|
|
|
|
|
|
| 24 |
flow_run = get_run_context().flow_run
|
| 25 |
flow_run_url = ""
|
| 26 |
|
| 27 |
+
if flow_run and settings.PREFECT_UI_URL:
|
| 28 |
+
flow_run_url = (
|
| 29 |
+
f"{settings.PREFECT_UI_URL.value()}/flow-runs/flow-run/{flow_run.id}"
|
| 30 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
logger.info(f"Flow run UI available at: {flow_run_url}")
|
| 32 |
|
| 33 |
# Pause the flow and wait for input
|
|
|
|
| 39 |
comment="",
|
| 40 |
improve_feature=False,
|
| 41 |
),
|
| 42 |
+
timeout=300, # 5 minutes timeout
|
| 43 |
)
|
| 44 |
|
| 45 |
# Process the input received after the flow is resumed
|
|
|
|
| 48 |
logger.info(f"Comment: {user_feedback.comment}")
|
| 49 |
logger.info(f"Wants feature improvement: {user_feedback.improve_feature}")
|
| 50 |
|
| 51 |
+
return {
|
|
|
|
| 52 |
"username": username,
|
| 53 |
"rating": user_feedback.rating,
|
| 54 |
"comment": user_feedback.comment,
|
| 55 |
"improve_feature": user_feedback.improve_feature,
|
|
|
|
| 56 |
}
|
| 57 |
|
|
|
|
|
|
|
| 58 |
|
| 59 |
def run_feedback_flow(username):
|
| 60 |
"""Run the Prefect flow and handle the UI interaction."""
|
|
|
|
| 64 |
|
| 65 |
print(f"Starting feedback collection for user: {username}")
|
| 66 |
|
| 67 |
+
# Start the flow in a separate thread to avoid blocking the Gradio interface
|
| 68 |
+
import threading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
def run_flow():
|
| 71 |
+
# Run the flow and get the result
|
| 72 |
+
result = asyncio.run(collect_feedback(username))
|
| 73 |
+
print(f"Flow completed with results: {result}")
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# Start the thread
|
| 76 |
+
threading.Thread(target=run_flow).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
return f"""
|
| 79 |
Process started for {username}!
|
| 80 |
|
| 81 |
IMPORTANT INSTRUCTIONS:
|
| 82 |
+
1. Look for a line in the terminal that says "Flow run UI available at: [URL]"
|
| 83 |
+
2. Copy that URL and open it in your browser
|
| 84 |
+
3. Click "Resume" in the Prefect UI
|
| 85 |
+
4. Fill out the feedback form and submit
|
| 86 |
|
| 87 |
+
The results will be available in the terminal after completion.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"""
|
|
|
|
| 89 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
return f"An error occurred: {str(e)}"
|
| 91 |
|
| 92 |
|