Spaces:
Sleeping
Sleeping
Upload try 01
#3
by
ThieLin
- opened
app.py
CHANGED
@@ -1,64 +1,82 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
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,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
"""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.
|
52 |
-
gr.
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering, pipeline
|
4 |
+
from sentence_transformers import SentenceTransformer, util
|
5 |
|
6 |
+
# Carregar modelos
|
7 |
+
model_name = "deepset/roberta-base-squad2"
|
8 |
+
qa_pipeline = pipeline('question-answering', model=model_name, tokenizer=model_name)
|
9 |
+
chat_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
+
embed_model = SentenceTransformer('all-MiniLM-L6-v2')
|
11 |
|
12 |
|
13 |
+
class MultiModelQA:
|
14 |
+
def __init__(self, qa_pipeline, chat_client, embed_model):
|
15 |
+
self.qa_pipeline = qa_pipeline
|
16 |
+
self.chat_client = chat_client
|
17 |
+
self.embed_model = embed_model
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def answer_with_qa_model(self, question, context):
|
20 |
+
return self.qa_pipeline({'question': question, 'context': context})['answer']
|
|
|
|
|
|
|
21 |
|
22 |
+
def answer_with_chat_model(self, question, system_message, max_tokens, temperature, top_p):
|
23 |
+
messages = [
|
24 |
+
{"role": "system", "content": system_message},
|
25 |
+
{"role": "user", "content": question}
|
26 |
+
]
|
27 |
+
response = ""
|
28 |
+
for msg in self.chat_client.chat_completion(
|
29 |
+
messages,
|
30 |
+
max_tokens=max_tokens,
|
31 |
+
stream=True,
|
32 |
+
temperature=temperature,
|
33 |
+
top_p=top_p,
|
34 |
+
):
|
35 |
+
token = msg.choices[0].delta.content
|
36 |
+
response += token
|
37 |
+
return response
|
38 |
|
39 |
+
def comparar_semanticamente(self, resp1, resp2):
|
40 |
+
emb1 = self.embed_model.encode(resp1, convert_to_tensor=True)
|
41 |
+
emb2 = self.embed_model.encode(resp2, convert_to_tensor=True)
|
42 |
+
similarity = util.cos_sim(emb1, emb2).item()
|
43 |
+
return similarity
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
multiqa = MultiModelQA(qa_pipeline, chat_client, embed_model)
|
|
|
47 |
|
48 |
|
49 |
+
def responder_e_comparar(question, context, system_message, max_tokens, temperature, top_p):
|
50 |
+
qa_resp = multiqa.answer_with_qa_model(question, context)
|
51 |
+
chat_resp = multiqa.answer_with_chat_model(question, system_message, max_tokens, temperature, top_p)
|
52 |
+
similaridade = multiqa.comparar_semanticamente(qa_resp, chat_resp)
|
53 |
+
|
54 |
+
result = f"""### Resposta do modelo QA:
|
55 |
+
{qa_resp}
|
56 |
+
|
57 |
+
### Resposta do modelo Chat:
|
58 |
+
{chat_resp}
|
59 |
+
|
60 |
+
### Similaridade semântica (coseno): {similaridade:.2%}
|
61 |
"""
|
62 |
+
return result
|
63 |
+
|
64 |
+
|
65 |
+
# Interface Gradio
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=responder_e_comparar,
|
68 |
+
inputs=[
|
69 |
+
gr.Textbox(label="Pergunta"),
|
70 |
+
gr.Textbox(label="Contexto"),
|
71 |
+
gr.Textbox(value="Você é um assistente útil.", label="Mensagem do sistema"),
|
72 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Máximo de tokens"),
|
73 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperatura"),
|
74 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
|
|
|
|
75 |
],
|
76 |
+
outputs=gr.Markdown(),
|
77 |
+
title="Comparador de Respostas de Modelos",
|
78 |
+
description="Compara as respostas de um modelo de QA e um modelo de chat (Zephyr-7B) e calcula a similaridade semântica entre elas."
|
79 |
)
|
80 |
|
|
|
81 |
if __name__ == "__main__":
|
82 |
demo.launch()
|