Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 导入所需的包
|
2 |
+
from transformers import pipeline
|
3 |
+
from IPython.display import Audio, display
|
4 |
+
|
5 |
+
##########################################
|
6 |
+
# Step 1:情感分类 —— 分析用户评论的情感倾向
|
7 |
+
##########################################
|
8 |
+
|
9 |
+
# 在 Hugging Face 上,经过实践效果较好的情感分类模型推荐:
|
10 |
+
# 1. "distilbert-base-uncased-finetuned-sst-2-english" —— 经典正负情感模型
|
11 |
+
# 2. "cardiffnlp/twitter-roberta-base-sentiment" —— 针对社交媒体语境优化
|
12 |
+
# 3. "nlptown/bert-base-multilingual-uncased-sentiment" —— 多语种情感分析模型
|
13 |
+
# 4. "siebert/sentiment-roberta-large-english" —— Roberta 大模型版本
|
14 |
+
sentiment_models = [
|
15 |
+
"distilbert-base-uncased-finetuned-sst-2-english",
|
16 |
+
"cardiffnlp/twitter-roberta-base-sentiment",
|
17 |
+
"nlptown/bert-base-multilingual-uncased-sentiment",
|
18 |
+
"siebert/sentiment-roberta-large-english"
|
19 |
+
]
|
20 |
+
|
21 |
+
# 选取第一款模型作为示例(你可以修改索引以试用其他模型)
|
22 |
+
selected_sentiment_model = sentiment_models[0]
|
23 |
+
|
24 |
+
# 使用 transformers 的 pipeline 创建情感分析模块
|
25 |
+
sentiment_classifier = pipeline("sentiment-analysis", model=selected_sentiment_model)
|
26 |
+
|
27 |
+
# 示例用户评论(你可以将其替换为实际的用户输入)
|
28 |
+
user_review = "I love the fast delivery, but the product quality could be better."
|
29 |
+
|
30 |
+
# 运行情感分类
|
31 |
+
sentiment_result = sentiment_classifier(user_review)[0]
|
32 |
+
print("情感分析结果:", sentiment_result)
|
33 |
+
|
34 |
+
##########################################
|
35 |
+
# Step 2:回复生成 —— 根据情感生成自动回复
|
36 |
+
##########################################
|
37 |
+
|
38 |
+
# 针对文本生成任务,Hugging Face 上较好效果的模型有:
|
39 |
+
# 1. "gpt2" —— 基础文本生成模型
|
40 |
+
# 2. "microsoft/DialoGPT-medium" —— 针对对话回复优化
|
41 |
+
# 3. "EleutherAI/gpt-neo-125M" —— GPT-Neo 系列小模型
|
42 |
+
# 4. "facebook/blenderbot-400M-distill" —— BlenderBot 对话生成模型
|
43 |
+
generation_models = [
|
44 |
+
"gpt2",
|
45 |
+
"microsoft/DialoGPT-medium",
|
46 |
+
"EleutherAI/gpt-neo-125M",
|
47 |
+
"facebook/blenderbot-400M-distill"
|
48 |
+
]
|
49 |
+
|
50 |
+
# 此处选取 "microsoft/DialoGPT-medium" 作为生成回复的模型
|
51 |
+
selected_generation_model = generation_models[1]
|
52 |
+
reply_generator = pipeline("text-generation", model=selected_generation_model)
|
53 |
+
|
54 |
+
# 根据情感结果构造生成回复用的 prompt
|
55 |
+
# 注意不同模型返回的标签可能不同,有的直接写 "POSITIVE"/"NEGATIVE",有的可能返回 "LABEL_1"/"LABEL_0"
|
56 |
+
label = sentiment_result['label'].upper() # 转为大写方便比较
|
57 |
+
|
58 |
+
if "POSITIVE" in label or "LABEL_1" in label:
|
59 |
+
# 正面评论的回复模板:表达感谢和认同(拟人化、贴心)
|
60 |
+
prompt = f"用户评论表达了正面情感,评论内容: {user_review}\n系统回复(请用贴心温暖的语气,表达感谢和认可):"
|
61 |
+
elif "NEGATIVE" in label or "LABEL_0" in label:
|
62 |
+
# 负面评论的回复模板:使用温柔的安抚语气,表达歉意和关切
|
63 |
+
prompt = f"用户评论表达了负面情感,评论内容: {user_review}\n系统回复(请用温柔体贴的话语进行安抚和道歉):"
|
64 |
+
else:
|
65 |
+
# 如果情感不明确,可使用中性模板
|
66 |
+
prompt = f"用户评论: {user_review}\n系统回复:"
|
67 |
+
|
68 |
+
print("\n生成回复的提示词 (Prompt):")
|
69 |
+
print(prompt)
|
70 |
+
|
71 |
+
# 生成回复文字
|
72 |
+
# 设置 max_length: 最大生成长度;do_sample, top_p, temperature 控制生成风格和随机性
|
73 |
+
generated = reply_generator(prompt, max_length=120, do_sample=True, top_p=0.9, temperature=0.8)
|
74 |
+
generated_reply = generated[0]['generated_text']
|
75 |
+
print("\n生成的回复文本:")
|
76 |
+
print(generated_reply)
|
77 |
+
|
78 |
+
##########################################
|
79 |
+
# Step 3:文本转语音(TTS) —— 将回复朗读出来
|
80 |
+
##########################################
|
81 |
+
|
82 |
+
# Hugging Face 上几款 TTS 模型的候选:
|
83 |
+
# 1. "microsoft/speecht5_tts" —— 微软的 SpeechT5 TTS 模型,支持情感化控制;
|
84 |
+
# 2. "espnet/kan-bayashi_ljspeech_tts_fastspeech2" —— 基于 FastSpeech2 的模型;
|
85 |
+
# 3. "espnet/kan-bayashi_ljspeech_tts_tacotron2" —— 基于 Tacotron2 的模型;
|
86 |
+
# 4. “facebook/s2tts” —— 示例(如果可用,实际情况可选择其他推荐的 TTS 模型)
|
87 |
+
tts_models = [
|
88 |
+
"microsoft/speecht5_tts",
|
89 |
+
"espnet/kan-bayashi_ljspeech_tts_fastspeech2",
|
90 |
+
"espnet/kan-bayashi_ljspeech_tts_tacotron2",
|
91 |
+
"facebook/s2tts"
|
92 |
+
]
|
93 |
+
|
94 |
+
# 选取第一款作为示例
|
95 |
+
selected_tts_model = tts_models[0]
|
96 |
+
tts_pipeline = pipeline("text-to-speech", model=selected_tts_model)
|
97 |
+
|
98 |
+
# 利用 TTS 模型将生成的回复文本转换为语音
|
99 |
+
tts_output = tts_pipeline(generated_reply)
|
100 |
+
|
101 |
+
# tts_output 通常返回一个字典,其中包含 "wav" (二进制音频数据) 和 "sample_rate"
|
102 |
+
speech_wav = tts_output["wav"]
|
103 |
+
sample_rate = tts_output["sample_rate"]
|
104 |
+
|
105 |
+
# (可选)保存生成的语音文件到本地文件系统(Colab 中生成)
|
106 |
+
with open("reply.wav", "wb") as f:
|
107 |
+
f.write(speech_wav)
|
108 |
+
|
109 |
+
# 在 Colab 中直接播放语音,可以用 IPython.display.Audio
|
110 |
+
print("\n���放生成的回复语音:")
|
111 |
+
display(Audio(data=speech_wav, rate=sample_rate))
|