Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,250 +1,150 @@
|
|
1 |
-
import streamlit as st
|
2 |
import extra_streamlit_components as stx
|
3 |
import requests
|
4 |
from PIL import Image
|
|
|
5 |
from io import BytesIO
|
|
|
6 |
from llama_index.llms.palm import PaLM
|
7 |
-
from llama_index import ServiceContext, VectorStoreIndex, Document
|
8 |
from llama_index.memory import ChatMemoryBuffer
|
9 |
import os
|
10 |
import datetime
|
11 |
-
from llama_index.llms import Cohere
|
12 |
-
from llama_index.query_engine import CitationQueryEngine
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
#imports for resnet
|
17 |
-
from transformers import AutoFeatureExtractor, ResNetForImageClassification
|
18 |
-
import torch
|
19 |
-
from io import BytesIO
|
20 |
|
21 |
# Set up the title of the application
|
22 |
-
st.title("
|
23 |
-
|
24 |
-
st.write("My
|
25 |
|
26 |
# Sidebar
|
27 |
st.sidebar.markdown('## Created By')
|
28 |
st.sidebar.markdown("""
|
29 |
-
Harshad Suryawanshi
|
30 |
-
- [Linkedin](https://www.linkedin.com/in/harshadsuryawanshi/)
|
31 |
-
- [Medium](https://harshadsuryawanshi.medium.com/)
|
32 |
""")
|
33 |
|
34 |
-
|
35 |
st.sidebar.markdown('## Other Projects')
|
36 |
st.sidebar.markdown("""
|
37 |
-
- [Building My Own GPT4-V with PaLM and Kosmos](https://lnkd.in/dawgKZBP)
|
38 |
- [AI Equity Research Analyst](https://ai-eqty-rsrch-anlyst.streamlit.app/)
|
39 |
- [Recasting "The Office" Scene](https://blackmirroroffice.streamlit.app/)
|
40 |
- [Story Generator](https://appstorycombined-agaf9j4ceit.streamlit.app/)
|
41 |
-
""")
|
42 |
-
|
43 |
st.sidebar.markdown('## Disclaimer')
|
44 |
st.sidebar.markdown("""
|
45 |
-
This application
|
46 |
""")
|
47 |
|
48 |
# Initialize the cookie manager
|
49 |
cookie_manager = stx.CookieManager()
|
50 |
|
51 |
-
#Function to
|
52 |
-
|
53 |
-
@st.cache_resource(show_spinner="Initializing ResNet model for image classification. Please wait...")
|
54 |
-
def load_model_and_labels():
|
55 |
-
# Load animal labels as a dictionary
|
56 |
-
animal_labels_dict = {}
|
57 |
-
with open('imagenet_animal_labels_subset.txt', 'r') as file:
|
58 |
-
for line in file:
|
59 |
-
parts = line.strip().split(':')
|
60 |
-
class_id = int(parts[0].strip())
|
61 |
-
label_name = parts[1].strip().strip("'")
|
62 |
-
animal_labels_dict[class_id] = label_name
|
63 |
-
|
64 |
-
# Initialize feature extractor and model
|
65 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained("microsoft/resnet-18")
|
66 |
-
model = ResNetForImageClassification.from_pretrained("microsoft/resnet-18")
|
67 |
-
|
68 |
-
return feature_extractor, model, animal_labels_dict
|
69 |
-
|
70 |
-
feature_extractor, model, animal_labels_dict = load_model_and_labels()
|
71 |
-
|
72 |
-
# Function to predict image label
|
73 |
@st.cache_data
|
74 |
def get_image_caption(image_data):
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
return
|
86 |
-
|
87 |
-
|
88 |
-
@st.cache_resource(show_spinner="Initializing LLM and setting up service context. Please wait...")
|
89 |
-
def init_llm(api_key):
|
90 |
-
# llm = PaLM(api_key=api_key)
|
91 |
-
llm = Cohere(model="command", api_key=st.secrets['COHERE_API_TOKEN'])
|
92 |
-
|
93 |
-
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")
|
94 |
-
|
95 |
-
storage_context = StorageContext.from_defaults(persist_dir="storage")
|
96 |
-
index = load_index_from_storage(storage_context, index_id="index", service_context=service_context)
|
97 |
-
chatmemory = ChatMemoryBuffer.from_defaults(token_limit=1500)
|
98 |
-
|
99 |
-
return llm, service_context, storage_context, index, chatmemory
|
100 |
-
|
101 |
-
llm, service_context, storage_context, index, chatmemory = init_llm(os.environ["GOOGLE_API_KEY"])
|
102 |
-
|
103 |
-
def is_animal(predicted_label_id):
|
104 |
-
# Check if the predicted label ID is within the animal classes range
|
105 |
-
return 0 <= predicted_label_id <= 398
|
106 |
-
|
107 |
|
108 |
# Function to create the chat engine.
|
109 |
@st.cache_resource
|
110 |
def create_chat_engine(img_desc, api_key):
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
doc = Document(text=img_desc)
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
|
|
|
|
125 |
)
|
126 |
-
|
127 |
-
return query_engine
|
128 |
-
|
129 |
-
|
130 |
|
131 |
# Clear chat function
|
132 |
def clear_chat():
|
133 |
if "messages" in st.session_state:
|
134 |
del st.session_state.messages
|
135 |
-
if "
|
136 |
-
del st.session_state.
|
137 |
|
138 |
# Callback function to clear the chat when a new image is uploaded
|
139 |
def on_image_upload():
|
140 |
-
clear_chat()
|
141 |
-
|
142 |
-
# Retrieve the message count from cookies
|
143 |
-
message_count = cookie_manager.get(cookie='message_count')
|
144 |
-
if message_count is None:
|
145 |
-
message_count = 0
|
146 |
-
else:
|
147 |
message_count = int(message_count)
|
148 |
|
149 |
# If the message limit has been reached, disable the inputs
|
150 |
-
|
151 |
-
|
152 |
st.error("Notice: The maximum message limit for this demo version has been reached.")
|
153 |
# Disabling the uploader and input by not displaying them
|
154 |
image_uploader_placeholder = st.empty() # Placeholder for the uploader
|
155 |
chat_input_placeholder = st.empty() # Placeholder for the chat input
|
156 |
-
|
157 |
else:
|
158 |
# Add a clear chat button
|
159 |
if st.button("Clear Chat"):
|
160 |
-
clear_chat()
|
161 |
|
162 |
# Image upload section.
|
163 |
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="uploaded_image", on_change=on_image_upload)
|
164 |
-
|
165 |
-
col1, col2, col3 = st.columns([1, 2, 1])
|
166 |
-
with col2: # Camera input will be in the middle column
|
167 |
-
camera_image = st.camera_input("Take a picture", on_change=on_image_upload)
|
168 |
-
|
169 |
-
|
170 |
-
# Determine the source of the image (upload or camera)
|
171 |
-
if image_file is not None:
|
172 |
-
image_data = BytesIO(image_file.getvalue())
|
173 |
-
elif camera_image is not None:
|
174 |
-
image_data = BytesIO(camera_image.getvalue())
|
175 |
-
else:
|
176 |
-
image_data = None
|
177 |
-
|
178 |
-
if image_data:
|
179 |
# Display the uploaded image at a standard width.
|
180 |
-
st.
|
181 |
-
st.image(image_data, caption='Uploaded Image.', width=200)
|
182 |
|
183 |
# Process the uploaded image to get a caption.
|
184 |
-
|
185 |
-
img_desc
|
186 |
-
|
187 |
-
if not (is_animal(label_id)):
|
188 |
-
#st.error("Please upload image of an animal!")
|
189 |
-
st.error("Please upload image of an animal!")
|
190 |
-
st.stop()
|
191 |
|
192 |
# Initialize the chat engine with the image description.
|
193 |
chat_engine = create_chat_engine(img_desc, os.environ["GOOGLE_API_KEY"])
|
194 |
-
st.write("Image Uploaded Successfully. Ask me anything about it.")
|
195 |
-
|
196 |
|
197 |
# Initialize session state for messages if it doesn't exist
|
198 |
if "messages" not in st.session_state:
|
199 |
-
st.session_state.messages = []
|
200 |
|
201 |
# Display previous messages
|
202 |
for message in st.session_state.messages:
|
203 |
-
|
204 |
-
|
205 |
-
st.write(message["content"])
|
206 |
|
207 |
# Handle new user input
|
208 |
user_input = st.chat_input("Ask me about the image:", key="chat_input")
|
209 |
-
if user_input:
|
210 |
-
# Append user message to the session state
|
211 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
212 |
|
213 |
# Display user message immediately
|
214 |
with st.chat_message("user"):
|
215 |
-
st.
|
216 |
|
217 |
# Call the chat engine to get the response if an image has been uploaded
|
218 |
-
if
|
219 |
try:
|
220 |
with st.spinner('Waiting for the chat engine to respond...'):
|
221 |
# Get the response from your chat engine
|
222 |
-
|
223 |
-
|
224 |
-
You always answer in great detail and are polite. Your job is to roleplay as an {img_desc}.
|
225 |
-
Remember to make {img_desc} sounds while talking but dont overdo it.
|
226 |
-
"""
|
227 |
-
|
228 |
-
response = chat_engine.query(f"{system_prompt}. {user_input}")
|
229 |
-
|
230 |
-
#response = chat_engine.chat(f"""You are a chatbot that roleplays as an animal and also makes animal sounds when chatting.
|
231 |
-
#You always answer in great detail and are polite. Your responses always descriptive.
|
232 |
-
#Your job is to rolelpay as the animal that is mentioned in the image the user has uploaded. Image description: {img_desc}. User question
|
233 |
-
#{user_input}""")
|
234 |
-
|
235 |
# Append assistant message to the session state
|
236 |
-
st.session_state.messages.append({"role": "assistant", "content": response
|
237 |
|
238 |
# Display the assistant message
|
239 |
with st.chat_message("assistant"):
|
240 |
-
st.
|
241 |
-
st.expander("hello")
|
242 |
|
243 |
except Exception as e:
|
244 |
-
st.error(f'An error occurred
|
245 |
# Optionally, you can choose to break the flow here if a critical error happens
|
246 |
# return
|
247 |
|
248 |
-
# Increment the message count and update the cookie
|
249 |
message_count += 1
|
250 |
cookie_manager.set('message_count', str(message_count), expires_at=datetime.datetime.now() + datetime.timedelta(days=30))
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import extra_streamlit_components as stx
|
2 |
import requests
|
3 |
from PIL import Image
|
4 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
5 |
from io import BytesIO
|
6 |
+
import replicate
|
7 |
from llama_index.llms.palm import PaLM
|
8 |
+
from llama_index import ServiceContext, VectorStoreIndex, Document
|
9 |
from llama_index.memory import ChatMemoryBuffer
|
10 |
import os
|
11 |
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Set up the title of the application
|
14 |
+
#st.title("PaLM-Kosmos-Vision")
|
15 |
+
st.set_page_config(layout="wide")
|
16 |
+
st.write("My version of ChatGPT vision. You can upload an image and start chatting with the LLM about the image")
|
17 |
|
18 |
# Sidebar
|
19 |
st.sidebar.markdown('## Created By')
|
20 |
st.sidebar.markdown("""
|
21 |
+
[Harshad Suryawanshi](https://www.linkedin.com/in/harshadsuryawanshi/)
|
|
|
|
|
22 |
""")
|
23 |
|
|
|
24 |
st.sidebar.markdown('## Other Projects')
|
25 |
st.sidebar.markdown("""
|
|
|
26 |
- [AI Equity Research Analyst](https://ai-eqty-rsrch-anlyst.streamlit.app/)
|
27 |
- [Recasting "The Office" Scene](https://blackmirroroffice.streamlit.app/)
|
28 |
- [Story Generator](https://appstorycombined-agaf9j4ceit.streamlit.app/)
|
|
|
|
|
29 |
st.sidebar.markdown('## Disclaimer')
|
30 |
st.sidebar.markdown("""
|
31 |
+
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.
|
32 |
""")
|
33 |
|
34 |
# Initialize the cookie manager
|
35 |
cookie_manager = stx.CookieManager()
|
36 |
|
37 |
+
# Function to get image caption via Kosmos2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
@st.cache_data
|
39 |
def get_image_caption(image_data):
|
40 |
+
input_data = {
|
41 |
+
"image": image_data,
|
42 |
+
"description_type": "Brief"
|
43 |
+
}
|
44 |
+
output = replicate.run(
|
45 |
+
"lucataco/kosmos-2:3e7b211c29c092f4bcc8853922cc986baa52efe255876b80cac2c2fbb4aff805",
|
46 |
+
input=input_data
|
47 |
+
)
|
48 |
+
# Split the output string on the newline character and take the first item
|
49 |
+
text_description = output.split('\n\n')[0]
|
50 |
+
return text_description
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
# Function to create the chat engine.
|
53 |
@st.cache_resource
|
54 |
def create_chat_engine(img_desc, api_key):
|
55 |
+
llm = PaLM(api_key=api_key)
|
56 |
+
service_context = ServiceContext.from_defaults(llm=llm)
|
57 |
+
|
58 |
doc = Document(text=img_desc)
|
59 |
+
index = VectorStoreIndex.from_documents([doc], service_context=service_context)
|
60 |
+
chatmemory = ChatMemoryBuffer.from_defaults(token_limit=1500)
|
61 |
+
|
62 |
+
chat_engine = index.as_chat_engine(
|
63 |
+
chat_mode="context",
|
64 |
+
system_prompt=(
|
65 |
+
f"You are a chatbot, able to have normal interactions, as well as talk. "
|
66 |
+
"You always answer in great detail and are polite. Your responses always descriptive. "
|
67 |
+
"Your job is to talk about an image the user has uploaded. Image description: {img_desc}."
|
68 |
+
),
|
69 |
+
verbose=True,
|
70 |
+
memory=chatmemory
|
71 |
)
|
72 |
+
return chat_engine
|
|
|
|
|
|
|
73 |
|
74 |
# Clear chat function
|
75 |
def clear_chat():
|
76 |
if "messages" in st.session_state:
|
77 |
del st.session_state.messages
|
78 |
+
if "image_file" in st.session_state:
|
79 |
+
del st.session_state.image_file
|
80 |
|
81 |
# Callback function to clear the chat when a new image is uploaded
|
82 |
def on_image_upload():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
message_count = int(message_count)
|
84 |
|
85 |
# If the message limit has been reached, disable the inputs
|
86 |
+
if message_count >= 20:
|
87 |
+
|
88 |
st.error("Notice: The maximum message limit for this demo version has been reached.")
|
89 |
# Disabling the uploader and input by not displaying them
|
90 |
image_uploader_placeholder = st.empty() # Placeholder for the uploader
|
91 |
chat_input_placeholder = st.empty() # Placeholder for the chat input
|
92 |
+
|
93 |
else:
|
94 |
# Add a clear chat button
|
95 |
if st.button("Clear Chat"):
|
|
|
96 |
|
97 |
# Image upload section.
|
98 |
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="uploaded_image", on_change=on_image_upload)
|
99 |
+
if image_file:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
# Display the uploaded image at a standard width.
|
101 |
+
st.image(image_file, caption='Uploaded Image.', width=200)
|
|
|
102 |
|
103 |
# Process the uploaded image to get a caption.
|
104 |
+
image_data = BytesIO(image_file.getvalue())
|
105 |
+
img_desc = get_image_caption(image_data)
|
106 |
+
st.write("Image Uploaded Successfully. Ask me anything about it.")
|
|
|
|
|
|
|
|
|
107 |
|
108 |
# Initialize the chat engine with the image description.
|
109 |
chat_engine = create_chat_engine(img_desc, os.environ["GOOGLE_API_KEY"])
|
|
|
|
|
110 |
|
111 |
# Initialize session state for messages if it doesn't exist
|
112 |
if "messages" not in st.session_state:
|
|
|
113 |
|
114 |
# Display previous messages
|
115 |
for message in st.session_state.messages:
|
116 |
+
with st.chat_message(message["role"]):
|
117 |
+
st.markdown(message["content"])
|
|
|
118 |
|
119 |
# Handle new user input
|
120 |
user_input = st.chat_input("Ask me about the image:", key="chat_input")
|
|
|
|
|
|
|
121 |
|
122 |
# Display user message immediately
|
123 |
with st.chat_message("user"):
|
124 |
+
st.markdown(user_input)
|
125 |
|
126 |
# Call the chat engine to get the response if an image has been uploaded
|
127 |
+
if image_file and user_input:
|
128 |
try:
|
129 |
with st.spinner('Waiting for the chat engine to respond...'):
|
130 |
# Get the response from your chat engine
|
131 |
+
response = chat_engine.chat(user_input)
|
132 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
# Append assistant message to the session state
|
134 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
135 |
|
136 |
# Display the assistant message
|
137 |
with st.chat_message("assistant"):
|
138 |
+
st.markdown(response)
|
|
|
139 |
|
140 |
except Exception as e:
|
141 |
+
st.error(f'An error occurred: {e}')
|
142 |
# Optionally, you can choose to break the flow here if a critical error happens
|
143 |
# return
|
144 |
|
|
|
145 |
message_count += 1
|
146 |
cookie_manager.set('message_count', str(message_count), expires_at=datetime.datetime.now() + datetime.timedelta(days=30))
|
147 |
+
|
148 |
+
# Set Replicate and Google API keys
|
149 |
+
os.environ['REPLICATE_API_TOKEN'] = st.secrets['REPLICATE_API_TOKEN']
|
150 |
+
os.environ["GOOGLE_API_KEY"] = st.secrets['GOOGLE_API_KEY']
|