Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Set Streamlit configuration for wide mode and dark theme
|
| 6 |
+
st.set_page_config(layout="wide", page_title="2-2")
|
| 7 |
+
|
| 8 |
+
# Modified function to send file to the API and get the response, now includes system_message parameter
|
| 9 |
+
def upload_file(file):
|
| 10 |
+
if file is not None:
|
| 11 |
+
url = "https://insly.ai/api/docusmart?locale=en-US"
|
| 12 |
+
headers = {
|
| 13 |
+
"accept": "multipart/form-data",
|
| 14 |
+
"X-API-Key": "ai",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
data = {
|
| 18 |
+
'system_message': '"""Respond in ALLCAPS"""',
|
| 19 |
+
}
|
| 20 |
+
files = {
|
| 21 |
+
"file": (file.name, file, file.type)
|
| 22 |
+
}
|
| 23 |
+
response = requests.post(url, headers=headers, files=files, data=data)
|
| 24 |
+
return response.json()
|
| 25 |
+
return None
|
| 26 |
+
|
| 27 |
+
# Function to display response and its subelements including key value pairs
|
| 28 |
+
def display_response_and_table(response_json):
|
| 29 |
+
if response_json:
|
| 30 |
+
# Displaying the full JSON response in a collapsed textbox
|
| 31 |
+
with st.expander("Full JSON Response", expanded=False):
|
| 32 |
+
st.json(response_json)
|
| 33 |
+
|
| 34 |
+
# Extracting the 'response' subelement
|
| 35 |
+
response_data = response_json.get('response', {})
|
| 36 |
+
|
| 37 |
+
# Checking if there are key value pairs and merging them with the rest of the response data
|
| 38 |
+
key_value_pairs = response_data.pop('key_value_pairs', {})
|
| 39 |
+
all_response_data = {**response_data, **key_value_pairs}
|
| 40 |
+
|
| 41 |
+
# Converting the merged dictionary to a pandas DataFrame for display
|
| 42 |
+
df = pd.DataFrame(list(all_response_data.items()), columns=['Key', 'Value'])
|
| 43 |
+
|
| 44 |
+
# Displaying the DataFrame as a transposed table in Streamlit
|
| 45 |
+
st.table(df)
|
| 46 |
+
|
| 47 |
+
else:
|
| 48 |
+
st.write("No response to display.")
|
| 49 |
+
|
| 50 |
+
# Create two columns for file uploads
|
| 51 |
+
col1, col2 = st.columns(2)
|
| 52 |
+
|
| 53 |
+
with col1:
|
| 54 |
+
file1 = st.file_uploader("Upload file to Column 1", key="file1", label_visibility="collapsed")
|
| 55 |
+
if file1:
|
| 56 |
+
json_response1 = upload_file(file1)
|
| 57 |
+
if json_response1:
|
| 58 |
+
display_response_and_table(json_response1)
|
| 59 |
+
|
| 60 |
+
with col2:
|
| 61 |
+
file2 = st.file_uploader("Upload file to Column 2", key="file2", label_visibility="collapsed")
|
| 62 |
+
if file2:
|
| 63 |
+
json_response2 = upload_file(file2)
|
| 64 |
+
if json_response2:
|
| 65 |
+
display_response_and_table(json_response2)
|