Spaces:
Runtime error
Runtime error
Added tabs for better organization
Browse files
app.py
CHANGED
|
@@ -10,26 +10,19 @@ st.title("Transaction Summarizer")
|
|
| 10 |
# Input for selecting model
|
| 11 |
model_selection = st.radio("Select model to use:", ["Tinyllama", "Gemma"])
|
| 12 |
|
| 13 |
-
# Input for submitting new transactions
|
| 14 |
-
new_transactions_input = st.text_area("Enter your transactions (comma-separated)", key="input_area")
|
| 15 |
-
submit_button = st.button("Submit New Transactions", type="primary")
|
| 16 |
-
|
| 17 |
-
# File uploader for CSV files
|
| 18 |
-
uploaded_file = st.file_uploader("Upload a CSV file of transactions", type=["csv"])
|
| 19 |
-
|
| 20 |
-
# Session state to keep track of job ID and status
|
| 21 |
-
if "job_id" not in st.session_state:
|
| 22 |
-
st.session_state.job_id = None
|
| 23 |
-
if "job_status" not in st.session_state:
|
| 24 |
-
st.session_state.job_status = None
|
| 25 |
-
|
| 26 |
# Determine URL based on model selection
|
| 27 |
if model_selection == "Gemma":
|
| 28 |
base_url = "https://api.runpod.ai/v2/lld3iiy6fx7hcf/"
|
| 29 |
elif model_selection == "Tinyllama":
|
| 30 |
base_url = "https://api.runpod.ai/v2/0wnm75vx5o77s1/"
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
def process_transactions(transactions):
|
| 34 |
url = base_url + "runsync"
|
| 35 |
|
|
@@ -81,64 +74,85 @@ def process_transactions(transactions):
|
|
| 81 |
st.error(f"An error occurred: {e}")
|
| 82 |
return None
|
| 83 |
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
result_data = process_transactions(new_transactions)
|
| 89 |
-
if result_data:
|
| 90 |
-
st.write("Transaction Summaries:")
|
| 91 |
-
st.write(result_data)
|
| 92 |
-
|
| 93 |
-
if uploaded_file is not None:
|
| 94 |
-
# Read the uploaded CSV file
|
| 95 |
-
try:
|
| 96 |
-
df = pd.read_csv(uploaded_file)
|
| 97 |
-
st.write("Uploaded Data:")
|
| 98 |
-
st.write(df)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
if result_data:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
df.to_csv(csv_buffer, index=False)
|
| 113 |
-
csv_buffer.seek(0)
|
| 114 |
-
|
| 115 |
-
# Download link for the summarized CSV
|
| 116 |
-
st.download_button(
|
| 117 |
-
label="Download Summarized CSV",
|
| 118 |
-
data=csv_buffer,
|
| 119 |
-
file_name="summarized_transactions.csv",
|
| 120 |
-
mime="text/csv"
|
| 121 |
-
)
|
| 122 |
-
|
| 123 |
-
except Exception as e:
|
| 124 |
-
st.error(f"An error occurred while processing the CSV file: {e}")
|
| 125 |
-
|
| 126 |
-
# Cancel button
|
| 127 |
-
if st.session_state.job_id and st.session_state.job_status == "IN_QUEUE":
|
| 128 |
-
cancel_button = st.button("Cancel Request")
|
| 129 |
-
if cancel_button:
|
| 130 |
-
cancel_url = f"{base_url}cancel/{st.session_state.job_id}"
|
| 131 |
try:
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
st.session_state.job_status = "CANCELLED"
|
| 136 |
-
st.write(f"Job {st.session_state.job_id} has been cancelled.")
|
| 137 |
-
except requests.exceptions.RequestException as e:
|
| 138 |
-
st.error(f"An error occurred while cancelling: {e}")
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Input for selecting model
|
| 11 |
model_selection = st.radio("Select model to use:", ["Tinyllama", "Gemma"])
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Determine URL based on model selection
|
| 14 |
if model_selection == "Gemma":
|
| 15 |
base_url = "https://api.runpod.ai/v2/lld3iiy6fx7hcf/"
|
| 16 |
elif model_selection == "Tinyllama":
|
| 17 |
base_url = "https://api.runpod.ai/v2/0wnm75vx5o77s1/"
|
| 18 |
|
| 19 |
+
# Initialize session state for job ID and status
|
| 20 |
+
if "job_id" not in st.session_state:
|
| 21 |
+
st.session_state.job_id = None
|
| 22 |
+
if "job_status" not in st.session_state:
|
| 23 |
+
st.session_state.job_status = None
|
| 24 |
|
| 25 |
+
# Define the transaction processing function
|
| 26 |
def process_transactions(transactions):
|
| 27 |
url = base_url + "runsync"
|
| 28 |
|
|
|
|
| 74 |
st.error(f"An error occurred: {e}")
|
| 75 |
return None
|
| 76 |
|
| 77 |
+
# Creating tabs for different pages
|
| 78 |
+
tab1, tab2, tab3 = st.tabs(["Submit Transactions", "Upload CSV", "Manage Jobs"])
|
| 79 |
|
| 80 |
+
# Tab 1: Submit Transactions
|
| 81 |
+
with tab1:
|
| 82 |
+
st.header("Submit New Transactions")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Input for submitting new transactions
|
| 85 |
+
new_transactions_input = st.text_area("Enter your transactions (comma-separated)", key="input_area")
|
| 86 |
+
submit_button = st.button("Submit New Transactions", type="primary")
|
| 87 |
|
| 88 |
+
if submit_button and new_transactions_input:
|
| 89 |
+
# Split transactions and strip whitespace
|
| 90 |
+
new_transactions = [i.strip() for i in new_transactions_input.split(',') if i.strip()]
|
| 91 |
+
result_data = process_transactions(new_transactions)
|
| 92 |
if result_data:
|
| 93 |
+
st.write("Transaction Summaries:")
|
| 94 |
+
st.write(result_data)
|
| 95 |
+
|
| 96 |
+
# Tab 2: Upload CSV
|
| 97 |
+
with tab2:
|
| 98 |
+
st.header("Upload a CSV File of Transactions")
|
| 99 |
+
|
| 100 |
+
# File uploader for CSV files
|
| 101 |
+
uploaded_file = st.file_uploader("Upload a CSV file of transactions", type=["csv"])
|
| 102 |
|
| 103 |
+
if uploaded_file is not None:
|
| 104 |
+
# Read the uploaded CSV file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
try:
|
| 106 |
+
df = pd.read_csv(uploaded_file)
|
| 107 |
+
st.write("Uploaded Data:")
|
| 108 |
+
st.write(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
# Process the transactions in the CSV file
|
| 111 |
+
transactions = df['transaction'].tolist()
|
| 112 |
+
result_data = process_transactions(transactions)
|
| 113 |
+
|
| 114 |
+
if result_data:
|
| 115 |
+
# Assuming result_data is a list of summaries matching the input transactions
|
| 116 |
+
df['summary'] = result_data
|
| 117 |
+
st.write("Summarized Data:")
|
| 118 |
+
st.write(df)
|
| 119 |
+
|
| 120 |
+
# Prepare the summarized data for download
|
| 121 |
+
csv_buffer = io.StringIO()
|
| 122 |
+
df.to_csv(csv_buffer, index=False)
|
| 123 |
+
csv_buffer.seek(0)
|
| 124 |
+
|
| 125 |
+
# Download link for the summarized CSV
|
| 126 |
+
st.download_button(
|
| 127 |
+
label="Download Summarized CSV",
|
| 128 |
+
data=csv_buffer,
|
| 129 |
+
file_name="summarized_transactions.csv",
|
| 130 |
+
mime="text/csv"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
except Exception as e:
|
| 134 |
+
st.error(f"An error occurred while processing the CSV file: {e}")
|
| 135 |
+
|
| 136 |
+
# Tab 3: Manage Jobs
|
| 137 |
+
with tab3:
|
| 138 |
+
st.header("Manage Jobs")
|
| 139 |
+
|
| 140 |
+
# Cancel button
|
| 141 |
+
if st.session_state.job_id and st.session_state.job_status == "IN_QUEUE":
|
| 142 |
+
cancel_button = st.button("Cancel Request")
|
| 143 |
+
if cancel_button:
|
| 144 |
+
cancel_url = f"{base_url}cancel/{st.session_state.job_id}"
|
| 145 |
+
try:
|
| 146 |
+
cancel_response = requests.post(cancel_url, headers=headers)
|
| 147 |
+
cancel_response.raise_for_status()
|
| 148 |
+
cancel_result = cancel_response.json()
|
| 149 |
+
st.session_state.job_status = "CANCELLED"
|
| 150 |
+
st.write(f"Job {st.session_state.job_id} has been cancelled.")
|
| 151 |
+
except requests.exceptions.RequestException as e:
|
| 152 |
+
st.error(f"An error occurred while cancelling: {e}")
|
| 153 |
+
|
| 154 |
+
# Reset button
|
| 155 |
+
if st.button("Reset", key="reset_button"):
|
| 156 |
+
st.session_state.job_id = None
|
| 157 |
+
st.session_state.job_status = None
|
| 158 |
+
st.text_area("Enter your transactions (comma-separated)", value="", key="reset_area")
|