Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
|
| 5 |
-
#
|
| 6 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 7 |
if not API_KEY:
|
| 8 |
raise ValueError("Defina a variável de ambiente OPENROUTER_API_KEY com sua chave válida.")
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
# Modelos a serem
|
| 14 |
MODEL_1 = "meta-llama/llama-3.2-1b-instruct:free"
|
| 15 |
MODEL_2 = "mistralai/mistral-7b-instruct:free"
|
| 16 |
MODEL_3 = "google/gemma-2-9b-it:free" # LLM julgadora
|
| 17 |
|
| 18 |
def call_model(model_name, messages, max_tokens=512, temperature=0.7, top_p=0.95):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
try:
|
| 20 |
-
response =
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
max_tokens=max_tokens,
|
| 24 |
-
temperature=temperature,
|
| 25 |
-
top_p=top_p
|
| 26 |
-
)
|
| 27 |
-
return response.choices[0].message.content.strip()
|
| 28 |
except Exception as e:
|
| 29 |
-
|
| 30 |
-
error_msg = f"Erro na chamada ao modelo {model_name}: {str(e)}"
|
| 31 |
-
print(error_msg)
|
| 32 |
-
return error_msg
|
| 33 |
|
| 34 |
def generate_and_judge(user_input):
|
| 35 |
if not user_input.strip():
|
| 36 |
-
return "
|
| 37 |
|
| 38 |
system_prompt = "Você é um assistente útil e objetivo."
|
| 39 |
|
|
@@ -42,11 +47,11 @@ def generate_and_judge(user_input):
|
|
| 42 |
{"role": "user", "content": user_input}
|
| 43 |
]
|
| 44 |
|
| 45 |
-
# Gera respostas
|
| 46 |
response1 = call_model(MODEL_1, messages)
|
| 47 |
response2 = call_model(MODEL_2, messages)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
judge_prompt = f"""
|
| 51 |
Você é um avaliador imparcial. Dadas duas respostas para a mesma pergunta, escolha a melhor.
|
| 52 |
|
|
@@ -69,17 +74,18 @@ Resposta:
|
|
| 69 |
|
| 70 |
return response1, response2, judgment
|
| 71 |
|
|
|
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
-
gr.Markdown("
|
| 74 |
-
user_input = gr.Textbox(label="Digite sua pergunta
|
| 75 |
-
generate_button = gr.Button("Gerar
|
| 76 |
|
| 77 |
response1_out = gr.Textbox(label="Resposta da LLM 1", interactive=False)
|
| 78 |
response2_out = gr.Textbox(label="Resposta da LLM 2", interactive=False)
|
| 79 |
judgment_out = gr.Textbox(label="Julgamento da LLM 3", interactive=False)
|
| 80 |
|
| 81 |
generate_button.click(
|
| 82 |
-
generate_and_judge,
|
| 83 |
inputs=user_input,
|
| 84 |
outputs=[response1_out, response2_out, judgment_out]
|
| 85 |
)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Verifique se a variável de ambiente está configurada
|
| 6 |
API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
| 7 |
if not API_KEY:
|
| 8 |
raise ValueError("Defina a variável de ambiente OPENROUTER_API_KEY com sua chave válida.")
|
| 9 |
|
| 10 |
+
# Endpoint correto da OpenRouter
|
| 11 |
+
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 12 |
|
| 13 |
+
# Modelos a serem utilizados
|
| 14 |
MODEL_1 = "meta-llama/llama-3.2-1b-instruct:free"
|
| 15 |
MODEL_2 = "mistralai/mistral-7b-instruct:free"
|
| 16 |
MODEL_3 = "google/gemma-2-9b-it:free" # LLM julgadora
|
| 17 |
|
| 18 |
def call_model(model_name, messages, max_tokens=512, temperature=0.7, top_p=0.95):
|
| 19 |
+
headers = {
|
| 20 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 21 |
+
"Content-Type": "application/json"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
payload = {
|
| 25 |
+
"model": model_name,
|
| 26 |
+
"messages": messages,
|
| 27 |
+
"max_tokens": max_tokens,
|
| 28 |
+
"temperature": temperature,
|
| 29 |
+
"top_p": top_p
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
try:
|
| 33 |
+
response = requests.post(OPENROUTER_URL, headers=headers, json=payload)
|
| 34 |
+
response.raise_for_status()
|
| 35 |
+
return response.json()['choices'][0]['message']['content'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Erro na chamada ao modelo {model_name}: {str(e)}"
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def generate_and_judge(user_input):
|
| 40 |
if not user_input.strip():
|
| 41 |
+
return "Digite algo válido.", "", ""
|
| 42 |
|
| 43 |
system_prompt = "Você é um assistente útil e objetivo."
|
| 44 |
|
|
|
|
| 47 |
{"role": "user", "content": user_input}
|
| 48 |
]
|
| 49 |
|
| 50 |
+
# Gera respostas dos dois primeiros modelos
|
| 51 |
response1 = call_model(MODEL_1, messages)
|
| 52 |
response2 = call_model(MODEL_2, messages)
|
| 53 |
|
| 54 |
+
# Monta o prompt para a LLM julgadora
|
| 55 |
judge_prompt = f"""
|
| 56 |
Você é um avaliador imparcial. Dadas duas respostas para a mesma pergunta, escolha a melhor.
|
| 57 |
|
|
|
|
| 74 |
|
| 75 |
return response1, response2, judgment
|
| 76 |
|
| 77 |
+
# Interface Gradio minimalista
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("### Cascata de 3 LLMs com Julgamento")
|
| 80 |
+
user_input = gr.Textbox(label="Pergunta", placeholder="Digite sua pergunta")
|
| 81 |
+
generate_button = gr.Button("Gerar e Julgar")
|
| 82 |
|
| 83 |
response1_out = gr.Textbox(label="Resposta da LLM 1", interactive=False)
|
| 84 |
response2_out = gr.Textbox(label="Resposta da LLM 2", interactive=False)
|
| 85 |
judgment_out = gr.Textbox(label="Julgamento da LLM 3", interactive=False)
|
| 86 |
|
| 87 |
generate_button.click(
|
| 88 |
+
fn=generate_and_judge,
|
| 89 |
inputs=user_input,
|
| 90 |
outputs=[response1_out, response2_out, judgment_out]
|
| 91 |
)
|