KeviniveK commited on
Commit
7344fc9
·
verified ·
1 Parent(s): 91336fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -1,37 +1,35 @@
1
  # app.py
2
  import gradio as gr
3
  from transformers import pipeline
 
 
 
 
4
 
5
  # 初始化模型pipeline
6
- # 情感分析模型(假设输出格式为 {'label': 'POSITIVE/NEGATIVE', 'score': float})
7
  sentiment_pipeline = pipeline(
8
  "text-classification",
9
- model="KeviniveK/CustomModel_IMDB"
 
10
  )
11
 
12
- # 翻译模型(英文->中文)
13
  translation_pipeline = pipeline(
14
  "translation",
15
- model="Helsinki-NLP/opus-mt-en-zh"
 
16
  )
17
 
18
  def analyze_and_translate(text):
19
- # 情感分析
20
  sentiment_result = sentiment_pipeline(text)[0]
21
-
22
- # 翻译处理
23
  translation_result = translation_pipeline(text)[0]['translation_text']
24
-
25
- # 格式化置信度为百分比
26
  confidence = f"{sentiment_result['score']*100:.2f}%"
27
-
28
  return {
29
  "情感分类": sentiment_result['label'],
30
  "置信度": confidence,
31
  "中文翻译": translation_result
32
  }
33
 
34
- # 创建Gradio界面
35
  demo = gr.Interface(
36
  fn=analyze_and_translate,
37
  inputs=gr.Textbox(
@@ -39,11 +37,7 @@ demo = gr.Interface(
39
  placeholder="Enter your English review here...",
40
  lines=3
41
  ),
42
- outputs=[
43
- gr.Textbox(label="情感分类"),
44
- gr.Textbox(label="置信度"),
45
- gr.Textbox(label="中文翻译")
46
- ],
47
  title="🎬 IMDB评价情感分析+翻译系统",
48
  description="输入英文影评,获取情感分析结果(分类+置信度)和中文翻译",
49
  examples=[
@@ -53,4 +47,4 @@ demo = gr.Interface(
53
  )
54
 
55
  if __name__ == "__main__":
56
- demo.launch()
 
1
  # app.py
2
  import gradio as gr
3
  from transformers import pipeline
4
+ import torch
5
+
6
+ # 使用 GPU 如果可用
7
+ device = 0 if torch.cuda.is_available() else -1
8
 
9
  # 初始化模型pipeline
 
10
  sentiment_pipeline = pipeline(
11
  "text-classification",
12
+ model="KeviniveK/CustomModel_IMDB",
13
+ device=device
14
  )
15
 
 
16
  translation_pipeline = pipeline(
17
  "translation",
18
+ model="Helsinki-NLP/opus-mt-en-zh",
19
+ device=device
20
  )
21
 
22
  def analyze_and_translate(text):
 
23
  sentiment_result = sentiment_pipeline(text)[0]
 
 
24
  translation_result = translation_pipeline(text)[0]['translation_text']
 
 
25
  confidence = f"{sentiment_result['score']*100:.2f}%"
 
26
  return {
27
  "情感分类": sentiment_result['label'],
28
  "置信度": confidence,
29
  "中文翻译": translation_result
30
  }
31
 
32
+ # 使用JSON组件简化输出
33
  demo = gr.Interface(
34
  fn=analyze_and_translate,
35
  inputs=gr.Textbox(
 
37
  placeholder="Enter your English review here...",
38
  lines=3
39
  ),
40
+ outputs=gr.JSON(label="分析结果"),
 
 
 
 
41
  title="🎬 IMDB评价情感分析+翻译系统",
42
  description="输入英文影评,获取情感分析结果(分类+置信度)和中文翻译",
43
  examples=[
 
47
  )
48
 
49
  if __name__ == "__main__":
50
+ demo.launch()