AI-ANK commited on
Commit
cc63034
1 Parent(s): 58ea41b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +219 -0
app.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, StorageContext, load_index_from_storage
8
+ from llama_index.memory import ChatMemoryBuffer
9
+ import os
10
+ import datetime
11
+
12
+ #imports for resnet
13
+ from transformers import AutoFeatureExtractor, ResNetForImageClassification
14
+ import torch
15
+ from io import BytesIO
16
+
17
+ # Set up the title of the application
18
+ st.title("AInimal Go!")
19
+ #st.set_page_config(layout="wide")
20
+ st.write("My Pokemon Go inspired 'AInimal Go!' app. You can upload an image or snap a picture of an animal and start chatting with it")
21
+
22
+ # Sidebar
23
+ st.sidebar.markdown('## Created By')
24
+ st.sidebar.markdown("""
25
+ Harshad Suryawanshi
26
+ - [Linkedin](https://www.linkedin.com/in/harshadsuryawanshi/)
27
+ - [Medium](https://harshadsuryawanshi.medium.com/)
28
+ """)
29
+
30
+
31
+ st.sidebar.markdown('## Other Projects')
32
+ st.sidebar.markdown("""
33
+ - [Building My Own GPT4-V with PaLM and Kosmos](https://lnkd.in/dawgKZBP)
34
+ - [AI Equity Research Analyst](https://ai-eqty-rsrch-anlyst.streamlit.app/)
35
+ - [Recasting "The Office" Scene](https://blackmirroroffice.streamlit.app/)
36
+ - [Story Generator](https://appstorycombined-agaf9j4ceit.streamlit.app/)
37
+ """)
38
+
39
+ st.sidebar.markdown('## Disclaimer')
40
+ st.sidebar.markdown("""
41
+ This application, titled 'AInimal Go!', is a conceptual prototype designed to demonstrate the innovative use of Large Language Models (LLMs) in enabling interactive conversations with animals through images. While the concept is vaguely inspired by the interactive and augmented reality elements popularized by games like Pokemon Go, it does not use any assets, characters, or intellectual property from the Pokemon franchise. The interactions and conversations generated by this application are entirely fictional and created for entertainment and educational purposes. They should not be regarded as factual or accurate representations of animal behavior or communication. The author and the application do not hold any affiliation with the Pokemon brand or its creators, and no endorsement from them is implied. Users are encouraged to use this application responsibly and with an understanding of its purely illustrative nature.
42
+ """)
43
+
44
+ # Initialize the cookie manager
45
+ cookie_manager = stx.CookieManager()
46
+
47
+ #Function to init resnet
48
+
49
+ @st.cache_resource()
50
+ def load_model_and_labels():
51
+ # Load animal labels as a dictionary
52
+ animal_labels_dict = {}
53
+ with open('imagenet_animal_labels_subset.txt', 'r') as file:
54
+ for line in file:
55
+ parts = line.strip().split(':')
56
+ class_id = int(parts[0].strip())
57
+ label_name = parts[1].strip().strip("'")
58
+ animal_labels_dict[class_id] = label_name
59
+
60
+ # Initialize feature extractor and model
61
+ feature_extractor = AutoFeatureExtractor.from_pretrained("microsoft/resnet-18")
62
+ model = ResNetForImageClassification.from_pretrained("microsoft/resnet-18")
63
+
64
+ return feature_extractor, model, animal_labels_dict
65
+
66
+ feature_extractor, model, animal_labels_dict = load_model_and_labels()
67
+
68
+ # Function to predict image label
69
+ @st.cache_data
70
+ def get_image_caption(image_data):
71
+ image = Image.open(image_data)
72
+ inputs = feature_extractor(images=image, return_tensors="pt")
73
+
74
+ with torch.no_grad():
75
+ logits = model(**inputs).logits
76
+
77
+ predicted_label_id = logits.argmax(-1).item()
78
+ predicted_label_name = model.config.id2label[predicted_label_id]
79
+ st.write(predicted_label_name)
80
+ # Return the predicted animal name
81
+ return predicted_label_name, predicted_label_id
82
+
83
+
84
+ @st.cache_resource
85
+ def init_llm(api_key):
86
+ llm = PaLM(api_key=api_key)
87
+ service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")
88
+
89
+ storage_context = StorageContext.from_defaults(persist_dir="storage")
90
+ index = load_index_from_storage(storage_context, index_id="index", service_context=service_context)
91
+ chatmemory = ChatMemoryBuffer.from_defaults(token_limit=1500)
92
+
93
+ return llm, service_context, storage_context, index, chatmemory
94
+
95
+ llm, service_context, storage_context, index, chatmemory = init_llm(st.secrets['GOOGLE_API_KEY'])
96
+
97
+ def is_animal(predicted_label_id):
98
+ # Check if the predicted label ID is within the animal classes range
99
+ return 0 <= predicted_label_id <= 398
100
+
101
+
102
+ # Function to create the chat engine.
103
+ @st.cache_resource
104
+ def create_chat_engine(img_desc, api_key):
105
+ doc = Document(text=img_desc)
106
+
107
+ chat_engine = index.as_chat_engine(
108
+ chat_mode="react",
109
+ verbose=True,
110
+ memory=chatmemory
111
+ )
112
+
113
+ return chat_engine
114
+
115
+
116
+ # Clear chat function
117
+ def clear_chat():
118
+ if "messages" in st.session_state:
119
+ del st.session_state.messages
120
+ if "image_file" in st.session_state:
121
+ del st.session_state.image_file
122
+
123
+ # Callback function to clear the chat when a new image is uploaded
124
+ def on_image_upload():
125
+ clear_chat()
126
+
127
+ # Retrieve the message count from cookies
128
+ message_count = cookie_manager.get(cookie='message_count')
129
+ if message_count is None:
130
+ message_count = 0
131
+ else:
132
+ message_count = int(message_count)
133
+
134
+ # If the message limit has been reached, disable the inputs
135
+ if message_count <= 20:
136
+ st.error("Notice: The maximum message limit for this demo version has been reached.")
137
+ # Disabling the uploader and input by not displaying them
138
+ image_uploader_placeholder = st.empty() # Placeholder for the uploader
139
+ chat_input_placeholder = st.empty() # Placeholder for the chat input
140
+ st.stop()
141
+ else:
142
+ # Add a clear chat button
143
+ if st.button("Clear Chat"):
144
+ clear_chat()
145
+
146
+ # Image upload section.
147
+ image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"], key="uploaded_image", on_change=on_image_upload)
148
+
149
+ #col1, col2, col3 = st.columns([1, 2, 1])
150
+ #with col2: # Camera input will be in the middle column
151
+ camera_image = st.camera_input("Take a picture")
152
+
153
+
154
+ # Determine the source of the image (upload or camera)
155
+ if image_file is not None:
156
+ image_data = BytesIO(image_file.getvalue())
157
+ elif camera_image is not None:
158
+ image_data = BytesIO(camera_image.getvalue())
159
+ else:
160
+ image_data = None
161
+
162
+ if image_data:
163
+ # Display the uploaded image at a standard width.
164
+ st.image(image_data, caption='Uploaded Image.', width=200)
165
+
166
+ # Process the uploaded image to get a caption.
167
+ img_desc, label_id = get_image_caption(image_data)
168
+
169
+ if not (is_animal(label_id)):
170
+ st.error("Please upload image of an animal!")
171
+ st.stop()
172
+
173
+ # Initialize the chat engine with the image description.
174
+ chat_engine = create_chat_engine(img_desc, st.secrets['GOOGLE_API_KEY'])
175
+ st.write("Image Uploaded Successfully. Ask me anything about it.")
176
+
177
+
178
+ # Initialize session state for messages if it doesn't exist
179
+ if "messages" not in st.session_state:
180
+ st.session_state.messages = []
181
+
182
+ # Display previous messages
183
+ for message in st.session_state.messages:
184
+ with st.chat_message(message["role"]):
185
+ st.markdown(message["content"])
186
+
187
+ # Handle new user input
188
+ user_input = st.chat_input("Ask me about the image:", key="chat_input")
189
+ if user_input:
190
+ # Append user message to the session state
191
+ st.session_state.messages.append({"role": "user", "content": user_input})
192
+
193
+ # Display user message immediately
194
+ with st.chat_message("user"):
195
+ st.markdown(user_input)
196
+
197
+ # Call the chat engine to get the response if an image has been uploaded
198
+ if image_file and user_input:
199
+ try:
200
+ with st.spinner('Waiting for the chat engine to respond...'):
201
+ # Get the response from your chat engine
202
+ response = chat_engine.chat(f"""You are a chatbot that roleplays as an animal and also makes animal sounds when chatting.
203
+ You always answer in great detail and are polite. Your responses always descriptive.
204
+ Your job is to rolelpay as the animal that is mentioned in the image the user has uploaded. Image description: {img_desc}. User question
205
+ {user_input}""")
206
+
207
+ # Append assistant message to the session state
208
+ st.session_state.messages.append({"role": "assistant", "content": response})
209
+
210
+ # Display the assistant message
211
+ with st.chat_message("assistant"):
212
+ st.markdown(response)
213
+
214
+ except Exception as e:
215
+ st.error(f'An error occurred.')
216
+
217
+ # Increment the message count and update the cookie
218
+ message_count += 1
219
+ cookie_manager.set('message_count', str(message_count), expires_at=datetime.datetime.now() + datetime.timedelta(days=30))