Spaces:
Sleeping
Sleeping
Commit ·
51c3e2c
1
Parent(s): 0f2517d
feat: Weon analise bot
Browse files- .editorconfig +12 -0
- .gitignore +4 -0
- app.py +53 -26
- dataset.py +12 -0
- prompt.py +39 -0
- requirements.txt +83 -1
- schema.py +58 -0
- utils/utils.py +15 -0
.editorconfig
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# EditorConfig is awesome: https://EditorConfig.org
|
| 2 |
+
|
| 3 |
+
# top-most EditorConfig file
|
| 4 |
+
root = true
|
| 5 |
+
|
| 6 |
+
[*]
|
| 7 |
+
indent_style = space
|
| 8 |
+
indent_size = 4
|
| 9 |
+
end_of_line = lf
|
| 10 |
+
charset = utf-8
|
| 11 |
+
trim_trailing_whitespace = true
|
| 12 |
+
insert_final_newline = true
|
.gitignore
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv
|
| 2 |
+
__pycache__
|
| 3 |
+
.env
|
| 4 |
+
.python-version
|
app.py
CHANGED
|
@@ -1,31 +1,60 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
-
message,
|
| 12 |
-
history:
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
|
@@ -34,19 +63,15 @@ def respond(
|
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="
|
| 50 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
|
@@ -57,6 +82,8 @@ demo = gr.ChatInterface(
|
|
| 57 |
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
| 1 |
+
from typing import List, Optional
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
+
from dataset import get_response_from_huggingface_dataset
|
| 8 |
+
from prompt import prompt_for_template, template_bot
|
| 9 |
+
from schema import WeonTest
|
| 10 |
+
from utils.utils import load_token
|
| 11 |
+
|
| 12 |
+
description: str = WeonTest.description
|
| 13 |
+
rules: str = WeonTest.rules
|
| 14 |
+
behavior: str = WeonTest.comportamento
|
| 15 |
+
examples: str = WeonTest.examples
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
MODEL: str = "meta-llama/Llama-3.2-3B-Instruct"
|
| 19 |
+
#TOKEN: str = load_token("token_env")
|
| 20 |
+
|
| 21 |
+
TEMPLATE_BOT = template_bot()
|
| 22 |
+
prompt_template = prompt_for_template(TEMPLATE_BOT)
|
| 23 |
+
|
| 24 |
+
DATASET = load_dataset("wendellast/GUI-Ban")
|
| 25 |
+
|
| 26 |
+
client: InferenceClient = InferenceClient(model=MODEL)
|
| 27 |
|
| 28 |
|
| 29 |
def respond(
|
| 30 |
+
message: str,
|
| 31 |
+
history: List[dict],
|
| 32 |
+
system_message: str,
|
| 33 |
+
max_tokens: int,
|
| 34 |
+
temperature: float,
|
| 35 |
+
top_p: float,
|
| 36 |
+
) -> any:
|
| 37 |
+
response: Optional[str] = get_response_from_huggingface_dataset(message, DATASET)
|
| 38 |
+
if response:
|
| 39 |
+
yield response
|
| 40 |
+
return
|
| 41 |
|
| 42 |
+
historico = "\n".join(
|
| 43 |
+
f"{entry['role'].capitalize()}: {entry['content']}" for entry in history
|
| 44 |
+
)
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
prompt: str = prompt_template.format(
|
| 47 |
+
description=description,
|
| 48 |
+
regras=rules,
|
| 49 |
+
comportamento=behavior,
|
| 50 |
+
exemplos=examples,
|
| 51 |
+
mensagem=message,
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
print(prompt)
|
| 55 |
+
|
| 56 |
+
messages: List[dict] = [{"role": "system", "content": prompt}]
|
| 57 |
+
response: str = ""
|
| 58 |
|
| 59 |
for message in client.chat_completion(
|
| 60 |
messages,
|
|
|
|
| 63 |
temperature=temperature,
|
| 64 |
top_p=top_p,
|
| 65 |
):
|
| 66 |
+
token: str = message.choices[0].delta.content
|
|
|
|
| 67 |
response += token
|
| 68 |
yield response
|
| 69 |
|
| 70 |
|
| 71 |
+
demo: gr.ChatInterface = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
| 72 |
respond,
|
| 73 |
additional_inputs=[
|
| 74 |
+
gr.Textbox(value="", label="System message"),
|
| 75 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 76 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 77 |
gr.Slider(
|
|
|
|
| 82 |
label="Top-p (nucleus sampling)",
|
| 83 |
),
|
| 84 |
],
|
| 85 |
+
title="WeOn-BOT",
|
| 86 |
+
type="messages",
|
| 87 |
)
|
| 88 |
|
| 89 |
|
dataset.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Optional
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def get_response_from_huggingface_dataset(message: str, DATASET) -> Optional[str]:
|
| 5 |
+
for data in DATASET["train"]:
|
| 6 |
+
if "dialog" in data and len(data["dialog"]) > 1:
|
| 7 |
+
input_text: str = data["dialog"][0].lower()
|
| 8 |
+
response_text: str = data["dialog"][1]
|
| 9 |
+
|
| 10 |
+
if input_text == message.lower():
|
| 11 |
+
return response_text
|
| 12 |
+
return None
|
prompt.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.prompts import PromptTemplate
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def template_bot() -> str:
|
| 5 |
+
template: str = """
|
| 6 |
+
Descrição:
|
| 7 |
+
- Você é {description}
|
| 8 |
+
- Aqui estão algumas regras que você deve seguir:
|
| 9 |
+
|
| 10 |
+
Regras:
|
| 11 |
+
{regras}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
se comporte assim:
|
| 15 |
+
{comportamento}
|
| 16 |
+
|
| 17 |
+
Aqui estao alguns exemplos de messagens:
|
| 18 |
+
{exemplos}
|
| 19 |
+
|
| 20 |
+
Usuário: {mensagem}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
"""
|
| 25 |
+
return template
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def prompt_for_template(template_bot: str) -> str:
|
| 29 |
+
prompt_template: PromptTemplate = PromptTemplate(
|
| 30 |
+
input_variables=[
|
| 31 |
+
"description",
|
| 32 |
+
"regras",
|
| 33 |
+
"comportamento",
|
| 34 |
+
"exemplos" "mensagem",
|
| 35 |
+
],
|
| 36 |
+
template=template_bot,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
return prompt_template
|
requirements.txt
CHANGED
|
@@ -1 +1,83 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
aiofiles==23.2.1
|
| 2 |
+
aiohappyeyeballs==2.4.4
|
| 3 |
+
aiohttp==3.11.11
|
| 4 |
+
aiosignal==1.3.2
|
| 5 |
+
annotated-types==0.7.0
|
| 6 |
+
anyio==4.8.0
|
| 7 |
+
async-timeout==4.0.3
|
| 8 |
+
attrs==24.3.0
|
| 9 |
+
black==24.10.0
|
| 10 |
+
certifi==2024.12.14
|
| 11 |
+
charset-normalizer==3.4.1
|
| 12 |
+
click==8.1.8
|
| 13 |
+
datasets==3.2.0
|
| 14 |
+
dill==0.3.8
|
| 15 |
+
exceptiongroup==1.2.2
|
| 16 |
+
fastapi==0.115.6
|
| 17 |
+
ffmpy==0.5.0
|
| 18 |
+
filelock==3.16.1
|
| 19 |
+
frozenlist==1.5.0
|
| 20 |
+
fsspec==2024.9.0
|
| 21 |
+
gradio==5.11.0
|
| 22 |
+
gradio_client==1.5.3
|
| 23 |
+
greenlet==3.1.1
|
| 24 |
+
h11==0.14.0
|
| 25 |
+
httpcore==1.0.7
|
| 26 |
+
httpx==0.28.1
|
| 27 |
+
huggingface-hub==0.27.1
|
| 28 |
+
idna==3.10
|
| 29 |
+
isort==5.13.2
|
| 30 |
+
Jinja2==3.1.5
|
| 31 |
+
jsonpatch==1.33
|
| 32 |
+
jsonpointer==3.0.0
|
| 33 |
+
langchain==0.3.14
|
| 34 |
+
langchain-core==0.3.29
|
| 35 |
+
langchain-text-splitters==0.3.5
|
| 36 |
+
langsmith==0.2.10
|
| 37 |
+
markdown-it-py==3.0.0
|
| 38 |
+
MarkupSafe==2.1.5
|
| 39 |
+
mdurl==0.1.2
|
| 40 |
+
multidict==6.1.0
|
| 41 |
+
multiprocess==0.70.16
|
| 42 |
+
mypy-extensions==1.0.0
|
| 43 |
+
numpy==1.26.4
|
| 44 |
+
orjson==3.10.14
|
| 45 |
+
packaging==24.2
|
| 46 |
+
pandas==2.2.3
|
| 47 |
+
pathspec==0.12.1
|
| 48 |
+
pillow==11.1.0
|
| 49 |
+
platformdirs==4.3.6
|
| 50 |
+
propcache==0.2.1
|
| 51 |
+
pyarrow==18.1.0
|
| 52 |
+
pydantic==2.10.5
|
| 53 |
+
pydantic_core==2.27.2
|
| 54 |
+
pydub==0.25.1
|
| 55 |
+
Pygments==2.19.1
|
| 56 |
+
python-dateutil==2.9.0.post0
|
| 57 |
+
python-dotenv==1.0.1
|
| 58 |
+
python-multipart==0.0.20
|
| 59 |
+
pytz==2024.2
|
| 60 |
+
PyYAML==6.0.2
|
| 61 |
+
requests==2.32.3
|
| 62 |
+
requests-toolbelt==1.0.0
|
| 63 |
+
rich==13.9.4
|
| 64 |
+
ruff==0.9.0
|
| 65 |
+
safehttpx==0.1.6
|
| 66 |
+
semantic-version==2.10.0
|
| 67 |
+
shellingham==1.5.4
|
| 68 |
+
six==1.17.0
|
| 69 |
+
sniffio==1.3.1
|
| 70 |
+
SQLAlchemy==2.0.36
|
| 71 |
+
starlette==0.41.3
|
| 72 |
+
tenacity==9.0.0
|
| 73 |
+
tomli==2.2.1
|
| 74 |
+
tomlkit==0.13.2
|
| 75 |
+
tqdm==4.67.1
|
| 76 |
+
typer==0.15.1
|
| 77 |
+
typing_extensions==4.12.2
|
| 78 |
+
tzdata==2024.2
|
| 79 |
+
urllib3==2.3.0
|
| 80 |
+
uvicorn==0.34.0
|
| 81 |
+
websockets==14.1
|
| 82 |
+
xxhash==3.5.0
|
| 83 |
+
yarl==1.18.3
|
schema.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class WeonBot:
|
| 2 |
+
def __init__(self, description: str, rules: str, behavior: str, examples: str):
|
| 3 |
+
self.description: str = description
|
| 4 |
+
self.rules: str = rules
|
| 5 |
+
self.comportamento: str = behavior
|
| 6 |
+
self.examples: str = examples
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
description: str = """
|
| 10 |
+
Você é uma assistente especializada em analisar mensagens e críticas dos usuários. Suas respostas são claras, objetivas e ajudam a resolver problemas de forma eficiente.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
behavior: str = (
|
| 14 |
+
"Analise a messagem do usuario e retorne se a messagem é (neutra, positiva, negativa), sua resposta deve apenas conter uma das 3 opçoes e nada mais, sua resposta deve ser **apenas uma das 3 opçoes**, sem comentários ou explicações adicionais."
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
rules: str = """
|
| 18 |
+
# sua resposta deve ser **apenas uma das 3 opçoes**, sem comentários ou explicações adicionais.
|
| 19 |
+
# Se a messagem nao for negativa mas tiver alguma critica classifique ela como Neutra
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
examples: str = """
|
| 23 |
+
# Positiva:
|
| 24 |
+
A equipe de suporte foi extremamente atenciosa
|
| 25 |
+
e dedicada. Adorei o atendimento, pois desde o
|
| 26 |
+
início até a resolução do meu problema fui
|
| 27 |
+
informado de cada etapa do processo. Eles
|
| 28 |
+
fizeram de tudo para que eu entendesse o que
|
| 29 |
+
estava acontecendo e até me ofereceram um
|
| 30 |
+
acompanhamento extra para garantir que tudo
|
| 31 |
+
estivesse funcionando corretamente após a
|
| 32 |
+
solução.
|
| 33 |
+
|
| 34 |
+
# Negativa:
|
| 35 |
+
O serviço foi muito demorado e o atendente
|
| 36 |
+
parecia completamente despreparado. Precisei
|
| 37 |
+
repetir meu problema várias vezes, e mesmo
|
| 38 |
+
assim senti que ele não estava entendendo o
|
| 39 |
+
que eu estava dizendo. Perdi muito tempo, e o
|
| 40 |
+
pior de tudo é que o problema não foi resolvido
|
| 41 |
+
ao final. Vou reconsiderar continuar usando esse
|
| 42 |
+
serviço
|
| 43 |
+
|
| 44 |
+
# Neutra:
|
| 45 |
+
O atendimento foi rápido e eficiente, mas senti
|
| 46 |
+
que poderia ser mais detalhado em alguns
|
| 47 |
+
pontos técnicos. Por exemplo, ao explicar a
|
| 48 |
+
falha que ocorreu, o atendente não conseguiu
|
| 49 |
+
detalhar a causa raiz do problema, o que me
|
| 50 |
+
deixou com dúvidas sobre o que realmente
|
| 51 |
+
aconteceu. No geral, foi uma experiência
|
| 52 |
+
satisfatória, mas acredito que poderia ser mais
|
| 53 |
+
completa
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
WeonTest: WeonBot = WeonBot(
|
| 57 |
+
behavior=behavior, description=description, rules=rules, examples=examples
|
| 58 |
+
)
|
utils/utils.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def load_token(token_env):
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
token = os.getenv(token_env)
|
| 11 |
+
|
| 12 |
+
if token is None:
|
| 13 |
+
raise ValueError("Token não encontrado no arquivo .env")
|
| 14 |
+
|
| 15 |
+
return str(token)
|