Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,87 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
)
|
| 20 |
-
textgen_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen1.5-0.5B", use_fast=True)
|
| 21 |
-
textgen_model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen1.5-0.5B", torch_dtype=torch.float16)
|
| 22 |
-
tts_processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 23 |
-
tts_model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 24 |
-
tts_vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 25 |
-
speaker_embeddings = torch.tensor(
|
| 26 |
-
load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")[7306]["xvector"]
|
| 27 |
-
).unsqueeze(0)
|
| 28 |
-
|
| 29 |
-
# Emotion prompt templates
|
| 30 |
-
PROMPT_TEMPLATES = {
|
| 31 |
-
"sadness": "Sadness detected: {input}\n1. Empathetic acknowledgment\n2. Support offer\n3. Solution proposal\nResponse:",
|
| 32 |
-
"joy": "Joy detected: {input}\n1. Enthusiastic thanks\n2. Positive reinforcement\n3. Future engagement\nResponse:",
|
| 33 |
-
"love": "Affection detected: {input}\n1. Warm appreciation\n2. Community focus\n3. Exclusive benefit\nResponse:",
|
| 34 |
-
"anger": "Anger detected: {input}\n1. Sincere apology\n2. Action steps\n3. Compensation\nResponse:",
|
| 35 |
-
"fear": "Concern detected: {input}\n1. Reassurance\n2. Safety measures\n3. Support options\nResponse:",
|
| 36 |
-
"surprise": "Surprise detected: {input}\n1. Acknowledge uniqueness\n2. Creative solution\n3. Follow-up\nResponse:"
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
def analyze_emotion(text):
|
| 40 |
-
scores = emotion_classifier(text, return_all_scores=True)[0]
|
| 41 |
-
valid = [e for e in scores if e['label'].lower() in PROMPT_TEMPLATES]
|
| 42 |
-
return max(valid, key=lambda x: x['score'])['label'].lower()
|
| 43 |
-
|
| 44 |
-
def generate_response(comment):
|
| 45 |
-
emotion = analyze_emotion(comment)
|
| 46 |
-
prompt = PROMPT_TEMPLATES[emotion].format(input=comment)
|
| 47 |
-
inputs = textgen_tokenizer(prompt, return_tensors="pt").to("cpu")
|
| 48 |
-
output_ids = textgen_model.generate(
|
| 49 |
-
inputs.input_ids,
|
| 50 |
-
max_new_tokens=100,
|
| 51 |
-
temperature=0.7,
|
| 52 |
-
top_p=0.9,
|
| 53 |
-
do_sample=True,
|
| 54 |
-
pad_token_id=textgen_tokenizer.eos_token_id
|
| 55 |
-
)
|
| 56 |
-
raw_text = textgen_tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 57 |
-
result = raw_text.split("Response:")[-1].strip()
|
| 58 |
-
if '.' in result:
|
| 59 |
-
result = result.rsplit('.', 1)[0] + '.'
|
| 60 |
-
return result[:200] if len(result) > 50 else "Cảm ơn bạn đã phản hồi. Chúng tôi sẽ xem xét kỹ lưỡng."
|
| 61 |
-
|
| 62 |
-
def generate_audio(text):
|
| 63 |
-
inputs = tts_processor(text=text, return_tensors="pt")
|
| 64 |
-
with torch.no_grad():
|
| 65 |
-
speech = tts_model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
| 66 |
-
waveform = tts_vocoder(speech)
|
| 67 |
-
sf.write("response.wav", waveform.numpy(), 16000)
|
| 68 |
-
return "response.wav"
|
| 69 |
-
|
| 70 |
-
def full_pipeline(comment):
|
| 71 |
-
response = generate_response(comment)
|
| 72 |
-
audio_path = generate_audio(response)
|
| 73 |
-
return response, audio_path
|
| 74 |
-
|
| 75 |
-
# Gradio UI
|
| 76 |
-
demo = gr.Interface(
|
| 77 |
-
fn=full_pipeline,
|
| 78 |
-
inputs=gr.Textbox(label="💬 Nhập bình luận", placeholder="Ví dụ: Sản phẩm này có bền không vậy?"),
|
| 79 |
-
outputs=[
|
| 80 |
-
gr.Textbox(label="📄 Phản hồi AI"),
|
| 81 |
-
gr.Audio(label="🔊 Phát lại", type="filepath")
|
| 82 |
-
],
|
| 83 |
-
title="Just Comment 🐠 (Gradio Edition)",
|
| 84 |
-
description="Phân tích cảm xúc + phản hồi AI + chuyển thành giọng nói"
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Thay mô hình nếu bạn biết rõ hơn mô hình cũ
|
| 5 |
+
reply_pipeline = pipeline("text-generation", model="microsoft/DialoGPT-medium")
|
| 6 |
+
|
| 7 |
+
def reply_comment(comment):
|
| 8 |
+
prompt = f"Trả lời một cách thân thiện, tự nhiên, có thể hài hước:\nBình luận: {comment}\nTrả lời:"
|
| 9 |
+
response = reply_pipeline(prompt, max_length=100, do_sample=True)
|
| 10 |
+
return response[0]['generated_text'].replace(prompt, "").strip()
|
| 11 |
+
|
| 12 |
+
gr.Interface(
|
| 13 |
+
fn=reply_comment,
|
| 14 |
+
inputs=gr.Textbox(label="Nhập bình luận cần trả lời", placeholder="Ví dụ: Sản phẩm này có tốt không vậy?"),
|
| 15 |
+
outputs=gr.Textbox(label="Phản hồi AI"),
|
| 16 |
+
title="Comment Reply AI 🐠",
|
| 17 |
+
description="Nhập một bình luận, AI sẽ đề xuất câu trả lời phù hợp"
|
| 18 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|