File size: 4,092 Bytes
52b3b00
27c9a2b
09dc132
03f21b1
c464662
b8fa165
 
 
52b3b00
 
 
 
 
 
 
 
2ff26c6
 
 
 
 
 
 
52b3b00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ff26c6
 
 
 
52b3b00
 
 
 
 
 
03f21b1
52b3b00
 
03f21b1
 
 
2ff26c6
03f21b1
 
 
 
 
2ff26c6
03f21b1
 
 
 
 
 
 
 
 
 
 
 
2ff26c6
 
 
 
 
 
 
 
 
 
 
03f21b1
2ff26c6
 
 
 
 
 
03f21b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52b3b00
 
 
 
 
 
 
 
b8fa165
 
 
2ff26c6
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# app.py
import streamlit as st
from main import ask_agent_sync
from pdf_generator import pdf_receipt_generator

st.set_page_config(page_title="Intelligent Booking Agent", page_icon=":robot:")
st.title("Velora AI Agent")
st.write("Chat with Velora about Bookings and Receipts")

# Initialize chat session
if "messages" not in st.session_state:
    st.session_state.messages = []

if "history" not in st.session_state:
    st.session_state.history = None

if "tool_data" not in st.session_state:
    st.session_state.tool_data = []

if "pdfs" not in st.session_state:
    st.session_state.pdfs = []


# Display chat history
for msg in st.session_state.messages:
    role = msg["role"]
    content = msg["content"]
    with st.chat_message(role):
        st.markdown(content)

# User input
if prompt := st.chat_input("Type your message..."):
    # Append user message
    st.session_state.messages.append({"role": "user", "content": prompt})

    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        message_placeholder.markdown("Typing...")

    st.session_state.tool_data = []
    st.session_state.pdfs = []


    # Call agent
    response = ask_agent_sync(prompt, st.session_state.history)
    st.session_state.history = response["history"]

    # Append agent response
    st.session_state.messages.append({"role": "assistant", "content": response["output"]})

    # Replace placeholder with actual response
    message_placeholder.markdown(response["output"])
    # message_placeholder.markdown(response)
    # response

    # st.session_state.tool_data = []
    for entry in response["history"]:
        if entry.__class__.__name__ == "ModelRequest":
            for parts in entry.parts:
                if parts.__class__.__name__ == "ToolReturnPart":
                    if parts.tool_name == "fetch_records":
                        st.session_state.tool_data.extend(parts.content)
                        # print(parts.content)



    # 
    # for entry in response.get("history", []):
    #     model_response = getattr(entry, "model_response", None)
    #     if model_response:
    #         for part in getattr(model_response, "parts", []):
    #             if part.__class__.__name__ == "ToolReturnPart":
    #                 tool_data.extend(part.content)  # This is your Airtable records

    if st.session_state.tool_data and not st.session_state.pdfs:
        for item in st.session_state.tool_data:
            pdf_buffer = pdf_receipt_generator(item)
            st.session_state.pdfs.append(pdf_buffer)  # Only if the tool returned data


if st.session_state.pdfs:
    st.markdown("### πŸ“„ Available Receipts") 
    
    for idx, item in enumerate(st.session_state.pdfs, start=1):
        # pdf_buffer = pdf_receipt_generator(item)
        st.download_button(
            label=f"πŸ“„ Download Receipt {idx}",
            data=item,
            file_name=f"trip_receipt_{idx}.pdf",
            mime="application/pdf",
            key=f"download_{idx}"
            )


    # Test the pdf_generator separately
# if st.button("Test PDF Generation"):
#     test_data = {
#         "trip_id": "12345",
#         "passenger_name": "John Doe",
#         "pickup": "123 Main St",
#         "dropoff": "456 Oak Ave",
#         "fare": "$25.00",
#         "date": "2025-01-01"
#         }
    
#     try:
#         pdf_buffer = pdf_receipt_generator(test_data)
#         st.download_button(
#             label="πŸ“„ Download Test Receipt",
#             data=pdf_buffer,
#             file_name="test_receipt.pdf",
#             mime="application/pdf"
#         )
#     except Exception as e:
#         st.error(f"PDF generation error: {str(e)}")

#    st.experimental_rerun()


# -----------------------
# Footer / credits
# -----------------------
st.markdown("---")
# st.markdown(
#     "Created with :heart: using **Streamlit** and **Airbyte**."
# )



import logging
logging.basicConfig(level=logging.INFO)

logging.info(f"Tool records found: {len(st.session_state.tool_data)}")