thanhdov commited on
Commit
9c7e5df
·
1 Parent(s): fd41173

Add requirements and Sentiment Analysis app

Browse files
Files changed (2) hide show
  1. app.py +28 -10
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,14 +1,32 @@
1
  import gradio as gr
2
- import spaces
3
- import torch
4
 
5
- zero = torch.Tensor([0]).cuda()
6
- print(zero.device) # <-- 'cpu' 🤔
 
 
 
7
 
8
- @spaces.GPU
9
- def greet(n):
10
- print(zero.device) # <-- 'cuda:0' 🤗
11
- return f"Hello {zero + n} Tensor"
12
 
13
- demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
14
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
 
4
+ # Load pipeline mô hình (ví dụ: mô hình phân loại cảm xúc văn bản)
5
+ classifier = pipeline(
6
+ "sentiment-analysis",
7
+ model="distilbert-base-uncased-finetuned-sst-2-english",
8
+ )
9
 
 
 
 
 
10
 
11
+ def analyze_text(text):
12
+ if not text.strip():
13
+ return "Vui lòng nhập văn bản cần phân tích."
14
+ result = classifier(text)[0]
15
+ label = result["label"]
16
+ score = round(result["score"], 4)
17
+ return f"Kết quả: {label} (Độ tin cậy: {score})"
18
+
19
+
20
+ # Tạo giao diện người dùng
21
+ demo = gr.Interface(
22
+ fn=analyze_text,
23
+ inputs=gr.Textbox(
24
+ lines=3, placeholder="Nhập câu tiếng Anh cần kiểm tra..."
25
+ ),
26
+ outputs="text",
27
+ title="Sentiment Analysis Demo",
28
+ description="Ứng dụng phân tích cảm xúc sử dụng mô hình Hugging Face Transformers.",
29
+ )
30
+
31
+ if __name__ == "__main__":
32
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch