Yi-666 commited on
Commit
82baf39
·
verified ·
1 Parent(s): af0957c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -17
app.py CHANGED
@@ -1,25 +1,65 @@
1
- # Use a pipeline as a high-level helper
2
  from transformers import pipeline
 
 
 
3
 
 
4
  pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
5
- # Load model directly
6
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
 
8
- tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
9
- model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
10
- import streamlit as st
11
- from transformers import pipeline
12
 
13
- # 加载情感分析模型
14
- pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # 设置页面标题
17
- st.title("Twitter Sentiment Analysis App")
 
 
 
 
 
 
18
 
19
- # 输入框让用户输入文本
20
- user_input = st.text_input("Enter a tweet to analyze:")
 
 
 
 
 
21
 
22
- if user_input:
23
- # 调用模型分析用户输入
24
- result = pipe(user_input)
25
- st.write("Sentiment Analysis Result:", result)
 
1
+ import streamlit as st
2
  from transformers import pipeline
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
+ import matplotlib.pyplot as plt
5
+ from sklearn.metrics import precision_recall_curve, auc
6
 
7
+ # 初始化情感分析模型
8
  pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest")
 
 
9
 
10
+ # 定义页面功能
11
+ st.sidebar.title("App Navigation")
12
+ page = st.sidebar.radio("Choose a feature", ["Sentiment Analysis", "Model Evaluation"])
 
13
 
14
+ if page == "Sentiment Analysis":
15
+ # 设置页面标题
16
+ st.title("Twitter Sentiment Analysis App")
17
+
18
+ # 输入框让用户输入文本
19
+ user_input = st.text_input("Enter a tweet to analyze:")
20
+
21
+ if user_input:
22
+ # 调用模型分析用户输入
23
+ result = pipe(user_input)
24
+ st.write("Sentiment Analysis Result:", result)
25
+
26
+ elif page == "Model Evaluation":
27
+ # 设置评估页面标题
28
+ st.title("Model Precision-Recall Evaluation")
29
+
30
+ # 上传真实标签和预测概率
31
+ st.write("### 输入数据")
32
+ y_true_input = st.text_area("输入真实标签 (用逗号分隔)", "1,0,1,1,0,1")
33
+ y_score_input = st.text_area("输入预测概率 (用逗号分隔)", "0.95,0.1,0.85,0.75,0.2,0.9")
34
+
35
+ if y_true_input and y_score_input:
36
+ try:
37
+ # 解析输入
38
+ y_true = list(map(int, y_true_input.split(",")))
39
+ y_score = list(map(float, y_score_input.split(",")))
40
+
41
+ # 检查长度是否一致
42
+ if len(y_true) != len(y_score):
43
+ st.error("真实标签和预测概率的长度不一致!请重新输入。")
44
+ else:
45
+ # 计算 Precision 和 Recall
46
+ precision, recall, _ = precision_recall_curve(y_true, y_score)
47
+ pr_auc = auc(recall, precision)
48
 
49
+ # 绘制 PR 曲线
50
+ fig, ax = plt.subplots()
51
+ ax.plot(recall, precision, label=f"PR Curve (AUC = {pr_auc:.2f})")
52
+ ax.set_xlabel("Recall")
53
+ ax.set_ylabel("Precision")
54
+ ax.set_title("Precision-Recall Curve")
55
+ ax.legend(loc="best")
56
+ ax.grid()
57
 
58
+ # 显示图像
59
+ st.pyplot(fig)
60
+ st.success(f"PR Curve AUC: {pr_auc:.2f}")
61
+ except Exception as e:
62
+ st.error(f"发生错误: {e}")
63
+ else:
64
+ st.info("请输入真实标签和预测概率以生成 PR 曲线。")
65