Commit
·
1dd63b2
1
Parent(s):
db4a849
Add application file
Browse files- app.py +29 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Define the models
|
| 5 |
+
model = pipeline("text-classification",
|
| 6 |
+
model="OpenAlex/bert-base-multilingual-cased-finetuned-openalex-topic-classification-title-abstract")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def classify_text(text, top_k):
|
| 10 |
+
result = model(text, top_k=top_k, truncation=True, max_length=512)
|
| 11 |
+
return {p["label"]: p["score"] for p in result}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
with gr.Blocks() as demo:
|
| 15 |
+
gr.Interface(
|
| 16 |
+
fn=classify_text,
|
| 17 |
+
inputs=[gr.Textbox(lines=5, label="Text", placeholder="<TITLE> {title}\n<ABSTRACT> {abstract}",
|
| 18 |
+
value="<TITLE> {title}\n<ABSTRACT> {abstract}"),
|
| 19 |
+
gr.Number(label="top_k", value=10, precision=0)],
|
| 20 |
+
outputs=gr.Label(label="openalex topic predicted"),
|
| 21 |
+
title="OpenAlex topic classification",
|
| 22 |
+
description="Enter a text and see the topic classification result!",
|
| 23 |
+
flagging_mode="never",
|
| 24 |
+
api_name="classify"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
print(gr.__version__)
|
| 29 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|