Spaces:
Sleeping
Sleeping
| # app.py | |
| import gradio as gr | |
| import pydantic_ai.messages as pai_msg | |
| from agent import agent, Deps | |
| def extract_content(content) -> str: | |
| if isinstance(content, str): | |
| return content | |
| if isinstance(content, list): | |
| return " ".join( | |
| item.get("text", "") if isinstance(item, dict) else str(item) | |
| for item in content | |
| ) | |
| return str(content or "") | |
| def gradio_to_pydantic_history(history: list) -> list: | |
| print(f"[HISTORY] Recebido: {len(history)} itens") | |
| result = [] | |
| for i, item in enumerate(history): | |
| try: | |
| role = item.get("role", "") | |
| content = extract_content(item.get("content", "")) | |
| print(f"[HISTORY] Item {i}: role={role}, content='{content[:60]}'") | |
| if not content: | |
| continue | |
| if role == "user": | |
| result.append(pai_msg.ModelRequest( | |
| parts=[pai_msg.UserPromptPart(content=content)] | |
| )) | |
| elif role == "assistant": | |
| result.append(pai_msg.ModelResponse( | |
| parts=[pai_msg.TextPart(content=content)], | |
| model_name="", | |
| )) | |
| except Exception as e: | |
| print(f"[HISTORY] Item {i} ERRO: {e}") | |
| print(f"[HISTORY] {len(result)} mensagens convertidas") | |
| return result | |
| async def respond(message: str, history: list): | |
| print(f"\n{'='*50}") | |
| print(f"[RESPOND] Mensagem: '{message}' | History: {len(history)} itens") | |
| pydantic_history = gradio_to_pydantic_history(history) | |
| try: | |
| result = await agent.run( | |
| message, | |
| deps=Deps(), | |
| message_history=pydantic_history, | |
| ) | |
| print(f"[AGENT] OK: '{result.output[:80]}'") | |
| yield result.output | |
| except Exception as e: | |
| print(f"[AGENT] ERRO: {type(e).__name__}: {e}") | |
| raise | |
| CUSTOM_CSS = """ | |
| .buy-coffee { | |
| display: flex; | |
| justify-content: center; | |
| margin-top: 8px; | |
| } | |
| .buy-coffee a img { | |
| height: 40px; | |
| border-radius: 8px; | |
| transition: opacity 0.2s; | |
| } | |
| .buy-coffee a img:hover { | |
| opacity: 0.85; | |
| } | |
| """ | |
| with gr.Blocks(css=CUSTOM_CSS) as demo: | |
| gr.ChatInterface( | |
| fn=respond, | |
| title="✈️ Agente de Viagens Brasil", | |
| description="Encontre as melhores passagens aéreas do Brasil. Ex: *'Quero ir de Salvador para São Paulo em abril'*", | |
| examples=[ | |
| "Quero viajar de Salvador para São Paulo em abril. Quais as melhores passagens?", | |
| "Voos de Recife para Florianópolis, ida e volta, 2 adultos, saindo dia 10/05 voltando 17/05", | |
| "Qual o voo mais barato de Fortaleza para Brasília em maio?", | |
| "Quero ir de Vitória da Conquista para o Rio de Janeiro no fim de semana", | |
| ], | |
| ) | |
| with gr.Row(elem_classes="buy-coffee"): | |
| gr.HTML(""" | |
| <a href="https://ko-fi.com/fredcaixeta" target="_blank"> | |
| <img src="https://ko-fi.com/img/githubbutton_sm.svg" alt="Buy Me a Coffee at ko-fi.com" /> | |
| </a> | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch() | |