File size: 3,424 Bytes
aac7f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5db107
aac7f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from llama_index import download_loader, GPTSimpleVectorIndex
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.embeddings import OpenAIEmbeddings
from openai.error import OpenAIError
from base import ArxivReader_mod_search, ArxivReader_mod
import os
import sys
import openai
import streamlit as st
from  sidebar  import *


# create the website reader
#ArxivReader= download_loader("ArxivReader")
global index,dummy



st.set_page_config(page_title="My App")

st.header("ArxivGPT ")
sidebar()
st.subheader(
    "I am an ArxivGPT(Chatbot) Research Scientist. Please fill the fields below to start our discussion.."
    "If you find it useful, you can kindly donate here [Stripe](https://buy.stripe.com/cN2dUu44OahXaJO288)"
)


#api_key = st.text_input('Open Api Key:')


#if 'OPENAI_API_KEY' not in st.session_state:
#   if api_key:
#      st.session_state['OPENAI_API_KEY'] = api_key
#      st.write(st.session_state.get('OPENAI_API_KEY'))



os.environ['OPENAI_API_KEY'] = str(st.session_state.get('OPENAIAPI_KEY'))


query = st.text_input("What scientific topic do you want to discuss?")

max_query = st.number_input("How many papers should i investigate?", step=0)
dummy = st.radio(
    "According to which criterion?",
    ('Relevance', 'LastUpdated', 'SubmittedDate'))


#st.write(load_call(documents))
if query and max_query:
    if dummy == 'Relevance':
        search_query_int = 0

    if dummy== "LastUpdated":
        search_query_int = 1

    if dummy == "SubmittedDate":
        search_query_int = 2
    try:
        # load the reader
        loader = ArxivReader_mod()
        documents = loader.load_data(search_query=query,    papers_dir= "papers", max_results=max_query, search_criterion=search_query_int)
        index = GPTSimpleVectorIndex.from_documents(documents)
        st.markdown("Arxiv papers are loaded based on the criteria")
        st.session_state["api_key_configured"] = True
    except Exception as e:
        st.error("Please configure your OpenAI API key!")




with st.form("my_form"):

        user = st.text_input("Ask me any question about "+query+":")
        # Every form must have a submit button.
        submitted = st.form_submit_button("Submit")
        if user:
            response = str(index.query(user))

        if submitted:
          try:
            if  not st.session_state.get("api_key_configured"):
                st.error("Please configure your OpenAI API key!")
            if not query:
                st.error("Please enter a topic to discuss!")
            if not max_query:
                st.error("Please choose number of files to be loaded!")
            if(st.session_state.get("api_key_configured") and query and max_query):
                st.text_area("Bot 🤖", response, height=500)
          except OpenAIError as e:
                 st.error(e._message)
          except (RuntimeError, TypeError, NameError):
                 st.error("Runtime/Type/Name Error")
          except OSError as err:
                 st.error("OS error:",err)
          except ValueError:
                 st.error("Could not convert data to string.")
          except Exception as err:
                 st.error(f"Unexpected {err=}, {type(err)=}")
          except ConnectionError as err:
                 st.error("Connection Error:", err)