Update app.py
Browse files
app.py
CHANGED
|
@@ -10,17 +10,26 @@ from huggingface_hub import HfApi, HfFolder
|
|
| 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):
|
| 25 |
inputs = tokenizer(text_input, return_tensors="pt", truncation=True)
|
| 26 |
with torch.no_grad():
|
|
@@ -55,22 +64,18 @@ def save_to_hf():
|
|
| 55 |
return f"Pushed {len(df)} logs to {HF_DATASET_REPO}!"
|
| 56 |
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
-
gr.Markdown("##
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
-
input_box = gr.Textbox(label="Input Text", lines=
|
| 62 |
-
output_box = gr.Textbox(label="Predicted Label", lines=
|
| 63 |
|
| 64 |
with gr.Row():
|
| 65 |
submit_btn = gr.Button("Submit")
|
| 66 |
clear_btn = gr.Button("Clear")
|
| 67 |
|
| 68 |
-
status_box = gr.Textbox(label="Status", interactive=False)
|
| 69 |
-
|
| 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()
|
|
|
|
| 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 |
# Token from environment in Spaces
|
| 14 |
HF_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
|
|
|
|
| 15 |
# Load model + tokenizer
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 17 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
|
|
|
| 18 |
# Log entries
|
| 19 |
log_entries = []
|
| 20 |
|
| 21 |
+
def setup_hf_dataset():
|
| 22 |
+
global DATASET_CREATED
|
| 23 |
+
if not DATASET_CREATED and HF_TOKEN:
|
| 24 |
+
try:
|
| 25 |
+
api = HfApi()
|
| 26 |
+
create_repo(DATASET_NAME, repo_type="dataset", token=HF_TOKEN, exist_ok=True)
|
| 27 |
+
DATASET_CREATED = True
|
| 28 |
+
print(f"Dataset {DATASET_NAME} is ready")
|
| 29 |
+
except Exception as e: print(f"Error setting up dataset: {e}")
|
| 30 |
+
elif not HF_TOKEN:
|
| 31 |
+
print("Warning: HF_TOKEN not set. Data will be stored locally only.")
|
| 32 |
+
|
| 33 |
def infer_and_log(text_input):
|
| 34 |
inputs = tokenizer(text_input, return_tensors="pt", truncation=True)
|
| 35 |
with torch.no_grad():
|
|
|
|
| 64 |
return f"Pushed {len(df)} logs to {HF_DATASET_REPO}!"
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
+
gr.Markdown("## AI-generated text detector")
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
+
input_box = gr.Textbox(label="Input Text", lines=6, interactive=True)
|
| 71 |
+
output_box = gr.Textbox(label="Predicted Label", lines=6)
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
submit_btn = gr.Button("Submit")
|
| 75 |
clear_btn = gr.Button("Clear")
|
| 76 |
|
|
|
|
|
|
|
| 77 |
submit_btn.click(fn=infer_and_log, inputs=input_box, outputs=output_box)
|
| 78 |
clear_btn.click(fn=clear_fields, outputs=[input_box, output_box])
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
demo.launch()
|