Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,10 @@ from llama_index import ServiceContext, VectorStoreIndex, Document, StorageConte
|
|
8 |
from llama_index.memory import ChatMemoryBuffer
|
9 |
import os
|
10 |
import datetime
|
|
|
|
|
|
|
|
|
11 |
|
12 |
#imports for resnet
|
13 |
from transformers import AutoFeatureExtractor, ResNetForImageClassification
|
@@ -83,7 +87,9 @@ def get_image_caption(image_data):
|
|
83 |
|
84 |
@st.cache_resource(show_spinner="Initializing LLM and setting up service context. Please wait...")
|
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")
|
@@ -92,7 +98,7 @@ def init_llm(api_key):
|
|
92 |
|
93 |
return llm, service_context, storage_context, index, chatmemory
|
94 |
|
95 |
-
llm, service_context, storage_context, index, chatmemory = init_llm(
|
96 |
|
97 |
def is_animal(predicted_label_id):
|
98 |
# Check if the predicted label ID is within the animal classes range
|
@@ -110,22 +116,15 @@ def create_chat_engine(img_desc, api_key):
|
|
110 |
# Now is_animal is a boolean indicating whether the image is of an animal
|
111 |
print("Is the image of an animal:", is_animal)
|
112 |
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
f"""You are a chatbot, able to have normal interactions, as well as talk.
|
120 |
-
You always answer in great detail and are polite. Your responses always descriptive.
|
121 |
-
Your job is to rolelpay as the animal that is mentioned in the image the user has uploaded. Image description: {img_desc}."""
|
122 |
-
|
123 |
-
),
|
124 |
-
verbose=True,
|
125 |
-
memory=chatmemory
|
126 |
)
|
127 |
|
128 |
-
return
|
129 |
|
130 |
|
131 |
|
@@ -191,7 +190,7 @@ else:
|
|
191 |
st.stop()
|
192 |
|
193 |
# Initialize the chat engine with the image description.
|
194 |
-
chat_engine = create_chat_engine(img_desc,
|
195 |
st.write("Image Uploaded Successfully. Ask me anything about it.")
|
196 |
|
197 |
|
@@ -220,17 +219,26 @@ else:
|
|
220 |
try:
|
221 |
with st.spinner('Waiting for the chat engine to respond...'):
|
222 |
# Get the response from your chat engine
|
223 |
-
|
224 |
-
You
|
225 |
-
|
226 |
-
{
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
# Append assistant message to the session state
|
229 |
st.session_state.messages.append({"role": "assistant", "content": response.response})
|
230 |
|
231 |
# Display the assistant message
|
232 |
with st.chat_message("assistant"):
|
233 |
st.write(response.response)
|
|
|
234 |
|
235 |
except Exception as e:
|
236 |
st.error(f'An error occurred.')
|
@@ -240,4 +248,3 @@ else:
|
|
240 |
# Increment the message count and update the cookie
|
241 |
message_count += 1
|
242 |
cookie_manager.set('message_count', str(message_count), expires_at=datetime.datetime.now() + datetime.timedelta(days=30))
|
243 |
-
|
|
|
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
|
|
|
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")
|
|
|
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
|
|
|
116 |
# Now is_animal is a boolean indicating whether the image is of an animal
|
117 |
print("Is the image of an animal:", is_animal)
|
118 |
|
119 |
+
query_engine = CitationQueryEngine.from_args(
|
120 |
+
index,
|
121 |
+
similarity_top_k=3,
|
122 |
+
# here we can control how granular citation sources are, the default is 512
|
123 |
+
citation_chunk_size=512,
|
124 |
+
verbose=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
)
|
126 |
|
127 |
+
return query_engine
|
128 |
|
129 |
|
130 |
|
|
|
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 |
|
|
|
219 |
try:
|
220 |
with st.spinner('Waiting for the chat engine to respond...'):
|
221 |
# Get the response from your chat engine
|
222 |
+
system_prompt=f"""
|
223 |
+
You are a chatbot, able to have normal interactions. Do not make up information.
|
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.response})
|
237 |
|
238 |
# Display the assistant message
|
239 |
with st.chat_message("assistant"):
|
240 |
st.write(response.response)
|
241 |
+
st.expander("hello")
|
242 |
|
243 |
except Exception as e:
|
244 |
st.error(f'An error occurred.')
|
|
|
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))
|
|