GutoFonseca commited on
Commit
f4313bd
·
verified ·
1 Parent(s): 7f46ab5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -50
app.py CHANGED
@@ -1,64 +1,70 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
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
- response = ""
 
 
 
 
 
 
 
 
 
 
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
- 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="You are a friendly Chatbot.", label="System message"),
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(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
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 transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
+ # Modelos escolhidos
5
+ model_name1 = "google/flan-t5-small"
6
+ model_name2 = "google/flan-t5-base"
7
+ arbitro_model_name = "google/flan-t5-small" # Pode usar o mesmo ou outro
8
 
9
+ # Carregar tokenizadores e modelos
10
+ tokenizer1 = AutoTokenizer.from_pretrained(model_name1)
11
+ model1 = AutoModelForSeq2SeqLM.from_pretrained(model_name1)
12
 
13
+ tokenizer2 = AutoTokenizer.from_pretrained(model_name2)
14
+ model2 = AutoModelForSeq2SeqLM.from_pretrained(model_name2)
 
 
 
 
 
 
 
15
 
16
+ tokenizer_arbitro = AutoTokenizer.from_pretrained(arbitro_model_name)
17
+ model_arbitro = AutoModelForSeq2SeqLM.from_pretrained(arbitro_model_name)
 
 
 
18
 
19
+ def gerar_resposta(model, tokenizer, pergunta):
20
+ # Prompt para focar na resposta correta da capital
21
+ prompt = f"Responda com o nome da capital do país nesta pergunta: {pergunta}"
22
+ inputs = tokenizer(prompt, return_tensors="pt")
23
+ outputs = model.generate(**inputs, max_length=20)
24
+ resposta = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
+ return resposta.strip()
26
 
27
+ def arbitro(pergunta, resp1, resp2):
28
+ # Prompt para o árbitro escolher a melhor resposta
29
+ prompt = (
30
+ f"Pergunta: {pergunta}\n"
31
+ f"Resposta 1: {resp1}\n"
32
+ f"Resposta 2: {resp2}\n"
33
+ "Qual resposta é mais correta? Responda apenas 1 ou 2."
34
+ )
35
+ inputs = tokenizer_arbitro(prompt, return_tensors="pt")
36
+ outputs = model_arbitro.generate(**inputs, max_length=5)
37
+ escolha = tokenizer_arbitro.decode(outputs[0], skip_special_tokens=True).strip()
38
 
39
+ if escolha == "1":
40
+ return resp1, "Modelo 1 (flan-t5-small)"
41
+ elif escolha == "2":
42
+ return resp2, "Modelo 2 (flan-t5-base)"
43
+ else:
44
+ # Caso o árbitro não dê 1 ou 2, escolhe a resposta 1 por padrão
45
+ return resp1, "Modelo 1 (flan-t5-small)"
 
46
 
47
+ def chatbot(pergunta):
48
+ resposta1 = gerar_resposta(model1, tokenizer1, pergunta)
49
+ resposta2 = gerar_resposta(model2, tokenizer2, pergunta)
50
+ resposta_final, modelo_escolhido = arbitro(pergunta, resposta1, resposta2)
51
 
52
+ return (
53
+ f"Resposta selecionada:\n{resposta_final}\n\nModelo escolhido:\n{modelo_escolhido}",
54
+ f"Resposta Modelo 1 (flan-t5-small):\n{resposta1}",
55
+ f"Resposta Modelo 2 (flan-t5-base):\n{resposta2}"
56
+ )
57
 
58
+ iface = gr.Interface(
59
+ fn=chatbot,
60
+ inputs=gr.Textbox(label="Digite uma pergunta sobre capitais"),
61
+ outputs=[
62
+ gr.Textbox(label="Resposta selecionada"),
63
+ gr.Textbox(label="Resposta Modelo 1"),
64
+ gr.Textbox(label="Resposta Modelo 2")
 
 
 
 
 
 
 
 
 
65
  ],
66
+ title="Chatbot em Cascata - Perguntas sobre Capitais"
67
  )
68
 
69
+ if _name_ == "_main_":
70
+ iface.launch()