Skip to content

Commit e97720a

Browse files
rehacking app, getting towards full pypi mode 🤩 (#18)
* rehacking app * include images dir * Delete amadeusgpt/static/demo1_amadeusGPT-EPM.mov * keep ROI demo * Update main.py - from #17 * Update test_EPM.py - from #17 * bug fix * cleaned up logging * wip, but merge * revert --------- Co-authored-by: Shaokai Ye <[email protected]>
1 parent 7af0847 commit e97720a

19 files changed

+99
-33
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ __pycache__/
1717
*$py.class
1818

1919
# Binary files
20-
*.png
2120
*.jpg
2221
*.jpeg
2322
*.ipynb

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export streamlit_app=True
22
app:
33

4-
streamlit run app.py --server.fileWatcherType none
4+
streamlit run amadeusgpt/app.py --server.fileWatcherType none

amadeusgpt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
from amadeusgpt.implementation import AnimalBehaviorAnalysis
99
from amadeusgpt.main import AMADEUS
1010

11-
from amadeusgpt.version import __version__, VERSION
11+
from amadeusgpt.version import __version__, VERSION

amadeusgpt/app.py

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import os
2+
import subprocess
3+
24
import streamlit as st
5+
6+
if "session_state" not in st.session_state:
7+
st.session_state.session_id = None
8+
st.session_state.username = None
9+
st.session_state.email = None
10+
311
import traceback
412
from collections import defaultdict
513
import uuid
@@ -9,24 +17,69 @@
917
from amadeusgpt import app_utils
1018
from amadeusgpt.utils import validate_openai_api_key
1119

20+
# Initialize session state variables if not present
1221
st._is_running_with_streamlit = True
1322
os.environ["streamlit_app"] = "True"
1423
assert "streamlit_app" in os.environ, "The 'streamlit_app' environment variable is not set!"
1524

16-
# Initialize session state variables if not present
1725
if "exist_valid_openai_api_key" not in st.session_state:
1826
st.session_state["exist_valid_openai_api_key"] = False
1927

20-
def main():
21-
subprocess.run(["./launch_app"], check=True)
22-
st.title("AmadeusGPT")
28+
# Set page configuration
29+
st.set_page_config(layout="wide")
30+
app_utils.load_css("static/styles/style.css")
2331

32+
def main():
2433
import time
2534
from streamlit_profiler import Profiler
2635

27-
# TITLE PANEL
28-
st.set_page_config(layout="wide")
29-
app_utils.load_css("static/styles/style.css")
36+
def fetch_user_headers():
37+
"""Fetch user and email info from HTTP headers.
38+
39+
Output of this function is identical to querying
40+
https://amadeusgpt.kinematik.ai/oauth2/userinfo, but
41+
works from within the streamlit app.
42+
"""
43+
# TODO(stes): This could change without warning n future streamlit
44+
# versions. So I'll leave the import here in case sth should go
45+
# wrong in the future
46+
from streamlit.web.server.websocket_headers import _get_websocket_headers
47+
48+
headers = _get_websocket_headers()
49+
AmadeusLogger.debug(f"Received Headers: {headers}")
50+
return dict(
51+
email=headers.get("X-Forwarded-Email", "no_email_in_header"),
52+
user=headers.get("X-Forwarded-User", "no_user_in_header"),
53+
)
54+
55+
56+
def fetch_user_info():
57+
url = "https://amadeusgpt.kinematik.ai/oauth2/userinfo"
58+
try:
59+
return fetch_user_headers()
60+
# TODO(stes): Lets be on the safe side for now.
61+
except Exception as e:
62+
AmadeusLogger.info(f"Error: {e}")
63+
return None
64+
65+
66+
if "streamlit_app" in os.environ:
67+
if "session_id" not in st.session_state:
68+
session_id = str(uuid.uuid4())
69+
st.session_state["session_id"] = session_id
70+
user_info = fetch_user_info()
71+
if user_info is not None:
72+
st.session_state["username"] = "no_username"
73+
st.session_state["email"] = "no_email"
74+
else:
75+
AmadeusLogger.info("Getting None from the endpoint")
76+
st.session_state["username"] = "no_username"
77+
st.session_state["email"] = "no_email"
78+
79+
AmadeusLogger.debug("A new user logs in ")
80+
81+
if f"database" not in st.session_state:
82+
st.session_state[f"database"] = defaultdict(dict)
3083

3184

3285
###### Initialize ######
@@ -116,9 +169,10 @@ def welcome_page(text):
116169

117170
# remove this for now
118171
# st.caption(f"git hash: {app_utils.get_git_hash()}")
119-
172+
current_script_directory = os.path.dirname(os.path.abspath(__file__))
173+
logo_path = os.path.join(current_script_directory, 'static/images/amadeusgpt_logo.png')
120174
st.image(
121-
os.path.join(os.getcwd(), "static/images/amadeusgpt_logo.png"),
175+
logo_path,
122176
caption=None,
123177
width=None,
124178
use_column_width=None,
@@ -164,9 +218,9 @@ def welcome_page(text):
164218
st.markdown(
165219
f"{small_font} - This demo serves to highlight a hosted user-experience, but does not include all the features yet..."
166220
)
167-
st.markdown(f"{small_font} - Watch the video below to see how to use the App.")
221+
#st.markdown(f"{small_font} - Watch the video below to see how to use the App.")
168222

169-
st.video("static/demo_withvoice.mp4")
223+
#st.video("static/demo_withvoice.mp4")
170224

171225
st.markdown("### ⚠️ Disclaimers")
172226

amadeusgpt/app_utils.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535

3636
LOG_DIR = os.path.join(os.path.expanduser("~"), "Amadeus_logs")
3737
VIDEO_EXTS = "mp4", "avi", "mov"
38-
user_profile_path = "static/images/cat.png"
39-
bot_profile_path = "static/images/chatbot.png"
38+
current_script_directory = os.path.dirname(os.path.abspath(__file__))
39+
user_profile_path = os.path.join(current_script_directory,'static/images/cat.png')
40+
bot_profile_path = os.path.join(current_script_directory,'static/images/chatbot.png')
4041

4142

4243
def get_git_hash():
@@ -311,9 +312,12 @@ def ask_amadeus(question):
311312

312313

313314
def load_css(css_file):
314-
with open(css_file, "r") as f:
315-
css = f.read()
316-
st.markdown(f"<style>{css}</style>", unsafe_allow_html=True)
315+
current_script_directory = os.path.dirname(os.path.abspath(__file__))
316+
css_path = os.path.join(current_script_directory, 'static/styles/style.css')
317+
if os.path.exists(css_path):
318+
st.markdown(f'<style>{open(css_path).read()}</style>', unsafe_allow_html=True)
319+
else:
320+
st.error(f"File not found: {css_path}")
317321

318322

319323
# caching display roi will make the roi stick to
@@ -626,8 +630,10 @@ def get_sam_image(example):
626630

627631
@conditional_memory_profile
628632
def render_page_by_example(example):
633+
current_script_directory = os.path.dirname(os.path.abspath(__file__))
634+
logo_path = os.path.join(current_script_directory, 'static/images/amadeusgpt_logo.png')
629635
st.image(
630-
os.path.join(os.getcwd(), "static/images/amadeusgpt_logo.png"),
636+
logo_path,
631637
caption=None,
632638
width=None,
633639
use_column_width=None,

amadeusgpt/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def format(cls, message):
2626
session_id = st.session_state.get("session_id", "")
2727
now = datetime.now()
2828
timestamp = now.strftime("%Y%m%d_%H%M%S")
29-
username = st.session_state.get("username", "fake_username")
30-
email = st.session_state.get("email", "fake_email")
29+
username = st.session_state["username"] = "no_username"
30+
email = st.session_state["email"] = "no_email"
3131
return f"{timestamp} - session_id:{session_id} - username:{username} - email: {email} - {message}"
3232

3333
@classmethod

amadeusgpt/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def release_cache_objects(cls):
125125
@classmethod
126126
def load_module_smartly(cls, user_input):
127127
sorted_query_results = match_module(user_input)
128-
if sorted_query_results is None:
128+
if len(sorted_query_results) == 0:
129129
return None
130130
# query result sorted by most relevant module text
131131
modules = []

amadeusgpt/static/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)