Spaces:
Paused
Paused
File size: 12,299 Bytes
3fff7cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
from llama_index import Document
from llama_index.chat_engine import CondenseQuestionChatEngine
from llama_index.indices.vector_store import VectorIndexRetriever
from llama_index.node_parser import SimpleNodeParser
from llama_index import LangchainEmbedding, ServiceContext
from llama_index import VectorStoreIndex
from llama_index import StorageContext, load_index_from_storage
from llama_index.query_engine import RetrieverQueryEngine
from llama_index.response_synthesizers import TreeSummarize,get_response_synthesizer
from llama_index.llms import ChatMessage
from langchain.llms import Clarifai
from langchain.embeddings import ClarifaiEmbeddings
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
import uuid
import streamlit as st
import modal
CLARIFAI_PAT = st.secrets.CLARIFAI_PAT
MODERATION_THRESHOLD = st.secrets.MODERATION_THRESHOLD
st.set_page_config(page_title="Research Buddy: Insights and Q&A on AI Research Papers using GPT and Nougat", page_icon="π§", layout="centered", initial_sidebar_state="auto", menu_items=None)
st.title(body="AI Research Buddy: Nougat + GPT Powered Paper Insights ππ€")
st.info("""This Application currently only works with arxiv and acl anthology web links which belong to the format:-
1) Arxiv:- https://arxiv.org/abs/paper_unique_identifier
2) ACL Anthology:- https://aclanthology.org/paper_unique_identifier/
This Application uses the recently released Meta Nougat Visual Transformer for processing Papers""", icon="βΉοΈ")
user_input = st.text_input("Enter the arxiv or acl anthology url of the paper", "https://aclanthology.org/2023.semeval-1.266/")
def initialize_session_state():
if "vector_store" not in st.session_state:
st.session_state.vector_store = None
if "messages" not in st.session_state.keys():
st.session_state.messages = [
{"role": "assistant", "content": "Ask me a question about the research paper"}
]
if "paper_content" not in st.session_state:
st.session_state.paper_content = None
if "paper_insights" not in st.session_state:
st.session_state.paper_insights = None
initialize_session_state()
def get_paper_content(url: str) -> str:
with st.spinner(text="Using Nougat(https://facebookresearch.github.io/nougat/) to read the paper contents and get the markdown representation of the paper"):
f = modal.Function.lookup("streamlit-hack", "main")
output = f.call(url)
st.session_state.paper_content = output
return output
def index_paper_content(content: str):
with st.spinner(text="Indexing the paper β hang tight! This should take 3-5 minutes"):
try:
LLM_USER_ID = 'openai'
LLM_APP_ID = 'chat-completion'
# Change these to whatever model and text URL you want to use
LLM_MODEL_ID = 'GPT-3_5-turbo'
llm = Clarifai(pat=CLARIFAI_PAT, user_id=LLM_USER_ID, app_id=LLM_APP_ID, model_id=LLM_MODEL_ID)
documents = [Document(text=content)]
parser = SimpleNodeParser.from_defaults()
nodes = parser.get_nodes_from_documents(documents)
USER_ID = 'openai'
APP_ID = 'embed'
# Change these to whatever model and text URL you want to use
MODEL_ID = 'text-embedding-ada'
embeddings = ClarifaiEmbeddings(pat=CLARIFAI_PAT, user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
embed_model = LangchainEmbedding(embeddings)
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
index = VectorStoreIndex(nodes, service_context=service_context)
persist_dir = uuid.uuid4().hex
st.session_state.vector_store = persist_dir
index.storage_context.persist(persist_dir=persist_dir)
return "Paper has been Indexed"
except Exception as e:
print(str(e))
return "Unable to Index the Research Paper"
def generate_insights():
with st.spinner(text="Generating insights on the paper and preparing the Chatbot"):
try:
LLM_USER_ID = 'openai'
LLM_APP_ID = 'chat-completion'
# Change these to whatever model and text URL you want to use
LLM_MODEL_ID = 'GPT-3_5-turbo'
llm = Clarifai(pat=CLARIFAI_PAT, user_id=LLM_USER_ID, app_id=LLM_APP_ID, model_id=LLM_MODEL_ID)
USER_ID = 'openai'
APP_ID = 'embed'
# Change these to whatever model and text URL you want to use
MODEL_ID = 'text-embedding-ada'
embeddings = ClarifaiEmbeddings(pat=CLARIFAI_PAT, user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
embed_model = LangchainEmbedding(embeddings)
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
index = load_index_from_storage(
StorageContext.from_defaults(persist_dir=st.session_state.vector_store),
service_context=service_context
)
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=4,
)
# configure response synthesizer
response_synthesizer = get_response_synthesizer(
response_mode="tree_summarize", service_context=service_context
)
# assemble query engine
query_engine = RetrieverQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
)
response_key_insights = query_engine.query("Generate core crux insights, contributions and results of the paper as Key Topics and thier content in markdown format where each Key Topic is in bold followed by its content")
except Exception as e:
print(str(e))
response_key_insights = "Error While Generating Insights"
st.session_state.paper_insights = response_key_insights.response
if st.button("Read and Index Paper"):
paper_content = get_paper_content(url=user_input)
if st.session_state.paper_content is not None:
with st.expander("See Paper Contents"):
st.markdown(paper_content)
result = index_paper_content(content=paper_content)
st.write(result)
generate_insights()
if st.session_state.paper_content is not None:
with st.expander("See Paper Contents"):
st.markdown(st.session_state.paper_content)
if st.session_state.paper_insights is not None:
st.sidebar.title("# π Illuminating Research Insights ππ‘")
st.sidebar.write(st.session_state.paper_insights)
def reset_conversation():
st.session_state.messages = [
{"role": "assistant", "content": "Ask me a question about the research paper"}
]
def moderate_text(text: str) -> tuple:
MODERATION_USER_ID = 'clarifai'
MODERATION_APP_ID = 'main'
# Change these to whatever model and text URL you want to use
MODERATION_MODEL_ID = 'moderation-multilingual-text-classification'
MODERATION_MODEL_VERSION_ID = '79c2248564b0465bb96265e0c239352b'
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + CLARIFAI_PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id=MODERATION_USER_ID, app_id=MODERATION_APP_ID)
# To use a local text file, uncomment the following lines
# with open(TEXT_FILE_LOCATION, "rb") as f:
# file_bytes = f.read()
post_model_outputs_response = stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
user_app_id=userDataObject,
# The userDataObject is created in the overview and is required when using a PAT
model_id=MODERATION_MODEL_ID,
version_id=MODERATION_MODEL_VERSION_ID, # This is optional. Defaults to the latest model version
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
text=resources_pb2.Text(
raw=text
)
)
)
]
),
metadata=metadata
)
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
print(post_model_outputs_response.status)
raise Exception("Post model outputs failed, status: " + post_model_outputs_response.status.description)
# Since we have one input, one output will exist here
output = post_model_outputs_response.outputs[0]
moderation_reasons = ""
intervention_required = False
for concept in output.data.concepts:
if concept.value > MODERATION_THRESHOLD:
moderation_reasons += concept.name + ","
intervention_required = True
return moderation_reasons, intervention_required
if st.session_state.vector_store is not None:
LLM_USER_ID = 'openai'
LLM_APP_ID = 'chat-completion'
# Change these to whatever model and text URL you want to use
LLM_MODEL_ID = 'GPT-3_5-turbo'
llm = Clarifai(pat=CLARIFAI_PAT, user_id=LLM_USER_ID, app_id=LLM_APP_ID, model_id=LLM_MODEL_ID)
USER_ID = 'openai'
APP_ID = 'embed'
# Change these to whatever model and text URL you want to use
MODEL_ID = 'text-embedding-ada'
embeddings = ClarifaiEmbeddings(pat=CLARIFAI_PAT, user_id=USER_ID, app_id=APP_ID, model_id=MODEL_ID)
embed_model = LangchainEmbedding(embeddings)
service_context = ServiceContext.from_defaults(llm=llm, embed_model=embed_model)
index = load_index_from_storage(
StorageContext.from_defaults(persist_dir=st.session_state.vector_store),
service_context=service_context
)
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=2,
)
# configure response synthesizer
response_synthesizer = get_response_synthesizer(
response_mode="tree_summarize", service_context=service_context
)
# assemble query engine
query_engine = RetrieverQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
)
custom_chat_history = []
for message in st.session_state.messages:
custom_message = ChatMessage(role=message["role"], content=message["content"])
custom_chat_history.append(custom_message)
chat_engine = CondenseQuestionChatEngine.from_defaults(service_context=service_context, query_engine=query_engine,
verbose=True,
chat_history=custom_chat_history)
if prompt := st.chat_input("Your question"): # Prompt for user input and save to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
st.button('Reset Chat', on_click=reset_conversation)
for message in st.session_state.messages: # Display the prior chat messages
with st.chat_message(message["role"]):
st.write(message["content"])
# If last message is not from assistant, generate a new response
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
reason, intervene = moderate_text(prompt)
except Exception as e:
print(str(e))
reason = ''
intervene = False
if not intervene:
response = chat_engine.chat(prompt)
st.write(response.response)
message = {"role": "assistant", "content": response.response}
st.session_state.messages.append(message) # Add response to message history
else:
response = f"This query cannot be processed as it has been detected to be {reason}"
st.write(response)
message = {"role": "assistant", "content": response.response}
st.session_state.messages.append(message)
|