davidberenstein1957 HF staff commited on
Commit
c931653
1 Parent(s): 48548de

feat: add payload code

Browse files
Files changed (1) hide show
  1. app.py +35 -39
app.py CHANGED
@@ -16,7 +16,9 @@ completed_record_events = Queue()
16
  def build_dataset(client: rg.Argilla) -> rg.Dataset:
17
  settings = rg.Settings.from_hub("stanfordnlp/imdb")
18
 
19
- settings.questions.add(rg.LabelQuestion(name="sentiment", labels=["negative", "positive"]))
 
 
20
 
21
  dataset_name = "stanfordnlp_imdb"
22
  dataset = client.datasets(dataset_name) or rg.Dataset.from_hub(
@@ -24,7 +26,7 @@ def build_dataset(client: rg.Argilla) -> rg.Dataset:
24
  name=dataset_name,
25
  settings=settings,
26
  client=client,
27
- split="train[:1000]"
28
  )
29
 
30
  return dataset
@@ -33,7 +35,9 @@ def build_dataset(client: rg.Argilla) -> rg.Dataset:
33
  with gr.Blocks() as demo:
34
  argilla_server = client.http_client.base_url
35
  gr.Markdown("## Argilla Events")
36
- gr.Markdown(f"This demo shows the incoming events from the [Argilla Server]({argilla_server}).")
 
 
37
  gr.Markdown("### Record Events")
38
  gr.Markdown("#### Records are processed in background and suggestions are added.")
39
 
@@ -72,7 +76,6 @@ def add_record_suggestions_on_response_created():
72
  continue
73
 
74
  # Prepare predict data
75
-
76
  field = dataset.settings.fields["text"]
77
  question = dataset.settings.questions["sentiment"]
78
 
@@ -93,7 +96,9 @@ def add_record_suggestions_on_response_created():
93
  if not some_pending_records:
94
  continue
95
 
96
- some_pending_records = parse_pending_records(some_pending_records, field, question, examples)
 
 
97
  dataset.records.log(some_pending_records)
98
 
99
  except Exception:
@@ -105,42 +110,33 @@ def parse_pending_records(
105
  records: List[rg.Record],
106
  field: rg.Field,
107
  question,
108
- example_records: List[rg.Record]
109
  ) -> List[rg.Record]:
110
- gradio_client = Client("davidberenstein1957/distilabel-argilla-labeller")
111
-
112
- payload = {
113
- "records": [record.to_dict() for record in records],
114
- "fields": [field.serialize()],
115
- "question": question.serialize(),
116
- "example_records": [record.to_dict() for record in example_records],
117
- }
118
-
119
- print("Sending payload")
120
- # response = gradio_client.predict(
121
- # records=json.dumps(payload["records"]),
122
- # example_records=json.dumps(payload["example_records"]),
123
- # fields=json.dumps(payload["fields"]),
124
- # question=json.dumps(payload["question"]),
125
- # api_name="/predict"
126
- # )
127
- # print(response)
128
- # response = json.loads(response)
129
- # print("Response ", response)
130
-
131
- response = {
132
- "results": [{"value": "positive", "score": None, "agent": "mock"} for _ in records]
133
- }
134
-
135
- for record, suggestion in zip(records, response["results"]):
136
- record.suggestions.add(
137
- rg.Suggestion(
138
- question_name=question.name,
139
- value=suggestion["value"],
140
- score=suggestion["score"],
141
- agent=suggestion["agent"],
142
  )
143
- )
 
144
 
145
  return records
146
 
 
16
  def build_dataset(client: rg.Argilla) -> rg.Dataset:
17
  settings = rg.Settings.from_hub("stanfordnlp/imdb")
18
 
19
+ settings.questions.add(
20
+ rg.LabelQuestion(name="sentiment", labels=["negative", "positive"])
21
+ )
22
 
23
  dataset_name = "stanfordnlp_imdb"
24
  dataset = client.datasets(dataset_name) or rg.Dataset.from_hub(
 
26
  name=dataset_name,
27
  settings=settings,
28
  client=client,
29
+ split="train[:1000]",
30
  )
31
 
32
  return dataset
 
35
  with gr.Blocks() as demo:
36
  argilla_server = client.http_client.base_url
37
  gr.Markdown("## Argilla Events")
38
+ gr.Markdown(
39
+ f"This demo shows the incoming events from the [Argilla Server]({argilla_server})."
40
+ )
41
  gr.Markdown("### Record Events")
42
  gr.Markdown("#### Records are processed in background and suggestions are added.")
43
 
 
76
  continue
77
 
78
  # Prepare predict data
 
79
  field = dataset.settings.fields["text"]
80
  question = dataset.settings.questions["sentiment"]
81
 
 
96
  if not some_pending_records:
97
  continue
98
 
99
+ some_pending_records = parse_pending_records(
100
+ some_pending_records, field, question, examples
101
+ )
102
  dataset.records.log(some_pending_records)
103
 
104
  except Exception:
 
110
  records: List[rg.Record],
111
  field: rg.Field,
112
  question,
113
+ example_records: List[rg.Record],
114
  ) -> List[rg.Record]:
115
+ try:
116
+ gradio_client = Client("davidberenstein1957/distilabel-argilla-labeller")
117
+
118
+ payload = {
119
+ "records": [record.to_dict() for record in records],
120
+ "fields": [field.serialize()],
121
+ "question": question.serialize(),
122
+ "example_records": [record.to_dict() for record in example_records],
123
+ "api_name": "/predict",
124
+ }
125
+
126
+ response = gradio_client.predict(**payload)
127
+ response = json.loads(response) if isinstance(response, str) else response
128
+
129
+ for record, suggestion in zip(records, response["results"]):
130
+ record.suggestions.add(
131
+ rg.Suggestion(
132
+ question_name=question.name,
133
+ value=suggestion["value"],
134
+ score=suggestion["score"],
135
+ agent=suggestion["agent"],
136
+ )
 
 
 
 
 
 
 
 
 
 
137
  )
138
+ except Exception:
139
+ print(traceback.format_exc())
140
 
141
  return records
142