AI-ANK commited on
Commit
4f5a895
·
1 Parent(s): 48b6028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import extra_streamlit_components as stx
2
  import requests
3
  from PIL import Image
@@ -17,16 +18,21 @@ st.write("My version of ChatGPT vision. You can upload an image and start chatti
17
 
18
  # Sidebar
19
  st.sidebar.markdown('## Created By')
20
- st.sidebar.markdown("[Harshad Suryawanshi](https://www.linkedin.com/in/harshadsuryawanshi/)")
 
 
21
 
22
  st.sidebar.markdown('## Other Projects')
23
  st.sidebar.markdown("""
24
  - [AI Equity Research Analyst](https://ai-eqty-rsrch-anlyst.streamlit.app/)
25
  - [Recasting "The Office" Scene](https://blackmirroroffice.streamlit.app/)
26
- - [Story Generator](https://appstorycombined-agaf9j4ceit.streamlit.app/)"""
27
-
 
28
  st.sidebar.markdown('## Disclaimer')
29
- st.sidebar.markdown("This application is a conceptual prototype created to demonstrate the potential of Large Language Models (LLMs) in generating equity research reports. The contents generated by this application are purely illustrative and should not be construed as financial advice, endorsements, or recommendations. The author and the application do not provide any guarantee regarding the accuracy, completeness, or timeliness of the information provided.")
 
 
30
 
31
  # Initialize the cookie manager
32
  cookie_manager = stx.CookieManager()
@@ -50,12 +56,11 @@ def get_image_caption(image_data):
50
  @st.cache_resource
51
  def create_chat_engine(img_desc, api_key):
52
  llm = PaLM(api_key=api_key)
53
- service_context = ServiceContext.from_defaults(llm=llm)
54
-
55
  doc = Document(text=img_desc)
56
  index = VectorStoreIndex.from_documents([doc], service_context=service_context)
57
  chatmemory = ChatMemoryBuffer.from_defaults(token_limit=1500)
58
-
59
  chat_engine = index.as_chat_engine(
60
  chat_mode="context",
61
  system_prompt=(
@@ -77,26 +82,31 @@ def clear_chat():
77
 
78
  # Callback function to clear the chat when a new image is uploaded
79
  def on_image_upload():
 
 
 
 
 
 
 
80
  message_count = int(message_count)
81
 
82
  # If the message limit has been reached, disable the inputs
83
  if message_count >= 20:
84
-
85
  st.error("Notice: The maximum message limit for this demo version has been reached.")
86
  # Disabling the uploader and input by not displaying them
87
  image_uploader_placeholder = st.empty() # Placeholder for the uploader
88
  chat_input_placeholder = st.empty() # Placeholder for the chat input
89
-
90
  else:
91
  # Add a clear chat button
92
  if st.button("Clear Chat"):
 
93
 
94
  # Image upload section.
95
  image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="uploaded_image", on_change=on_image_upload)
96
  if image_file:
97
  # Display the uploaded image at a standard width.
98
  st.image(image_file, caption='Uploaded Image.', width=200)
99
-
100
  # Process the uploaded image to get a caption.
101
  image_data = BytesIO(image_file.getvalue())
102
  img_desc = get_image_caption(image_data)
@@ -107,6 +117,7 @@ else:
107
 
108
  # Initialize session state for messages if it doesn't exist
109
  if "messages" not in st.session_state:
 
110
 
111
  # Display previous messages
112
  for message in st.session_state.messages:
@@ -115,6 +126,9 @@ else:
115
 
116
  # Handle new user input
117
  user_input = st.chat_input("Ask me about the image:", key="chat_input")
 
 
 
118
 
119
  # Display user message immediately
120
  with st.chat_message("user"):
@@ -135,13 +149,17 @@ else:
135
  st.markdown(response)
136
 
137
  except Exception as e:
138
- st.error(f'An error occurred: {e}')
139
  # Optionally, you can choose to break the flow here if a critical error happens
140
  # return
141
 
 
142
  message_count += 1
143
  cookie_manager.set('message_count', str(message_count), expires_at=datetime.datetime.now() + datetime.timedelta(days=30))
144
 
 
 
 
145
  # Set Replicate and Google API keys
146
  os.environ['REPLICATE_API_TOKEN'] = st.secrets['REPLICATE_API_TOKEN']
147
  os.environ["GOOGLE_API_KEY"] = st.secrets['GOOGLE_API_KEY']
 
1
+ import streamlit as st
2
  import extra_streamlit_components as stx
3
  import requests
4
  from PIL import Image
 
18
 
19
  # Sidebar
20
  st.sidebar.markdown('## Created By')
21
+ st.sidebar.markdown("""
22
+ [Harshad Suryawanshi](https://www.linkedin.com/in/harshadsuryawanshi/)
23
+ """)
24
 
25
  st.sidebar.markdown('## Other Projects')
26
  st.sidebar.markdown("""
27
  - [AI Equity Research Analyst](https://ai-eqty-rsrch-anlyst.streamlit.app/)
28
  - [Recasting "The Office" Scene](https://blackmirroroffice.streamlit.app/)
29
+ - [Story Generator](https://appstorycombined-agaf9j4ceit.streamlit.app/)
30
+ """)
31
+
32
  st.sidebar.markdown('## Disclaimer')
33
+ st.sidebar.markdown("""
34
+ This application is a conceptual prototype created to demonstrate the potential of Large Language Models (LLMs) in generating equity research reports. The contents generated by this application are purely illustrative and should not be construed as financial advice, endorsements, or recommendations. The author and the application do not provide any guarantee regarding the accuracy, completeness, or timeliness of the information provided.
35
+ """)
36
 
37
  # Initialize the cookie manager
38
  cookie_manager = stx.CookieManager()
 
56
  @st.cache_resource
57
  def create_chat_engine(img_desc, api_key):
58
  llm = PaLM(api_key=api_key)
59
+ service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")
 
60
  doc = Document(text=img_desc)
61
  index = VectorStoreIndex.from_documents([doc], service_context=service_context)
62
  chatmemory = ChatMemoryBuffer.from_defaults(token_limit=1500)
63
+
64
  chat_engine = index.as_chat_engine(
65
  chat_mode="context",
66
  system_prompt=(
 
82
 
83
  # Callback function to clear the chat when a new image is uploaded
84
  def on_image_upload():
85
+ clear_chat()
86
+
87
+ # Retrieve the message count from cookies
88
+ message_count = cookie_manager.get(cookie='message_count')
89
+ if message_count is None:
90
+ message_count = 0
91
+ else:
92
  message_count = int(message_count)
93
 
94
  # If the message limit has been reached, disable the inputs
95
  if message_count >= 20:
 
96
  st.error("Notice: The maximum message limit for this demo version has been reached.")
97
  # Disabling the uploader and input by not displaying them
98
  image_uploader_placeholder = st.empty() # Placeholder for the uploader
99
  chat_input_placeholder = st.empty() # Placeholder for the chat input
 
100
  else:
101
  # Add a clear chat button
102
  if st.button("Clear Chat"):
103
+ clear_chat()
104
 
105
  # Image upload section.
106
  image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="uploaded_image", on_change=on_image_upload)
107
  if image_file:
108
  # Display the uploaded image at a standard width.
109
  st.image(image_file, caption='Uploaded Image.', width=200)
 
110
  # Process the uploaded image to get a caption.
111
  image_data = BytesIO(image_file.getvalue())
112
  img_desc = get_image_caption(image_data)
 
117
 
118
  # Initialize session state for messages if it doesn't exist
119
  if "messages" not in st.session_state:
120
+ st.session_state.messages = []
121
 
122
  # Display previous messages
123
  for message in st.session_state.messages:
 
126
 
127
  # Handle new user input
128
  user_input = st.chat_input("Ask me about the image:", key="chat_input")
129
+ if user_input:
130
+ # Append user message to the session state
131
+ st.session_state.messages.append({"role": "user", "content": user_input})
132
 
133
  # Display user message immediately
134
  with st.chat_message("user"):
 
149
  st.markdown(response)
150
 
151
  except Exception as e:
152
+ st.error(f'An error occurred.')
153
  # Optionally, you can choose to break the flow here if a critical error happens
154
  # return
155
 
156
+ # Increment the message count and update the cookie
157
  message_count += 1
158
  cookie_manager.set('message_count', str(message_count), expires_at=datetime.datetime.now() + datetime.timedelta(days=30))
159
 
160
+
161
+
162
+
163
  # Set Replicate and Google API keys
164
  os.environ['REPLICATE_API_TOKEN'] = st.secrets['REPLICATE_API_TOKEN']
165
  os.environ["GOOGLE_API_KEY"] = st.secrets['GOOGLE_API_KEY']