Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -90,22 +90,19 @@ def classify_only(text: str):
|
|
| 90 |
# 2. IA Generativa (LLaMA 3) para resposta ao cliente
|
| 91 |
# ======================================================================
|
| 92 |
|
| 93 |
-
#
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
model=GEN_MODEL_ID,
|
| 102 |
-
tokenizer=GEN_MODEL_ID,
|
| 103 |
-
)
|
| 104 |
|
| 105 |
|
| 106 |
def build_prompt(history, user_text, sentimento_json):
|
| 107 |
"""
|
| 108 |
-
Constroi um prompt amigavel para
|
| 109 |
NENHUMA referencia a processo interno aparece na resposta.
|
| 110 |
"""
|
| 111 |
sentimento = None
|
|
@@ -117,7 +114,7 @@ def build_prompt(history, user_text, sentimento_json):
|
|
| 117 |
if sentimento is None:
|
| 118 |
sentimento = "nao identificado"
|
| 119 |
|
| 120 |
-
#
|
| 121 |
prompt = (
|
| 122 |
"VocΓͺ Γ© um atendente virtual educado, empΓ‘tico e profissional "
|
| 123 |
"de uma loja online. Responda SEMPRE em portuguΓͺs do Brasil, "
|
|
@@ -131,7 +128,7 @@ def build_prompt(history, user_text, sentimento_json):
|
|
| 131 |
"HistΓ³rico da conversa:\n"
|
| 132 |
)
|
| 133 |
|
| 134 |
-
#
|
| 135 |
if history:
|
| 136 |
for user, bot in history:
|
| 137 |
prompt += f"Cliente: {user}\n"
|
|
@@ -140,13 +137,13 @@ def build_prompt(history, user_text, sentimento_json):
|
|
| 140 |
# Nova mensagem
|
| 141 |
prompt += f"Cliente: {user_text}\n"
|
| 142 |
prompt += "Atendente:"
|
| 143 |
-
|
| 144 |
return prompt
|
| 145 |
|
| 146 |
|
| 147 |
def generate_reply_with_history(history, user_text, sentimento_json):
|
| 148 |
"""
|
| 149 |
-
Gera uma resposta levando em conta historico + sentimento
|
|
|
|
| 150 |
"""
|
| 151 |
if not user_text or user_text.strip() == "":
|
| 152 |
return "Digite uma mensagem."
|
|
@@ -155,11 +152,10 @@ def generate_reply_with_history(history, user_text, sentimento_json):
|
|
| 155 |
|
| 156 |
outputs = generator(
|
| 157 |
prompt,
|
| 158 |
-
|
|
|
|
| 159 |
temperature=0.7,
|
| 160 |
top_p=0.9,
|
| 161 |
-
do_sample=True,
|
| 162 |
-
return_full_text=False,
|
| 163 |
)
|
| 164 |
|
| 165 |
reply = outputs[0]["generated_text"]
|
|
|
|
| 90 |
# 2. IA Generativa (LLaMA 3) para resposta ao cliente
|
| 91 |
# ======================================================================
|
| 92 |
|
| 93 |
+
# ======================================================================
|
| 94 |
+
# 2. IA Generativa (FLAN-T5) para resposta ao cliente
|
| 95 |
+
# ======================================================================
|
| 96 |
+
|
| 97 |
+
GEN_MODEL_ID = os.getenv("GEN_MODEL_ID", "google/flan-t5-base")
|
| 98 |
|
| 99 |
+
# text2text-generation funciona muito bem com FLAN
|
| 100 |
+
generator = hf_pipeline("text2text-generation", model=GEN_MODEL_ID)
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
def build_prompt(history, user_text, sentimento_json):
|
| 104 |
"""
|
| 105 |
+
Constroi um prompt amigavel para FLAN-T5, usando historico + sentimento.
|
| 106 |
NENHUMA referencia a processo interno aparece na resposta.
|
| 107 |
"""
|
| 108 |
sentimento = None
|
|
|
|
| 114 |
if sentimento is None:
|
| 115 |
sentimento = "nao identificado"
|
| 116 |
|
| 117 |
+
# CabeΓ§alho de instruΓ§Γ£o (modelo vΓͺ, cliente nΓ£o)
|
| 118 |
prompt = (
|
| 119 |
"VocΓͺ Γ© um atendente virtual educado, empΓ‘tico e profissional "
|
| 120 |
"de uma loja online. Responda SEMPRE em portuguΓͺs do Brasil, "
|
|
|
|
| 128 |
"HistΓ³rico da conversa:\n"
|
| 129 |
)
|
| 130 |
|
| 131 |
+
# HistΓ³rico anterior
|
| 132 |
if history:
|
| 133 |
for user, bot in history:
|
| 134 |
prompt += f"Cliente: {user}\n"
|
|
|
|
| 137 |
# Nova mensagem
|
| 138 |
prompt += f"Cliente: {user_text}\n"
|
| 139 |
prompt += "Atendente:"
|
|
|
|
| 140 |
return prompt
|
| 141 |
|
| 142 |
|
| 143 |
def generate_reply_with_history(history, user_text, sentimento_json):
|
| 144 |
"""
|
| 145 |
+
Gera uma resposta levando em conta historico + sentimento,
|
| 146 |
+
usando FLAN-T5 em modo text2text-generation.
|
| 147 |
"""
|
| 148 |
if not user_text or user_text.strip() == "":
|
| 149 |
return "Digite uma mensagem."
|
|
|
|
| 152 |
|
| 153 |
outputs = generator(
|
| 154 |
prompt,
|
| 155 |
+
max_length=160,
|
| 156 |
+
do_sample=True,
|
| 157 |
temperature=0.7,
|
| 158 |
top_p=0.9,
|
|
|
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
reply = outputs[0]["generated_text"]
|