Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
@@ -15,34 +57,31 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 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=[
|
|
@@ -59,6 +98,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Defina os nomes dos modelos na Hugging Face que quer usar
|
| 5 |
+
MODEL_1 = "HuggingFaceH4/zephyr-7b-beta" # Modelo gerador 1
|
| 6 |
+
MODEL_2 = "facebook/blenderbot-400M-distill" # Modelo gerador 2 (exemplo)
|
| 7 |
+
JUDGE_MODEL = "google/flan-t5-large" # Modelo julgador (exemplo)
|
| 8 |
|
| 9 |
+
client_1 = InferenceClient(MODEL_1)
|
| 10 |
+
client_2 = InferenceClient(MODEL_2)
|
| 11 |
+
client_judge = InferenceClient(JUDGE_MODEL)
|
| 12 |
+
|
| 13 |
+
def call_model(client, messages, max_tokens, temperature, top_p):
|
| 14 |
+
# Chama o modelo de chat_completion e monta resposta completa (não stream)
|
| 15 |
+
response = ""
|
| 16 |
+
for message in client.chat_completion(
|
| 17 |
+
messages,
|
| 18 |
+
max_tokens=max_tokens,
|
| 19 |
+
stream=True,
|
| 20 |
+
temperature=temperature,
|
| 21 |
+
top_p=top_p,
|
| 22 |
+
):
|
| 23 |
+
token = message.choices[0].delta.content
|
| 24 |
+
if token:
|
| 25 |
+
response += token
|
| 26 |
+
return response
|
| 27 |
+
|
| 28 |
+
def judge_response(client, prompt, resp1, resp2):
|
| 29 |
+
# Monta prompt para o julgador comparar respostas
|
| 30 |
+
judge_prompt = (
|
| 31 |
+
f"Usuário perguntou: {prompt}\n"
|
| 32 |
+
f"Resposta 1: {resp1}\n"
|
| 33 |
+
f"Resposta 2: {resp2}\n"
|
| 34 |
+
f"Qual das respostas é melhor? Responda apenas com 1 ou 2."
|
| 35 |
+
)
|
| 36 |
+
inputs = [{"role": "user", "content": judge_prompt}]
|
| 37 |
+
|
| 38 |
+
response = ""
|
| 39 |
+
for message in client.chat_completion(
|
| 40 |
+
inputs,
|
| 41 |
+
max_tokens=20,
|
| 42 |
+
stream=True,
|
| 43 |
+
temperature=0.0, # julgador mais determinístico
|
| 44 |
+
top_p=1.0,
|
| 45 |
+
):
|
| 46 |
+
token = message.choices[0].delta.content
|
| 47 |
+
if token:
|
| 48 |
+
response += token
|
| 49 |
+
# Retorna o número da resposta escolhida, ou "1" como default
|
| 50 |
+
return response.strip()
|
| 51 |
|
| 52 |
def respond(
|
| 53 |
message,
|
|
|
|
| 57 |
temperature,
|
| 58 |
top_p,
|
| 59 |
):
|
| 60 |
+
# Monta o contexto de conversa para os geradores
|
| 61 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 62 |
for val in history:
|
| 63 |
if val[0]:
|
| 64 |
messages.append({"role": "user", "content": val[0]})
|
| 65 |
if val[1]:
|
| 66 |
messages.append({"role": "assistant", "content": val[1]})
|
|
|
|
| 67 |
messages.append({"role": "user", "content": message})
|
| 68 |
|
| 69 |
+
# Chama os dois modelos geradores
|
| 70 |
+
response1 = call_model(client_1, messages, max_tokens, temperature, top_p)
|
| 71 |
+
response2 = call_model(client_2, messages, max_tokens, temperature, top_p)
|
| 72 |
|
| 73 |
+
# Chama o julgador para decidir qual resposta é melhor
|
| 74 |
+
choice = judge_response(client_judge, message, response1, response2)
|
| 75 |
+
|
| 76 |
+
# Escolhe a resposta com base no julgamento
|
| 77 |
+
if choice == "2":
|
| 78 |
+
final_response = response2
|
| 79 |
+
else:
|
| 80 |
+
final_response = response1
|
| 81 |
|
| 82 |
+
yield final_response
|
|
|
|
| 83 |
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
demo = gr.ChatInterface(
|
| 86 |
respond,
|
| 87 |
additional_inputs=[
|
|
|
|
| 98 |
],
|
| 99 |
)
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|