KeviniveK commited on
Commit
91336fa
·
verified ·
1 Parent(s): 7d2cffe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -25
app.py CHANGED
@@ -1,32 +1,56 @@
1
- import streamlit as st
 
2
  from transformers import pipeline
3
 
4
- def main():
5
- # 1. 建立情感分析 pipeline
6
- sentiment_pipeline = pipeline(
7
- task="sentiment-analysis",
8
- model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
9
- )
10
 
11
- st.title("Sentiment Analysis with HuggingFace Spaces")
12
- st.write("Enter a sentence to analyze its sentiment:")
 
 
 
13
 
14
- user_input = st.text_input("")
15
- if user_input:
16
- result = sentiment_pipeline(user_input)[0]
17
- label = result["label"] # e.g. "LABEL_0" or "LABEL_1"
18
- score = result["score"]
 
 
 
 
 
 
 
 
 
 
19
 
20
- # 2. 映射成更友好的文本
21
- label_map = {
22
- "LABEL_0": "Negative",
23
- "LABEL_1": "Positive"
24
- }
25
- friendly_label = label_map.get(label, label)
26
-
27
- # 3. 输出
28
- st.write(f"Sentiment: **{friendly_label}**")
29
- st.write(f"Confidence: **{score:.2f}**")
 
 
 
 
 
 
 
 
 
 
30
 
31
  if __name__ == "__main__":
32
- main()
 
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(
38
+ label="英文评价输入",
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=[
50
+ ["This movie is absolutely fantastic! The acting was superb and the plot kept me engaged throughout."],
51
+ ["A terrible waste of time. The storyline was confusing and the acting felt forced."]
52
+ ]
53
+ )
54
 
55
  if __name__ == "__main__":
56
+ demo.launch()