Fanny1366 commited on
Commit
1b3da24
·
verified ·
1 Parent(s): 76d00cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -5,7 +5,7 @@ import numpy as np
5
  import torch
6
  from evaluate import load
7
 
8
- # 加载情感分析模型和分词器
9
  try:
10
  model = AutoModelForSequenceClassification.from_pretrained(
11
  "tabularisai/multilingual-sentiment-analysis",
@@ -20,15 +20,15 @@ try:
20
  tokenizer=tokenizer
21
  )
22
  except Exception as e:
23
- st.error(f"加载情感分析模型时出错: {e}")
24
 
25
- # 加载关键词提取模型
26
  try:
27
  kw_model = KeyBERT()
28
  except Exception as e:
29
- st.error(f"加载关键词提取模型时出错: {e}")
30
 
31
- # 定义标签映射
32
  label_map = {
33
  "Very Negative": 0,
34
  "Negative": 1,
@@ -37,21 +37,21 @@ label_map = {
37
  "Very Positive": 4
38
  }
39
 
40
- # Streamlit 应用标题
41
- st.title("文本情感与关键词分析")
42
 
43
- # 创建文本输入框
44
- input_text = st.text_area("请输入评价性文本", "")
45
 
46
  if input_text:
47
  try:
48
- # 情感分析
49
  result = sentiment_pipeline(input_text)
50
  predicted_label = label_map[result[0]['label']]
51
- rating = predicted_label + 1 # 将标签转换为 1 - 5 的评分
52
  confidence = result[0]['score']
53
 
54
- # 关键词提取
55
  keywords = kw_model.extract_keywords(
56
  input_text,
57
  keyphrase_ngram_range=(1, 2),
@@ -60,9 +60,9 @@ if input_text:
60
  )
61
  keyword_text = [kw[0] for kw in keywords]
62
 
63
- # 显示结果
64
- st.write(f"情感评分: {rating}/5")
65
- st.write(f"置信度: {confidence:.2f}")
66
- st.write(f"关键词: {', '.join(keyword_text)}")
67
  except Exception as e:
68
- st.error(f"分析文本时出错: {e}")
 
5
  import torch
6
  from evaluate import load
7
 
8
+ # Load sentiment analysis model and word divider
9
  try:
10
  model = AutoModelForSequenceClassification.from_pretrained(
11
  "tabularisai/multilingual-sentiment-analysis",
 
20
  tokenizer=tokenizer
21
  )
22
  except Exception as e:
23
+ st.error(f"Error loading sentiment analysis model: {e}")
24
 
25
+ # Load keyword extraction model
26
  try:
27
  kw_model = KeyBERT()
28
  except Exception as e:
29
+ st.error(f"Error loading sentiment analysis model: {e}")
30
 
31
+ # Define tag mapping
32
  label_map = {
33
  "Very Negative": 0,
34
  "Negative": 1,
 
37
  "Very Positive": 4
38
  }
39
 
40
+ # Streamlit
41
+ st.title("Text emotion and keyword analysis")
42
 
43
+ # Create a text entry box
44
+ input_text = st.text_area("Please enter user feedback", "")
45
 
46
  if input_text:
47
  try:
48
+ # sentiment analysis
49
  result = sentiment_pipeline(input_text)
50
  predicted_label = label_map[result[0]['label']]
51
+ rating = predicted_label + 1
52
  confidence = result[0]['score']
53
 
54
+ # ketwords extraction
55
  keywords = kw_model.extract_keywords(
56
  input_text,
57
  keyphrase_ngram_range=(1, 2),
 
60
  )
61
  keyword_text = [kw[0] for kw in keywords]
62
 
63
+ # result showing
64
+ st.write(f"Rating: {rating}/5")
65
+ st.write(f"Confidence: {confidence:.2f}")
66
+ st.write(f"Key words: {', '.join(keyword_text)}")
67
  except Exception as e:
68
+ st.error(f"Error while analyzing text: {e}")