Spaces:
Sleeping
Sleeping
Commit Β·
2ff26c6
1
Parent(s): 03f21b1
pdf download button issues resolved
Browse files- .gitignore +7 -1
- requirements.txt +2 -1
- src/app.py +38 -10
- src/pdf_generator.py +2 -1
.gitignore
CHANGED
|
@@ -11,4 +11,10 @@ wheels/
|
|
| 11 |
# Environment Variables File
|
| 12 |
.env
|
| 13 |
# Python Files
|
| 14 |
-
update_requirements.py
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Environment Variables File
|
| 12 |
.env
|
| 13 |
# Python Files
|
| 14 |
+
update_requirements.py
|
| 15 |
+
pyproject.toml
|
| 16 |
+
uv.lock
|
| 17 |
+
.gitattributes
|
| 18 |
+
README.md
|
| 19 |
+
.python-version
|
| 20 |
+
Docekrfile
|
requirements.txt
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# airbyte-agent-github>=0.18.21
|
| 2 |
pydantic-ai>=1.33.0
|
| 3 |
reportlab==4.4.7
|
| 4 |
-
streamlit==1.
|
|
|
|
| 5 |
nest_asyncio==1.5.8
|
| 6 |
requests
|
| 7 |
|
|
|
|
| 1 |
# airbyte-agent-github>=0.18.21
|
| 2 |
pydantic-ai>=1.33.0
|
| 3 |
reportlab==4.4.7
|
| 4 |
+
streamlit==1.52.2
|
| 5 |
+
# streamlit==1.27.0
|
| 6 |
nest_asyncio==1.5.8
|
| 7 |
requests
|
| 8 |
|
src/app.py
CHANGED
|
@@ -14,6 +14,13 @@ if "messages" not in st.session_state:
|
|
| 14 |
if "history" not in st.session_state:
|
| 15 |
st.session_state.history = None
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Display chat history
|
| 18 |
for msg in st.session_state.messages:
|
| 19 |
role = msg["role"]
|
|
@@ -33,6 +40,10 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 33 |
message_placeholder = st.empty()
|
| 34 |
message_placeholder.markdown("Typing...")
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Call agent
|
| 37 |
response = ask_agent_sync(prompt, st.session_state.history)
|
| 38 |
st.session_state.history = response["history"]
|
|
@@ -45,13 +56,13 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 45 |
# message_placeholder.markdown(response)
|
| 46 |
# response
|
| 47 |
|
| 48 |
-
tool_data = []
|
| 49 |
for entry in response["history"]:
|
| 50 |
if entry.__class__.__name__ == "ModelRequest":
|
| 51 |
for parts in entry.parts:
|
| 52 |
if parts.__class__.__name__ == "ToolReturnPart":
|
| 53 |
if parts.tool_name == "fetch_records":
|
| 54 |
-
tool_data.extend(parts.content)
|
| 55 |
# print(parts.content)
|
| 56 |
|
| 57 |
|
|
@@ -64,14 +75,24 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 64 |
# if part.__class__.__name__ == "ToolReturnPart":
|
| 65 |
# tool_data.extend(part.content) # This is your Airtable records
|
| 66 |
|
| 67 |
-
if tool_data
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
st.download_button(
|
| 70 |
-
label="π Download Receipt",
|
| 71 |
-
data=
|
| 72 |
-
file_name="
|
| 73 |
-
mime="application/pdf"
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
|
| 77 |
# Test the pdf_generator separately
|
|
@@ -105,4 +126,11 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 105 |
st.markdown("---")
|
| 106 |
st.markdown(
|
| 107 |
"Created with :heart: using **Streamlit** and **Airbyte**."
|
| 108 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if "history" not in st.session_state:
|
| 15 |
st.session_state.history = None
|
| 16 |
|
| 17 |
+
if "tool_data" not in st.session_state:
|
| 18 |
+
st.session_state.tool_data = []
|
| 19 |
+
|
| 20 |
+
if "pdfs" not in st.session_state:
|
| 21 |
+
st.session_state.pdfs = []
|
| 22 |
+
|
| 23 |
+
|
| 24 |
# Display chat history
|
| 25 |
for msg in st.session_state.messages:
|
| 26 |
role = msg["role"]
|
|
|
|
| 40 |
message_placeholder = st.empty()
|
| 41 |
message_placeholder.markdown("Typing...")
|
| 42 |
|
| 43 |
+
st.session_state.tool_data = []
|
| 44 |
+
st.session_state.pdfs = []
|
| 45 |
+
|
| 46 |
+
|
| 47 |
# Call agent
|
| 48 |
response = ask_agent_sync(prompt, st.session_state.history)
|
| 49 |
st.session_state.history = response["history"]
|
|
|
|
| 56 |
# message_placeholder.markdown(response)
|
| 57 |
# response
|
| 58 |
|
| 59 |
+
# st.session_state.tool_data = []
|
| 60 |
for entry in response["history"]:
|
| 61 |
if entry.__class__.__name__ == "ModelRequest":
|
| 62 |
for parts in entry.parts:
|
| 63 |
if parts.__class__.__name__ == "ToolReturnPart":
|
| 64 |
if parts.tool_name == "fetch_records":
|
| 65 |
+
st.session_state.tool_data.extend(parts.content)
|
| 66 |
# print(parts.content)
|
| 67 |
|
| 68 |
|
|
|
|
| 75 |
# if part.__class__.__name__ == "ToolReturnPart":
|
| 76 |
# tool_data.extend(part.content) # This is your Airtable records
|
| 77 |
|
| 78 |
+
if st.session_state.tool_data and not st.session_state.pdfs:
|
| 79 |
+
for item in st.session_state.tool_data:
|
| 80 |
+
pdf_buffer = pdf_receipt_generator(item)
|
| 81 |
+
st.session_state.pdfs.append(pdf_buffer) # Only if the tool returned data
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if st.session_state.pdfs:
|
| 85 |
+
st.markdown("### π Available Receipts")
|
| 86 |
+
|
| 87 |
+
for idx, item in enumerate(st.session_state.pdfs, start=1):
|
| 88 |
+
# pdf_buffer = pdf_receipt_generator(item)
|
| 89 |
st.download_button(
|
| 90 |
+
label=f"π Download Receipt {idx}",
|
| 91 |
+
data=item,
|
| 92 |
+
file_name=f"trip_receipt_{idx}.pdf",
|
| 93 |
+
mime="application/pdf",
|
| 94 |
+
key=f"download_{idx}"
|
| 95 |
+
)
|
| 96 |
|
| 97 |
|
| 98 |
# Test the pdf_generator separately
|
|
|
|
| 126 |
st.markdown("---")
|
| 127 |
st.markdown(
|
| 128 |
"Created with :heart: using **Streamlit** and **Airbyte**."
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
import logging
|
| 134 |
+
logging.basicConfig(level=logging.INFO)
|
| 135 |
+
|
| 136 |
+
logging.info(f"Tool records found: {len(st.session_state.tool_data)}")
|
src/pdf_generator.py
CHANGED
|
@@ -4,7 +4,8 @@ from io import BytesIO
|
|
| 4 |
|
| 5 |
|
| 6 |
def pdf_receipt_generator(data_input_api):
|
| 7 |
-
data = data_input_api[0].get("fields", {})
|
|
|
|
| 8 |
# Create PDF
|
| 9 |
# pdf_file = "trip_receipt.pdf"
|
| 10 |
pdf_buffer = BytesIO()
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
def pdf_receipt_generator(data_input_api):
|
| 7 |
+
# data = data_input_api[0].get("fields", {})
|
| 8 |
+
data = data_input_api.get("fields", {})
|
| 9 |
# Create PDF
|
| 10 |
# pdf_file = "trip_receipt.pdf"
|
| 11 |
pdf_buffer = BytesIO()
|