Spaces:
Build error
Build error
Commit
•
36821d3
1
Parent(s):
a822793
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from distilabel.llms import TransformersLLM
|
5 |
+
from distilabel.steps.tasks.argillalabeller import ArgillaLabeller
|
6 |
+
|
7 |
+
llm = TransformersLLM(model="microsoft/Phi-3-mini-4k-instruct")
|
8 |
+
task = ArgillaLabeller(llm=llm)
|
9 |
+
task.load()
|
10 |
+
|
11 |
+
@spaces.GPU
|
12 |
+
def process_records_gradio(records, example_records, field, question):
|
13 |
+
try:
|
14 |
+
# Convert string inputs to dictionaries
|
15 |
+
records = json.loads(records)
|
16 |
+
example_records = json.loads(example_records) if example_records else None
|
17 |
+
field = json.loads(field) if field else None
|
18 |
+
question = json.loads(question) if question else None
|
19 |
+
|
20 |
+
if not field and not question:
|
21 |
+
return "Error: Either field or question must be provided"
|
22 |
+
|
23 |
+
task.set_runtime_parameters(
|
24 |
+
{
|
25 |
+
"fields": [field] if field else None,
|
26 |
+
"question": question,
|
27 |
+
"example_records": example_records,
|
28 |
+
}
|
29 |
+
)
|
30 |
+
|
31 |
+
results = []
|
32 |
+
for record in records:
|
33 |
+
output = next(task.process(inputs=[{"records": record}]))
|
34 |
+
results.append(output[0]["suggestions"])
|
35 |
+
|
36 |
+
return json.dumps({"results": results}, indent=2)
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error: {str(e)}"
|
39 |
+
|
40 |
+
|
41 |
+
interface = gr.Interface(
|
42 |
+
fn=process_records_gradio,
|
43 |
+
inputs=[
|
44 |
+
gr.Code(label="Records (JSON)", language="json", lines=5),
|
45 |
+
gr.Code(label="Example Records (JSON, optional)", language="json", lines=5),
|
46 |
+
gr.Code(label="Field (JSON, optional)", language="json"),
|
47 |
+
gr.Code(label="Question (JSON, optional)", language="json"),
|
48 |
+
],
|
49 |
+
outputs=gr.Code(label="Suggestions", language="json", lines=10),
|
50 |
+
title="Record Processing Interface",
|
51 |
+
description="Enter JSON data for records, example records, field, and question. At least one of field or question must be provided.",
|
52 |
+
)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
interface.launch()
|