Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,8 @@
|
|
1 |
import streamlit as st
|
2 |
-
import numpy as np
|
3 |
-
import tempfile
|
4 |
-
import soundfile as sf
|
5 |
import os
|
6 |
import time
|
7 |
import re
|
8 |
from openai import OpenAI
|
9 |
-
from streamlit_audio_recorder import audio_recorder
|
10 |
|
11 |
# ------------------ App Configuration ------------------
|
12 |
st.set_page_config(page_title="Document AI Assistant", layout="wide")
|
@@ -19,7 +15,7 @@ ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
|
|
19 |
|
20 |
# ------------------ Error Handling for Missing Secrets ------------------
|
21 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
22 |
-
st.error("
|
23 |
st.stop()
|
24 |
|
25 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
@@ -33,8 +29,6 @@ if "image_url" not in st.session_state:
|
|
33 |
st.session_state.image_url = None
|
34 |
if "image_updated" not in st.session_state:
|
35 |
st.session_state.image_updated = False
|
36 |
-
if "transcript_preview" not in st.session_state:
|
37 |
-
st.session_state.transcript_preview = None
|
38 |
|
39 |
# ------------------ Sidebar Controls ------------------
|
40 |
st.sidebar.header("π§ Settings")
|
@@ -43,87 +37,62 @@ if st.sidebar.button("π Clear Chat"):
|
|
43 |
st.session_state.thread_id = None
|
44 |
st.session_state.image_url = None
|
45 |
st.session_state.image_updated = False
|
46 |
-
st.session_state.transcript_preview = None
|
47 |
st.rerun()
|
48 |
|
49 |
show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
50 |
-
preview_toggle = st.sidebar.checkbox("π Preview transcription before sending", value=True)
|
51 |
|
52 |
# ------------------ Split Layout ------------------
|
53 |
-
col1, col2 = st.columns([1, 2])
|
54 |
|
55 |
# ------------------ Image Panel (Left) ------------------
|
56 |
with col1:
|
57 |
if show_image and st.session_state.image_url:
|
58 |
st.image(st.session_state.image_url, caption="π Extracted Page", use_container_width=True)
|
59 |
-
st.session_state.image_updated = False
|
60 |
-
|
61 |
-
# ------------------ Voice Input Processing ------------------
|
62 |
-
with col2:
|
63 |
-
st.markdown("### ποΈ Voice Input (Optional)")
|
64 |
-
audio_bytes = audio_recorder(pause_threshold=2.0)
|
65 |
-
|
66 |
-
if audio_bytes:
|
67 |
-
st.info("Transcribing your voice...")
|
68 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
69 |
-
tmp.write(audio_bytes)
|
70 |
-
tmp_path = tmp.name
|
71 |
-
|
72 |
-
with open(tmp_path, "rb") as audio_file:
|
73 |
-
try:
|
74 |
-
whisper_result = client.audio.transcriptions.create(
|
75 |
-
model="whisper-1",
|
76 |
-
file=audio_file,
|
77 |
-
response_format="json"
|
78 |
-
)
|
79 |
-
transcript = whisper_result.text.strip()
|
80 |
-
confidence = whisper_result.get("confidence", "N/A")
|
81 |
-
|
82 |
-
if transcript:
|
83 |
-
st.success(f"Recognized: {transcript}")
|
84 |
-
st.caption(f"π§ Confidence: {confidence}")
|
85 |
-
if preview_toggle:
|
86 |
-
st.session_state.transcript_preview = transcript
|
87 |
-
else:
|
88 |
-
st.session_state.messages.append({"role": "user", "content": transcript})
|
89 |
-
st.rerun()
|
90 |
-
except Exception as e:
|
91 |
-
st.error(f"β Transcription failed: {str(e)}")
|
92 |
-
|
93 |
-
if st.session_state.transcript_preview:
|
94 |
-
st.markdown("---")
|
95 |
-
st.markdown("### π Transcription Preview")
|
96 |
-
st.markdown(f"> {st.session_state.transcript_preview}")
|
97 |
-
if st.button("β
Send to Assistant"):
|
98 |
-
st.session_state.messages.append({"role": "user", "content": st.session_state.transcript_preview})
|
99 |
-
st.session_state.transcript_preview = None
|
100 |
-
st.rerun()
|
101 |
-
if st.button("β Discard"):
|
102 |
-
st.session_state.transcript_preview = None
|
103 |
-
st.rerun()
|
104 |
|
105 |
# ------------------ Chat Panel (Right) ------------------
|
106 |
with col2:
|
|
|
|
|
|
|
|
|
107 |
if prompt := st.chat_input("Type your question about the document..."):
|
108 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
109 |
st.chat_message("user").write(prompt)
|
110 |
|
111 |
try:
|
|
|
112 |
if st.session_state.thread_id is None:
|
113 |
thread = client.beta.threads.create()
|
114 |
st.session_state.thread_id = thread.id
|
115 |
|
116 |
thread_id = st.session_state.thread_id
|
117 |
-
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=prompt)
|
118 |
-
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=ASSISTANT_ID)
|
119 |
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
while True:
|
122 |
-
run_status = client.beta.threads.runs.retrieve(
|
|
|
|
|
|
|
123 |
if run_status.status == "completed":
|
124 |
break
|
125 |
time.sleep(1)
|
126 |
|
|
|
127 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
128 |
assistant_message = None
|
129 |
for message in reversed(messages.data):
|
@@ -134,6 +103,7 @@ with col2:
|
|
134 |
st.chat_message("assistant").write(assistant_message)
|
135 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
136 |
|
|
|
137 |
image_match = re.search(
|
138 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
139 |
assistant_message
|
@@ -141,11 +111,7 @@ with col2:
|
|
141 |
if image_match:
|
142 |
st.session_state.image_url = image_match.group(0)
|
143 |
st.session_state.image_updated = True
|
144 |
-
st.rerun()
|
145 |
|
146 |
except Exception as e:
|
147 |
st.error(f"β Error: {str(e)}")
|
148 |
-
|
149 |
-
for message in reversed(st.session_state.messages):
|
150 |
-
role, content = message["role"], message["content"]
|
151 |
-
st.chat_message(role).write(content)
|
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
import os
|
3 |
import time
|
4 |
import re
|
5 |
from openai import OpenAI
|
|
|
6 |
|
7 |
# ------------------ App Configuration ------------------
|
8 |
st.set_page_config(page_title="Document AI Assistant", layout="wide")
|
|
|
15 |
|
16 |
# ------------------ Error Handling for Missing Secrets ------------------
|
17 |
if not OPENAI_API_KEY or not ASSISTANT_ID:
|
18 |
+
st.error("Missing secrets. Please ensure both OPENAI_API_KEY and ASSISTANT_ID are set in your Hugging Face Space secrets.")
|
19 |
st.stop()
|
20 |
|
21 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
|
29 |
st.session_state.image_url = None
|
30 |
if "image_updated" not in st.session_state:
|
31 |
st.session_state.image_updated = False
|
|
|
|
|
32 |
|
33 |
# ------------------ Sidebar Controls ------------------
|
34 |
st.sidebar.header("π§ Settings")
|
|
|
37 |
st.session_state.thread_id = None
|
38 |
st.session_state.image_url = None
|
39 |
st.session_state.image_updated = False
|
|
|
40 |
st.rerun()
|
41 |
|
42 |
show_image = st.sidebar.checkbox("π Show Document Image", value=True)
|
|
|
43 |
|
44 |
# ------------------ Split Layout ------------------
|
45 |
+
col1, col2 = st.columns([1, 2]) # Adjust ratio as needed
|
46 |
|
47 |
# ------------------ Image Panel (Left) ------------------
|
48 |
with col1:
|
49 |
if show_image and st.session_state.image_url:
|
50 |
st.image(st.session_state.image_url, caption="π Extracted Page", use_container_width=True)
|
51 |
+
st.session_state.image_updated = False # Reset flag after rendering
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# ------------------ Chat Panel (Right) ------------------
|
54 |
with col2:
|
55 |
+
for message in st.session_state.messages:
|
56 |
+
role, content = message["role"], message["content"]
|
57 |
+
st.chat_message(role).write(content)
|
58 |
+
|
59 |
if prompt := st.chat_input("Type your question about the document..."):
|
60 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
61 |
st.chat_message("user").write(prompt)
|
62 |
|
63 |
try:
|
64 |
+
# Initialize thread if needed
|
65 |
if st.session_state.thread_id is None:
|
66 |
thread = client.beta.threads.create()
|
67 |
st.session_state.thread_id = thread.id
|
68 |
|
69 |
thread_id = st.session_state.thread_id
|
|
|
|
|
70 |
|
71 |
+
# Send message to assistant
|
72 |
+
client.beta.threads.messages.create(
|
73 |
+
thread_id=thread_id,
|
74 |
+
role="user",
|
75 |
+
content=prompt
|
76 |
+
)
|
77 |
+
|
78 |
+
# Run assistant
|
79 |
+
run = client.beta.threads.runs.create(
|
80 |
+
thread_id=thread_id,
|
81 |
+
assistant_id=ASSISTANT_ID
|
82 |
+
)
|
83 |
+
|
84 |
+
# Wait for assistant response
|
85 |
+
with st.spinner("Assistant is thinking..."):
|
86 |
while True:
|
87 |
+
run_status = client.beta.threads.runs.retrieve(
|
88 |
+
thread_id=thread_id,
|
89 |
+
run_id=run.id
|
90 |
+
)
|
91 |
if run_status.status == "completed":
|
92 |
break
|
93 |
time.sleep(1)
|
94 |
|
95 |
+
# Get assistant response
|
96 |
messages = client.beta.threads.messages.list(thread_id=thread_id)
|
97 |
assistant_message = None
|
98 |
for message in reversed(messages.data):
|
|
|
103 |
st.chat_message("assistant").write(assistant_message)
|
104 |
st.session_state.messages.append({"role": "assistant", "content": assistant_message})
|
105 |
|
106 |
+
# Extract GitHub image from response if available
|
107 |
image_match = re.search(
|
108 |
r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
|
109 |
assistant_message
|
|
|
111 |
if image_match:
|
112 |
st.session_state.image_url = image_match.group(0)
|
113 |
st.session_state.image_updated = True
|
114 |
+
st.rerun() # Trigger rerun to refresh image display
|
115 |
|
116 |
except Exception as e:
|
117 |
st.error(f"β Error: {str(e)}")
|
|
|
|
|
|
|
|