Spaces:
Sleeping
Sleeping
Logs Space
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import time
|
5 |
+
|
6 |
+
st.title("Log Summarizer")
|
7 |
+
|
8 |
+
|
9 |
+
# Input for submitting new transactions
|
10 |
+
new_transactions_input = st.text_area("Enter your logs (comma-separated)", key="input_area")
|
11 |
+
submit_button = st.button("Submit New Logs", type="primary")
|
12 |
+
|
13 |
+
# Session state to keep track of job ID and status
|
14 |
+
if "job_id" not in st.session_state:
|
15 |
+
st.session_state.job_id = None
|
16 |
+
if "job_status" not in st.session_state:
|
17 |
+
st.session_state.job_status = None
|
18 |
+
|
19 |
+
if submit_button:
|
20 |
+
# Split transactions and strip whitespace
|
21 |
+
new_transactions = [i.strip() for i in new_transactions_input.split(',') if i.strip()]
|
22 |
+
else:
|
23 |
+
new_transactions = []
|
24 |
+
|
25 |
+
# Determine URL based on model selection
|
26 |
+
base_url = "https://api.runpod.ai/v2/v1a28yp6h5vurs/"
|
27 |
+
|
28 |
+
# When submit button is clicked and there are transactions to process
|
29 |
+
if submit_button and new_transactions:
|
30 |
+
url = base_url + "runsync"
|
31 |
+
|
32 |
+
# Retrieve API key from Streamlit secrets
|
33 |
+
api_key = st.secrets["api_key"]
|
34 |
+
|
35 |
+
headers = {
|
36 |
+
'Content-Type': 'application/json',
|
37 |
+
'Authorization': api_key
|
38 |
+
}
|
39 |
+
data = {
|
40 |
+
'input': {
|
41 |
+
'logs': new_transactions
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
json_data = json.dumps(data)
|
46 |
+
|
47 |
+
# Show a spinner while waiting for the response
|
48 |
+
with st.spinner("Processing..."):
|
49 |
+
try:
|
50 |
+
# Send POST request to start processing
|
51 |
+
response = requests.post(url, headers=headers, data=json_data)
|
52 |
+
response.raise_for_status() # Raise an error for bad status codes
|
53 |
+
|
54 |
+
# Parse response to get job ID
|
55 |
+
result = response.json()
|
56 |
+
st.session_state.job_id = result['id']
|
57 |
+
st.write(f"New Job ID: {st.session_state.job_id}")
|
58 |
+
|
59 |
+
# Keep checking status until it's no longer 'IN_QUEUE' or cancelled
|
60 |
+
status_url = f"{base_url}status/{st.session_state.job_id}"
|
61 |
+
st.session_state.job_status = "IN_QUEUE"
|
62 |
+
while st.session_state.job_status != "COMPLETED":
|
63 |
+
status_response = requests.get(status_url, headers=headers)
|
64 |
+
status_data = status_response.json()
|
65 |
+
st.session_state.job_status = status_data.get('status', '')
|
66 |
+
if st.session_state.job_status == "COMPLETED":
|
67 |
+
break
|
68 |
+
time.sleep(2) # Adjust interval as needed
|
69 |
+
|
70 |
+
# Once status changes, display final status
|
71 |
+
st.write("Final status:", status_data)
|
72 |
+
|
73 |
+
except requests.exceptions.RequestException as e:
|
74 |
+
st.error(f"An error occurred: {e}")
|
75 |
+
|
76 |
+
# Cancel button
|
77 |
+
if st.session_state.job_id and st.session_state.job_status == "IN_QUEUE":
|
78 |
+
cancel_button = st.button("Cancel Request")
|
79 |
+
if cancel_button:
|
80 |
+
cancel_url = f"{base_url}cancel/{st.session_state.job_id}"
|
81 |
+
try:
|
82 |
+
cancel_response = requests.post(cancel_url, headers=headers)
|
83 |
+
cancel_response.raise_for_status()
|
84 |
+
cancel_result = cancel_response.json()
|
85 |
+
st.session_state.job_status = "CANCELLED"
|
86 |
+
st.write(f"Job {st.session_state.job_id} has been cancelled.")
|
87 |
+
except requests.exceptions.RequestException as e:
|
88 |
+
st.error(f"An error occurred while cancelling: {e}")
|
89 |
+
|
90 |
+
# Reset button
|
91 |
+
if st.button("Reset", key="reset_button"):
|
92 |
+
st.session_state.job_id = None
|
93 |
+
st.session_state.job_status = None
|
94 |
+
st.text_area("Enter your transactions (comma-separated)", value="", key="reset_area")
|