MR17u commited on
Commit
2950aec
1 Parent(s): ef34bc5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+
4
+ from datetime import datetime
5
+ from huggingface_hub import CommitScheduler
6
+ from pathlib import Path
7
+ from transformers import pipeline
8
+ from uuid import uuid4
9
+
10
+ #based on https://huggingface.co/spaces/Wauplin/space_to_dataset_saver/blob/main/app_json.py
11
+ #data is saved at https://huggingface.co/datasets/MR17u/tweeteval-irony-mcc/tree/main
12
+
13
+ JSON_DATASET_DIR = Path("json_dataset")
14
+ JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
15
+
16
+ JSON_DATASET_PATH = JSON_DATASET_DIR / f"data-{uuid4()}.json"
17
+
18
+ CLS_MODEL_NAME = "PierreEpron/tweeteval-irony-mcc"
19
+
20
+ scheduler = CommitScheduler(
21
+ repo_id="tweeteval-irony-mcc",
22
+ repo_type="dataset",
23
+ folder_path=JSON_DATASET_DIR,
24
+ path_in_repo="data",
25
+ )
26
+
27
+ classifier = pipeline(model = CLS_MODEL_NAME, tokenizer = 'cardiffnlp/twitter-roberta-large-2022-154m')
28
+
29
+ def save_json(entry: str, result) -> None:
30
+ with scheduler.lock:
31
+ with JSON_DATASET_PATH.open("a") as f:
32
+ result = json.loads(result.replace("'",'"'))[0]
33
+ json.dump({"entry": entry, "label": result['label'], "score": result['score'], "datetime": datetime.now().isoformat()}, f)
34
+ f.write("\n")
35
+
36
+ def classif(text: str):
37
+ return classifier(text)
38
+
39
+ with gr.Blocks() as demo:
40
+ with gr.Row():
41
+ entry = gr.Textbox(label="Input")
42
+ result = gr.Textbox(label="Classification")
43
+ input_btn = gr.Button("Submit")
44
+ input_btn.click(fn=classif, inputs=entry, outputs=result).success(
45
+ fn=save_json,
46
+ inputs=[entry, result],
47
+ outputs=None
48
+ )
49
+
50
+ demo.launch()