tania-dm's picture
Refactor gradio app
237695c verified
Raw
History Blame Contribute Delete
1.48 kB
"""Super basic Gradio UI for add_collaborators_to_dataset_from_csv.py.
"""
import gradio as gr
from add_collaborators_to_dataset_from_csv import add_collaborators, format_error_message, read_collaborators
from segments import SegmentsClient
from segments.exceptions import SegmentsError
def run(api_key: str, dataset_identifier: str, csv_file: str) -> str:
if not api_key or not dataset_identifier or not csv_file:
return "Please fill in the API key, dataset identifier, and CSV file."
try:
collaborators, skipped_rows = read_collaborators(csv_file)
except ValueError as error:
return str(error)
for skipped_row in skipped_rows:
print(skipped_row)
if not collaborators:
return "No usernames found in CSV"
try:
client = SegmentsClient(api_key)
except SegmentsError as error:
return format_error_message(error)
logs, _has_failures = add_collaborators(client, dataset_identifier, collaborators)
for log in logs:
print(log)
return "\n".join(skipped_rows + logs)
demo = gr.Interface(
fn=run,
inputs=[
gr.Textbox(label="API key", type="password"),
gr.Textbox(label="Dataset identifier", placeholder="jane/flowers"),
gr.File(label="CSV file", type="filepath"),
],
outputs=gr.Textbox(label="Log", lines=15),
title="Add dataset collaborators from CSV",
flagging_mode="never",
)
if __name__ == "__main__":
demo.launch()