app.py
CHANGED
@@ -1,84 +1,55 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from transformers import AutoTokenizer,
|
|
|
|
|
4 |
|
5 |
# ---------------------------------------------------------------------------
|
6 |
-
# 1) Load the
|
7 |
# ---------------------------------------------------------------------------
|
8 |
-
|
9 |
-
|
10 |
-
# For example:
|
11 |
-
#
|
12 |
-
# from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
13 |
-
# bnb_config = BitsAndBytesConfig(
|
14 |
-
# load_in_4bit=True, # or load_in_8bit=True
|
15 |
-
# bnb_4bit_compute_dtype=torch.float16, # recommended compute dtype
|
16 |
-
# bnb_4bit_use_double_quant=True, # optional
|
17 |
-
# bnb_4bit_quant_type='nf4', # optional
|
18 |
-
# )
|
19 |
-
#
|
20 |
-
# model = AutoModelForCausalLM.from_pretrained(
|
21 |
-
# "cheberle/autotrain-35swc-b4r9z",
|
22 |
-
# quantization_config=bnb_config,
|
23 |
-
# device_map="auto",
|
24 |
-
# trust_remote_code=True
|
25 |
-
# )
|
26 |
-
# tokenizer = AutoTokenizer.from_pretrained("cheberle/autotrain-35swc-b4r9z", trust_remote_code=True)
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
"cheberle/autotrain-35swc-b4r9z",
|
31 |
-
torch_dtype=torch.float16, # Or "auto", or float32
|
32 |
-
trust_remote_code=True
|
33 |
-
)
|
34 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
35 |
-
"cheberle/autotrain-35swc-b4r9z",
|
36 |
-
trust_remote_code=True
|
37 |
-
)
|
38 |
|
39 |
# ---------------------------------------------------------------------------
|
40 |
-
# 2) Define
|
41 |
# ---------------------------------------------------------------------------
|
42 |
-
def
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
)
|
55 |
-
|
56 |
-
# Decode
|
57 |
-
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
58 |
-
return decoded
|
59 |
|
60 |
# ---------------------------------------------------------------------------
|
61 |
-
# 3)
|
62 |
# ---------------------------------------------------------------------------
|
63 |
with gr.Blocks() as demo:
|
64 |
-
gr.Markdown("<h3>Demo
|
65 |
|
66 |
with gr.Row():
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
output_box = gr.Textbox(lines=15, label="Model Output")
|
76 |
|
77 |
-
#
|
78 |
-
|
79 |
|
80 |
# ---------------------------------------------------------------------------
|
81 |
-
# 4) Launch
|
82 |
# ---------------------------------------------------------------------------
|
83 |
if __name__ == "__main__":
|
84 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
4 |
+
|
5 |
+
MODEL_NAME = "cheberle/autotrain-35swc-b4r9z"
|
6 |
|
7 |
# ---------------------------------------------------------------------------
|
8 |
+
# 1) Load the tokenizer and model for sequence classification
|
9 |
# ---------------------------------------------------------------------------
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
11 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
# Create a pipeline for text classification
|
14 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# ---------------------------------------------------------------------------
|
17 |
+
# 2) Define inference function
|
18 |
# ---------------------------------------------------------------------------
|
19 |
+
def classify_text(text):
|
20 |
+
"""
|
21 |
+
Return the classification results in the format:
|
22 |
+
[
|
23 |
+
{
|
24 |
+
'label': 'POSITIVE',
|
25 |
+
'score': 0.98
|
26 |
+
}
|
27 |
+
]
|
28 |
+
"""
|
29 |
+
results = classifier(text)
|
30 |
+
return results
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# ---------------------------------------------------------------------------
|
33 |
+
# 3) Build the Gradio UI
|
34 |
# ---------------------------------------------------------------------------
|
35 |
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("<h3>Text Classification Demo</h3>")
|
37 |
|
38 |
with gr.Row():
|
39 |
+
input_text = gr.Textbox(
|
40 |
+
lines=3,
|
41 |
+
label="Enter text to classify",
|
42 |
+
placeholder="Type something..."
|
43 |
+
)
|
44 |
+
output = gr.JSON(label="Classification Output")
|
45 |
+
|
46 |
+
classify_btn = gr.Button("Classify")
|
|
|
47 |
|
48 |
+
# Link the button to the function
|
49 |
+
classify_btn.click(fn=classify_text, inputs=input_text, outputs=output)
|
50 |
|
51 |
# ---------------------------------------------------------------------------
|
52 |
+
# 4) Launch the demo
|
53 |
# ---------------------------------------------------------------------------
|
54 |
if __name__ == "__main__":
|
55 |
demo.launch()
|