kawre commited on
Commit
76c2898
·
verified ·
1 Parent(s): 55f556b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -25
app.py CHANGED
@@ -21,9 +21,7 @@ from typing import List, Dict, Any, Tuple
21
  import gradio as gr
22
  from huggingface_hub import InferenceClient
23
 
24
- # -------------------------
25
- # Config / Logging
26
- # -------------------------
27
  logging.basicConfig(level=logging.INFO)
28
  logger = logging.getLogger("cascade_chatbot")
29
 
@@ -38,13 +36,13 @@ if not HF_TOKEN:
38
  # -------------------------
39
  # Inicializa clientes HF
40
  # -------------------------
41
- # Criamos clientes distintos por modelo para garantir independência de configuração
42
  try:
43
  client_main = InferenceClient(token=HF_TOKEN, model=DEFAULT_LLAMA_MODEL)
44
  client_aux1 = InferenceClient(token=HF_TOKEN, model=DEFAULT_AUX1)
45
  client_aux2 = InferenceClient(token=HF_TOKEN, model=DEFAULT_AUX2)
46
  except Exception:
47
- # falha na inicialização do client (token inválido, etc)
48
  logger.exception("Falha ao inicializar InferenceClient(s). Verifique HF_TOKEN e nomes dos modelos.")
49
  # Criar objetos None para evitar crash imediato; erros aparecerão ao tentar usar
50
  client_main = None
@@ -66,7 +64,7 @@ def _messages_to_prompt(messages: List[Dict[str, str]]) -> str:
66
  def _extract_text_from_response(obj: Any) -> str:
67
  if obj is None:
68
  return ""
69
- # Common attributes
70
  for attr in ("content", "text", "generated_text", "generation_text"):
71
  if hasattr(obj, attr):
72
  try:
@@ -76,7 +74,7 @@ def _extract_text_from_response(obj: Any) -> str:
76
  return str(v)
77
  except Exception:
78
  pass
79
- # choices style
80
  try:
81
  choices = None
82
  if hasattr(obj, "choices"):
@@ -100,7 +98,7 @@ def _extract_text_from_response(obj: Any) -> str:
100
  return first.text
101
  except Exception:
102
  pass
103
- # generations
104
  try:
105
  if hasattr(obj, "generations") and len(obj.generations) > 0:
106
  g = obj.generations[0]
@@ -110,7 +108,7 @@ def _extract_text_from_response(obj: Any) -> str:
110
  return g.text
111
  except Exception:
112
  pass
113
- # dict fallback
114
  try:
115
  if isinstance(obj, dict):
116
  for k in ("text", "content", "generated_text"):
@@ -118,7 +116,7 @@ def _extract_text_from_response(obj: Any) -> str:
118
  return obj[k]
119
  except Exception:
120
  pass
121
- # last resort
122
  try:
123
  return str(obj)
124
  except Exception:
@@ -136,7 +134,7 @@ def call_model_with_messages(client: InferenceClient, messages: List[Dict[str, s
136
 
137
  def try_call(method, /, *pos_args, **kw_args):
138
  try:
139
- # Não imprimir todo messages no log (pode ser grande) — resumir
140
  safe_kw = {k: ("[MESSAGES]" if k == "messages" else v) for k, v in kw_args.items()}
141
  logger.info("Tentando %s pos=%s kwargs=%s", getattr(method, "__name__", str(method)), pos_args, safe_kw)
142
  return method(*pos_args, **kw_args)
@@ -144,10 +142,10 @@ def call_model_with_messages(client: InferenceClient, messages: List[Dict[str, s
144
  logger.exception("Falha ao chamar %s", getattr(method, "__name__", str(method)))
145
  return None
146
 
147
- # Tentar obter nome do modelo (fallback)
148
  model_name = getattr(client, "model", None) or DEFAULT_LLAMA_MODEL
149
 
150
- # 1) chat_completion (método mais comum)
151
  try:
152
  cc = getattr(client, "chat_completion", None)
153
  if cc:
@@ -189,7 +187,7 @@ def call_model_with_messages(client: InferenceClient, messages: List[Dict[str, s
189
  except Exception:
190
  logger.exception("Erro no bloco chat namespace")
191
 
192
- # 3) text_generation (fallback)
193
  prompt = _messages_to_prompt(messages)
194
  try:
195
  if hasattr(client, "text_generation"):
@@ -288,13 +286,13 @@ def pipeline_cascade(user_message: str, system_message: str,
288
  # Gradio App
289
  # -------------------------
290
  with gr.Blocks(title="Chatbot em Cascata - Llama + FLAN + BART") as demo:
291
- gr.Markdown("## 🤖 Chatbot em Cascata\n"
292
- "Fluxo: **Llama (entrada)** → **FLAN-T5 (reformulação)** → **BART (resumo em 3 frases)**\n\n"
293
- "Antes de rodar, confirme que `HF_TOKEN` está definido nos Secrets do Space.")
294
 
295
  with gr.Row():
296
  with gr.Column(scale=2):
297
- system_message = gr.Textbox(value="Você é um chatbot amigável e prestativo.",
298
  label="System Message", lines=2)
299
  chatbot = gr.Chatbot(label="Chat")
300
  user_input = gr.Textbox(label="Digite sua mensagem", placeholder="Digite aqui...")
@@ -323,11 +321,11 @@ with gr.Blocks(title="Chatbot em Cascata - Llama + FLAN + BART") as demo:
323
  outputs=[chatbot, history])
324
 
325
  with gr.Column(scale=1):
326
- gr.Markdown("### Model Info & Config (dentro do app)\n"
327
- "Este painel documenta os modelos usados e as configurações (exigência do trabalho).")
328
 
329
  model_info_md = f"""
330
- **Modelos usados (mínimo 3):**
331
 
332
  - Llama (input): `{DEFAULT_LLAMA_MODEL}`
333
  - Aux 1 (reformulação): `{DEFAULT_AUX1}`
@@ -365,10 +363,9 @@ with gr.Blocks(title="Chatbot em Cascata - Llama + FLAN + BART") as demo:
365
  btn_test = gr.Button("Run self-test")
366
  btn_test.click(run_self_test, inputs=[system_message, max_tokens, temperature, top_p], outputs=[test_output])
367
 
368
- gr.Markdown("### Dicas de deploy\n"
369
- "- Defina `HF_TOKEN` nos Secrets do Space.\n"
370
- "- Use um runtime com GPU se disponível (modelos grandes exigem mais recursos).\n"
371
- "- Verifique permissões do modelo (alguns modelos exigem permissões específicas).")
372
 
373
  if __name__ == "__main__":
374
  demo.launch()
 
21
  import gradio as gr
22
  from huggingface_hub import InferenceClient
23
 
24
+
 
 
25
  logging.basicConfig(level=logging.INFO)
26
  logger = logging.getLogger("cascade_chatbot")
27
 
 
36
  # -------------------------
37
  # Inicializa clientes HF
38
  # -------------------------
39
+
40
  try:
41
  client_main = InferenceClient(token=HF_TOKEN, model=DEFAULT_LLAMA_MODEL)
42
  client_aux1 = InferenceClient(token=HF_TOKEN, model=DEFAULT_AUX1)
43
  client_aux2 = InferenceClient(token=HF_TOKEN, model=DEFAULT_AUX2)
44
  except Exception:
45
+
46
  logger.exception("Falha ao inicializar InferenceClient(s). Verifique HF_TOKEN e nomes dos modelos.")
47
  # Criar objetos None para evitar crash imediato; erros aparecerão ao tentar usar
48
  client_main = None
 
64
  def _extract_text_from_response(obj: Any) -> str:
65
  if obj is None:
66
  return ""
67
+ # Common atributos
68
  for attr in ("content", "text", "generated_text", "generation_text"):
69
  if hasattr(obj, attr):
70
  try:
 
74
  return str(v)
75
  except Exception:
76
  pass
77
+
78
  try:
79
  choices = None
80
  if hasattr(obj, "choices"):
 
98
  return first.text
99
  except Exception:
100
  pass
101
+
102
  try:
103
  if hasattr(obj, "generations") and len(obj.generations) > 0:
104
  g = obj.generations[0]
 
108
  return g.text
109
  except Exception:
110
  pass
111
+
112
  try:
113
  if isinstance(obj, dict):
114
  for k in ("text", "content", "generated_text"):
 
116
  return obj[k]
117
  except Exception:
118
  pass
119
+
120
  try:
121
  return str(obj)
122
  except Exception:
 
134
 
135
  def try_call(method, /, *pos_args, **kw_args):
136
  try:
137
+ # Não imprimir todo messages no log — resumir
138
  safe_kw = {k: ("[MESSAGES]" if k == "messages" else v) for k, v in kw_args.items()}
139
  logger.info("Tentando %s pos=%s kwargs=%s", getattr(method, "__name__", str(method)), pos_args, safe_kw)
140
  return method(*pos_args, **kw_args)
 
142
  logger.exception("Falha ao chamar %s", getattr(method, "__name__", str(method)))
143
  return None
144
 
145
+ # Tentar obter nome do modelo
146
  model_name = getattr(client, "model", None) or DEFAULT_LLAMA_MODEL
147
 
148
+ # 1) chat_completion
149
  try:
150
  cc = getattr(client, "chat_completion", None)
151
  if cc:
 
187
  except Exception:
188
  logger.exception("Erro no bloco chat namespace")
189
 
190
+ # 3) text_generation
191
  prompt = _messages_to_prompt(messages)
192
  try:
193
  if hasattr(client, "text_generation"):
 
286
  # Gradio App
287
  # -------------------------
288
  with gr.Blocks(title="Chatbot em Cascata - Llama + FLAN + BART") as demo:
289
+ gr.Markdown("## Chatbot em Cascata\n"
290
+ "Fluxo: **Llama (entrada)** → **FLAN-T5 (reformulação)** → **BART**\n\n"
291
+ "Disciplina: INTELIGÊNCIA ARTIFICIAL E APRENDIZADO DE MÁQUINA")
292
 
293
  with gr.Row():
294
  with gr.Column(scale=2):
295
+ system_message = gr.Textbox(value="Você é um chatbot racional e alegre.",
296
  label="System Message", lines=2)
297
  chatbot = gr.Chatbot(label="Chat")
298
  user_input = gr.Textbox(label="Digite sua mensagem", placeholder="Digite aqui...")
 
321
  outputs=[chatbot, history])
322
 
323
  with gr.Column(scale=1):
324
+ gr.Markdown("### Inforações do Projeto\n"
325
+ "Painel feito para descrever as configurações e realizar um teste automático")
326
 
327
  model_info_md = f"""
328
+ **Modelos usados:**
329
 
330
  - Llama (input): `{DEFAULT_LLAMA_MODEL}`
331
  - Aux 1 (reformulação): `{DEFAULT_AUX1}`
 
363
  btn_test = gr.Button("Run self-test")
364
  btn_test.click(run_self_test, inputs=[system_message, max_tokens, temperature, top_p], outputs=[test_output])
365
 
366
+ gr.Markdown("### Disciplina: INTELIGÊNCIA ARTIFICIAL E APRENDIZADO DE MÁQUINA\n"
367
+ "- Trabalho N2\n"
368
+ "- Turma Noturna de Bacharelado em Ciências da Computação 2025.\n")
 
369
 
370
  if __name__ == "__main__":
371
  demo.launch()