Spaces:
Sleeping
Sleeping
Delete app3.py
Browse files
app3.py
DELETED
|
@@ -1,96 +0,0 @@
|
|
| 1 |
-
import chainlit as cl
|
| 2 |
-
import arxiv
|
| 3 |
-
from langchain.chat_models import ChatOpenAI
|
| 4 |
-
from langchain.chains import ConversationalRetrievalChain
|
| 5 |
-
from langchain.memory import ConversationBufferMemory
|
| 6 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 7 |
-
from langchain.embeddings import OpenAIEmbeddings
|
| 8 |
-
from langchain.vectorstores import FAISS
|
| 9 |
-
import os
|
| 10 |
-
|
| 11 |
-
# Set your OpenAI API key
|
| 12 |
-
os.environ["OPENAI_API_KEY"] = "sk-proj-vFPqdrr801blzZCRBjztT3BlbkFJJJeQVcc62PA40cQ1S9Zv"
|
| 13 |
-
|
| 14 |
-
# Initialize global variables
|
| 15 |
-
selected_paper = None
|
| 16 |
-
qa_chain = None
|
| 17 |
-
papers = []
|
| 18 |
-
state = "SEARCH" # Possible states: SEARCH, SELECT, QA
|
| 19 |
-
|
| 20 |
-
@cl.on_chat_start
|
| 21 |
-
def start():
|
| 22 |
-
global state
|
| 23 |
-
state = "SEARCH"
|
| 24 |
-
cl.Message(content="Welcome! Please enter a search query for arXiv papers.").send()
|
| 25 |
-
|
| 26 |
-
@cl.on_message
|
| 27 |
-
def main(message: str):
|
| 28 |
-
global selected_paper, qa_chain, papers, state
|
| 29 |
-
|
| 30 |
-
if state == "SEARCH":
|
| 31 |
-
# Search for papers
|
| 32 |
-
search = arxiv.Search(
|
| 33 |
-
query=message,
|
| 34 |
-
max_results=5,
|
| 35 |
-
sort_by=arxiv.SortCriterion.Relevance
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
papers = list(search.results())
|
| 39 |
-
|
| 40 |
-
if not papers:
|
| 41 |
-
cl.Message(content="No papers found. Please try another search query.").send()
|
| 42 |
-
return
|
| 43 |
-
|
| 44 |
-
# Create a numbered list of papers
|
| 45 |
-
paper_list = "\n".join([f"{i+1}. {paper.title} - {paper.authors[0]}" for i, paper in enumerate(papers)])
|
| 46 |
-
cl.Message(content=f"Please select a paper by entering its number:\n\n{paper_list}\n\nEnter the number of the paper you want to select:").send()
|
| 47 |
-
state = "SELECT"
|
| 48 |
-
|
| 49 |
-
elif state == "SELECT":
|
| 50 |
-
try:
|
| 51 |
-
selected_index = int(message) - 1
|
| 52 |
-
if 0 <= selected_index < len(papers):
|
| 53 |
-
selected_paper = papers[selected_index]
|
| 54 |
-
else:
|
| 55 |
-
cl.Message(content="Invalid selection. Please try again.").send()
|
| 56 |
-
return
|
| 57 |
-
except ValueError:
|
| 58 |
-
cl.Message(content="Invalid input. Please enter a number.").send()
|
| 59 |
-
return
|
| 60 |
-
|
| 61 |
-
# Download and process the selected paper
|
| 62 |
-
paper_text = selected_paper.summary
|
| 63 |
-
|
| 64 |
-
# Split the text into chunks
|
| 65 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
| 66 |
-
chunks = text_splitter.split_text(paper_text)
|
| 67 |
-
|
| 68 |
-
# Create embeddings and vector store
|
| 69 |
-
embeddings = OpenAIEmbeddings()
|
| 70 |
-
vectorstore = FAISS.from_texts(chunks, embeddings)
|
| 71 |
-
|
| 72 |
-
# Create the conversational chain
|
| 73 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 74 |
-
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 75 |
-
ChatOpenAI(temperature=0),
|
| 76 |
-
vectorstore.as_retriever(),
|
| 77 |
-
memory=memory
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
cl.Message(content=f"Selected paper: {selected_paper.title}\nYou can now ask questions about this paper. Type 'new search' when you want to search for a different paper.").send()
|
| 81 |
-
state = "QA"
|
| 82 |
-
|
| 83 |
-
elif state == "QA":
|
| 84 |
-
if message.lower() == "new search":
|
| 85 |
-
state = "SEARCH"
|
| 86 |
-
selected_paper = None
|
| 87 |
-
qa_chain = None
|
| 88 |
-
papers = []
|
| 89 |
-
cl.Message(content="Sure! Please enter a new search query for arXiv papers.").send()
|
| 90 |
-
else:
|
| 91 |
-
# Answer questions about the selected paper
|
| 92 |
-
response = qa_chain({"question": message})
|
| 93 |
-
cl.Message(content=response["answer"]).send()
|
| 94 |
-
|
| 95 |
-
if __name__ == "__main__":
|
| 96 |
-
cl.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|