Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,24 @@
|
|
1 |
import gradio as gr
|
2 |
import datetime
|
3 |
import torch
|
|
|
4 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
5 |
-
from datasets import
|
6 |
-
import huggingface_hub
|
7 |
import pandas as pd
|
|
|
8 |
|
9 |
# CONFIG
|
10 |
-
MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english" #
|
11 |
-
HF_DATASET_REPO = "your-username/your-logging-dataset" #
|
12 |
-
|
|
|
|
|
13 |
|
14 |
# Load model + tokenizer
|
15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
16 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
17 |
|
18 |
-
#
|
19 |
-
huggingface_hub.login(token=HF_TOKEN)
|
20 |
-
|
21 |
-
# Store session logs
|
22 |
log_entries = []
|
23 |
|
24 |
def infer_and_log(text_input):
|
@@ -29,7 +29,6 @@ def infer_and_log(text_input):
|
|
29 |
predicted = torch.argmax(outputs.logits, dim=-1).item()
|
30 |
output_label = model.config.id2label[predicted]
|
31 |
|
32 |
-
# Create log entry
|
33 |
log_entries.append({
|
34 |
"timestamp": datetime.datetime.now().isoformat(),
|
35 |
"input": text_input,
|
@@ -42,20 +41,25 @@ def clear_fields():
|
|
42 |
return "", ""
|
43 |
|
44 |
def save_to_hf():
|
|
|
|
|
|
|
45 |
if not log_entries:
|
46 |
-
return "
|
|
|
|
|
|
|
47 |
|
48 |
-
dataset =
|
49 |
-
dataset.push_to_hub(HF_DATASET_REPO)
|
50 |
log_entries.clear()
|
51 |
-
return "
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
-
gr.Markdown("
|
55 |
|
56 |
with gr.Row():
|
57 |
-
input_box = gr.Textbox(label="Input Text", lines=
|
58 |
-
output_box = gr.Textbox(label="Predicted Label", lines=
|
59 |
|
60 |
with gr.Row():
|
61 |
submit_btn = gr.Button("Submit")
|
@@ -66,7 +70,7 @@ with gr.Blocks() as demo:
|
|
66 |
submit_btn.click(fn=infer_and_log, inputs=input_box, outputs=output_box)
|
67 |
clear_btn.click(fn=clear_fields, outputs=[input_box, output_box])
|
68 |
|
69 |
-
gr.Button("Save Logs to
|
70 |
|
71 |
if __name__ == "__main__":
|
72 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import datetime
|
3 |
import torch
|
4 |
+
import os
|
5 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
6 |
+
from datasets import Dataset, DatasetDict, disable_caching
|
|
|
7 |
import pandas as pd
|
8 |
+
from huggingface_hub import HfApi, HfFolder
|
9 |
|
10 |
# CONFIG
|
11 |
+
MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english" # Change if needed
|
12 |
+
HF_DATASET_REPO = "your-username/your-logging-dataset" # Must be created beforehand
|
13 |
+
|
14 |
+
# Token from environment in Spaces
|
15 |
+
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
16 |
|
17 |
# Load model + tokenizer
|
18 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
19 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
20 |
|
21 |
+
# Log entries
|
|
|
|
|
|
|
22 |
log_entries = []
|
23 |
|
24 |
def infer_and_log(text_input):
|
|
|
29 |
predicted = torch.argmax(outputs.logits, dim=-1).item()
|
30 |
output_label = model.config.id2label[predicted]
|
31 |
|
|
|
32 |
log_entries.append({
|
33 |
"timestamp": datetime.datetime.now().isoformat(),
|
34 |
"input": text_input,
|
|
|
41 |
return "", ""
|
42 |
|
43 |
def save_to_hf():
|
44 |
+
if not HF_TOKEN:
|
45 |
+
return "No Hugging Face token found in environment. Cannot push dataset."
|
46 |
+
|
47 |
if not log_entries:
|
48 |
+
return "No logs to push."
|
49 |
+
|
50 |
+
df = pd.DataFrame(log_entries)
|
51 |
+
dataset = Dataset.from_pandas(df)
|
52 |
|
53 |
+
dataset.push_to_hub(HF_DATASET_REPO, token=HF_TOKEN)
|
|
|
54 |
log_entries.clear()
|
55 |
+
return f"Pushed {len(df)} logs to {HF_DATASET_REPO}!"
|
56 |
|
57 |
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("## 🤖 Text Classification with Logging")
|
59 |
|
60 |
with gr.Row():
|
61 |
+
input_box = gr.Textbox(label="Input Text", lines=4, interactive=True)
|
62 |
+
output_box = gr.Textbox(label="Predicted Label", lines=2)
|
63 |
|
64 |
with gr.Row():
|
65 |
submit_btn = gr.Button("Submit")
|
|
|
70 |
submit_btn.click(fn=infer_and_log, inputs=input_box, outputs=output_box)
|
71 |
clear_btn.click(fn=clear_fields, outputs=[input_box, output_box])
|
72 |
|
73 |
+
gr.Button("Save Logs to HF Dataset").click(fn=save_to_hf, outputs=status_box)
|
74 |
|
75 |
if __name__ == "__main__":
|
76 |
demo.launch()
|