app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ from docx import Document
|
|
9 |
from datetime import datetime
|
10 |
from pydub import AudioSegment
|
11 |
from io import BytesIO
|
|
|
12 |
|
13 |
# Streamlit Page Config
|
14 |
st.set_page_config(page_title="Schlager ContractAi", layout="wide")
|
@@ -18,7 +19,7 @@ hide_huggingface_css = """
|
|
18 |
<style>
|
19 |
header {visibility: hidden;} /* Hides the Hugging Face header */
|
20 |
footer {visibility: hidden;} /* Hides the footer */
|
21 |
-
.stDeployButton {display: none !important;}
|
22 |
.stAppViewBlockContainer {padding-top: 0px !important;} /* Removes extra padding */
|
23 |
iframe {display: none !important;} /* Hides the Hugging Face iframe */
|
24 |
</style>
|
@@ -67,59 +68,15 @@ tab1, tab2, tab3, tab4 = st.tabs(["Contract", "Technical", "Minutes", "Document
|
|
67 |
SUPPORTED_AUDIO_FORMATS = (".mp3", ".wav", ".m4a")
|
68 |
SUPPORTED_TEXT_FORMATS = (".txt", ".docx", ".csv", ".xlsx", ".pdf")
|
69 |
|
70 |
-
def
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
install_dependencies()
|
81 |
-
|
82 |
-
def process_pdf(uploaded_file):
|
83 |
-
temp_pdf_path = "uploaded_document.pdf"
|
84 |
-
with open(temp_pdf_path, "wb") as f:
|
85 |
-
f.write(uploaded_file.read())
|
86 |
-
|
87 |
-
os.system(f'pdftoppm -png "{temp_pdf_path}" img')
|
88 |
-
|
89 |
-
extracted_text = ""
|
90 |
-
for image in os.listdir():
|
91 |
-
if image.startswith('img') and image.endswith('.png'):
|
92 |
-
output_txt = f"ocr_{image}.txt"
|
93 |
-
os.system(f'tesseract "{image}" "{output_txt[:-4]}"')
|
94 |
-
|
95 |
-
with open(output_txt, "r") as txt_file:
|
96 |
-
extracted_text += txt_file.read() + "\n"
|
97 |
-
|
98 |
-
for file in os.listdir():
|
99 |
-
if file.startswith('img') or file.startswith('ocr_img'):
|
100 |
-
os.remove(file)
|
101 |
-
os.remove(temp_pdf_path)
|
102 |
-
|
103 |
-
return extracted_text
|
104 |
-
|
105 |
-
with tab4:
|
106 |
-
st.subheader("Document Preparation")
|
107 |
-
uploaded_file = st.file_uploader("Upload a PDF (Max: 200MB)", type=["pdf"], accept_multiple_files=False)
|
108 |
-
|
109 |
-
if uploaded_file:
|
110 |
-
st.write("Processing the uploaded document...")
|
111 |
-
extracted_text = process_pdf(uploaded_file)
|
112 |
-
|
113 |
-
if extracted_text.strip():
|
114 |
-
st.text_area("Extracted Text", extracted_text, height=300)
|
115 |
-
st.download_button(
|
116 |
-
label="Download Extracted Text",
|
117 |
-
data=extracted_text,
|
118 |
-
file_name="extracted_text.txt",
|
119 |
-
mime="text/plain"
|
120 |
-
)
|
121 |
-
else:
|
122 |
-
st.error("No text could be extracted. Try another document.")
|
123 |
|
124 |
# Contract Chat Section
|
125 |
def contract_chat_section(tab, assistant_id, session_key, input_key):
|
@@ -137,7 +94,15 @@ def contract_chat_section(tab, assistant_id, session_key, input_key):
|
|
137 |
|
138 |
for message in st.session_state[session_key]:
|
139 |
role, content = message["role"], message["content"]
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
if prompt := st.chat_input("Enter your message:", key=input_key):
|
143 |
st.session_state[session_key].append({"role": "user", "content": prompt})
|
|
|
9 |
from datetime import datetime
|
10 |
from pydub import AudioSegment
|
11 |
from io import BytesIO
|
12 |
+
import json
|
13 |
|
14 |
# Streamlit Page Config
|
15 |
st.set_page_config(page_title="Schlager ContractAi", layout="wide")
|
|
|
19 |
<style>
|
20 |
header {visibility: hidden;} /* Hides the Hugging Face header */
|
21 |
footer {visibility: hidden;} /* Hides the footer */
|
22 |
+
.stDeployButton {display: none !important;} /
|
23 |
.stAppViewBlockContainer {padding-top: 0px !important;} /* Removes extra padding */
|
24 |
iframe {display: none !important;} /* Hides the Hugging Face iframe */
|
25 |
</style>
|
|
|
68 |
SUPPORTED_AUDIO_FORMATS = (".mp3", ".wav", ".m4a")
|
69 |
SUPPORTED_TEXT_FORMATS = (".txt", ".docx", ".csv", ".xlsx", ".pdf")
|
70 |
|
71 |
+
def save_flagged_response(user_query, ai_response):
|
72 |
+
flagged_data = {
|
73 |
+
"query": user_query,
|
74 |
+
"response": ai_response,
|
75 |
+
"timestamp": datetime.now().isoformat()
|
76 |
+
}
|
77 |
+
with open("flagged_responses.json", "a") as file:
|
78 |
+
file.write(json.dumps(flagged_data) + "\n")
|
79 |
+
st.success("Response flagged for review.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Contract Chat Section
|
82 |
def contract_chat_section(tab, assistant_id, session_key, input_key):
|
|
|
94 |
|
95 |
for message in st.session_state[session_key]:
|
96 |
role, content = message["role"], message["content"]
|
97 |
+
if role == "assistant":
|
98 |
+
col1, col2 = st.columns([5, 1])
|
99 |
+
with col1:
|
100 |
+
st.chat_message(role).write(content)
|
101 |
+
with col2:
|
102 |
+
if st.button("🚩 Flag", key=f"flag_{session_key}_{len(st.session_state[session_key])}"):
|
103 |
+
save_flagged_response(st.session_state[session_key][-2]["content"], content)
|
104 |
+
else:
|
105 |
+
st.chat_message(role).write(content)
|
106 |
|
107 |
if prompt := st.chat_input("Enter your message:", key=input_key):
|
108 |
st.session_state[session_key].append({"role": "user", "content": prompt})
|