entropy25 commited on
Commit
9dca84f
·
verified ·
1 Parent(s): 5a6ea0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -20
app.py CHANGED
@@ -1,41 +1,43 @@
1
  import torch
2
  import gradio as gr
3
  from transformers import BertTokenizer, BertForSequenceClassification
 
4
 
5
- # 修改这里:指向包含模型文件的文件夹,而不是直接指向 .safetensors 文件
6
- model_path = "./imdb_bert_model" # 你保存模型的文件夹路径
7
 
8
- # 加载分词器
9
- tokenizer = BertTokenizer.from_pretrained("./imdb_bert_tokenizer") # 或者使用保存的分词器路径
10
-
11
- # 使用 GPU 如果可用
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
13
 
14
- # 修改这里:直接加载整个模型,不再使用 load_state_dict
15
- model = BertForSequenceClassification.from_pretrained(model_path)
 
16
  model.to(device)
17
 
18
- # 定义情感分析函数
19
  def analyze_sentiment(text):
20
- # 将文本编码成BERT输入格式
21
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
22
 
23
- # 获取模型的预测
24
  with torch.no_grad():
25
  outputs = model(**inputs)
26
  logits = outputs.logits
27
- prediction = torch.argmax(logits, dim=-1).item() # 获取预测结果
28
- sentiment = "Positive" if prediction == 1 else "Negative" # 根据输出标签决定情感
29
 
30
  return sentiment
31
 
32
- # 创建 Gradio 接口
33
- demo = gr.Interface(fn=analyze_sentiment,
34
- inputs=gr.Textbox(lines=2, placeholder="Enter a sentence here..."),
35
- outputs=gr.Textbox(),
36
- title="IMDB Sentiment Analysis",
37
- description="Enter an English sentence to analyze its sentiment (Positive/Negative).")
 
 
38
 
39
- # 启动 Gradio 界面
40
  demo.launch()
41
 
 
1
  import torch
2
  import gradio as gr
3
  from transformers import BertTokenizer, BertForSequenceClassification
4
+ import os
5
 
6
+ # Get absolute paths to avoid path format issues
7
+ current_dir = os.path.dirname(os.path.abspath(__file__))
8
 
9
+ # Use GPU if available
 
 
 
10
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ print(f"Using device: {device}")
12
 
13
+ # Load tokenizer and model directly from the pre-trained model
14
+ tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
15
+ model = BertForSequenceClassification.from_pretrained("bert-base-uncased", num_labels=2)
16
  model.to(device)
17
 
18
+ # Define sentiment analysis function
19
  def analyze_sentiment(text):
20
+ # Encode text to BERT input format
21
  inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
22
 
23
+ # Get model predictions
24
  with torch.no_grad():
25
  outputs = model(**inputs)
26
  logits = outputs.logits
27
+ prediction = torch.argmax(logits, dim=-1).item()
28
+ sentiment = "Positive" if prediction == 1 else "Negative"
29
 
30
  return sentiment
31
 
32
+ # Create Gradio interface
33
+ demo = gr.Interface(
34
+ fn=analyze_sentiment,
35
+ inputs=gr.Textbox(lines=2, placeholder="Enter a sentence here..."),
36
+ outputs=gr.Textbox(),
37
+ title="IMDB Sentiment Analysis",
38
+ description="Enter an English sentence to analyze its sentiment (Positive/Negative)."
39
+ )
40
 
41
+ # Launch Gradio interface
42
  demo.launch()
43