Spaces:
Running
on
Zero
Running
on
Zero
add app
Browse files
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import pipeline
|
4 |
+
import os
|
5 |
+
|
6 |
+
#load_dotenv()
|
7 |
+
key=os.environ("HF_KEY")
|
8 |
+
|
9 |
+
|
10 |
+
def load_model():
|
11 |
+
pipe=pipeline(task="fill-mask",model="atlasia/xlm-roberta-large-ft-alatlas",token=key)
|
12 |
+
return pipe
|
13 |
+
|
14 |
+
print("[INFO] load model ...")
|
15 |
+
pipe=load_model()
|
16 |
+
print("[INFO] model loaded")
|
17 |
+
|
18 |
+
# def predict(text):
|
19 |
+
# predictions=pipe(text)
|
20 |
+
# return predictions[0]["sequence"],predictions
|
21 |
+
|
22 |
+
|
23 |
+
def predict(text):
|
24 |
+
|
25 |
+
# Get prediction
|
26 |
+
with torch.no_grad():
|
27 |
+
outputs = pipe(text)
|
28 |
+
scores= [x["score"] for x in outputs]
|
29 |
+
tokens= [x["token_str"] for x in outputs]
|
30 |
+
# scores= [x["score"] for x in outputs]
|
31 |
+
# Convert to percentages and create label-probability pairs
|
32 |
+
#probs = probabilities[0].tolist()
|
33 |
+
return {label: float(prob) * 100 for label, prob in zip(tokens, scores)}
|
34 |
+
|
35 |
+
# Create Gradio interface
|
36 |
+
with gr.Blocks() as demo:
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
# Input text box
|
40 |
+
input_text = gr.Textbox(
|
41 |
+
label="Input",
|
42 |
+
placeholder="Enter text here..."
|
43 |
+
)
|
44 |
+
|
45 |
+
# Button row
|
46 |
+
with gr.Row():
|
47 |
+
clear_btn = gr.Button("Clear")
|
48 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
49 |
+
|
50 |
+
# Examples section
|
51 |
+
gr.Examples(
|
52 |
+
examples=["Hugging Face is the AI community, working together, to [MASK] the future."],
|
53 |
+
inputs=input_text
|
54 |
+
)
|
55 |
+
|
56 |
+
with gr.Column():
|
57 |
+
# Output label
|
58 |
+
gr.Label("Classification")
|
59 |
+
|
60 |
+
# Output probabilities
|
61 |
+
output_labels = gr.Label(
|
62 |
+
label="Classification Results",
|
63 |
+
show_label=False
|
64 |
+
)
|
65 |
+
|
66 |
+
# Button actions
|
67 |
+
submit_btn.click(
|
68 |
+
predict,
|
69 |
+
inputs=input_text,
|
70 |
+
outputs=output_labels
|
71 |
+
)
|
72 |
+
|
73 |
+
clear_btn.click(
|
74 |
+
lambda: "",
|
75 |
+
outputs=input_text
|
76 |
+
)
|
77 |
+
|
78 |
+
# Launch the app
|
79 |
+
demo.launch()
|