OS84505 commited on
Commit
180df8d
·
verified ·
1 Parent(s): 434ba35

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ MODEL_ID = os.environ.get(
6
+ "HF_MODEL", "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
7
+ )
8
+ clf = pipeline("text-classification", model=MODEL_ID)
9
+
10
+ def predict(text: str):
11
+ text = (text or "").strip()
12
+ if not text:
13
+ return "No input", ""
14
+ out = clf(text, truncation=True, max_length=128)[0]
15
+ return out["label"], f"{float(out['score']):.6f}"
16
+
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown("# 🧠 Sentiment Analysis Demo\n"
19
+ f"Model: `{MODEL_ID}`")
20
+ inp = gr.Textbox(lines=3, label="Input Text", placeholder="Type a sentence…")
21
+ out_label = gr.Textbox(label="Predicted Label", interactive=False)
22
+ out_score = gr.Textbox(label="Confidence Score", interactive=False)
23
+ btn = gr.Button("Classify", variant="primary")
24
+ btn.click(predict, inputs=inp, outputs=[out_label, out_score], api_name="predict")
25
+
26
+ demo.queue()
27
+
28
+ if __name__ == "__main__":
29
+ demo.launch(server_name="0.0.0.0", server_port=7860)