RLTMai/filegpt
0
1import streamlit as st2from streamlit_chat import message3import os4from utils import (5 parse_docx,6 parse_pdf,7 parse_txt,8 parse_csv,9 search_docs,10 embed_docs,11 text_to_docs,12 get_answer,13 get_sources,14 wrap_text_in_html,15)16from openai.error import OpenAIError17 18def clear_submit():19 st.session_state["submit"] = False20 21def set_openai_api_key(api_key: str):22 st.session_state["OPENAI_API_KEY"] = api_key23 24st.markdown('<h1>File GPT 🤖<small> by <a href="https://codegpt.co">Code GPT</a></small></h1>', unsafe_allow_html=True)25 26# Sidebar27index = None28doc = None29with st.sidebar:30 user_secret = st.text_input(31 "OpenAI API Key",32 type="password",33 placeholder="Paste your OpenAI API key here (sk-...)",34 help="You can get your API key from https://platform.openai.com/account/api-keys.",35 value=st.session_state.get("OPENAI_API_KEY", ""),36 )37 if user_secret:38 set_openai_api_key(user_secret)39 40 uploaded_file = st.file_uploader(41 "Upload a pdf, docx, or txt file",42 type=["pdf", "docx", "txt", "csv"],43 help="Scanned documents are not supported yet!",44 on_change=clear_submit,45 )46 47 if uploaded_file is not None:48 if uploaded_file.name.endswith(".pdf"):49 doc = parse_pdf(uploaded_file)50 elif uploaded_file.name.endswith(".docx"):51 doc = parse_docx(uploaded_file)52 elif uploaded_file.name.endswith(".csv"):53 doc = parse_csv(uploaded_file)54 elif uploaded_file.name.endswith(".txt"):55 doc = parse_txt(uploaded_file)56 else:57 st.error("File type not supported")58 doc = None59 text = text_to_docs(doc)60 try:61 with st.spinner("Indexing document... This may take a while⏳"):62 index = embed_docs(text)63 st.session_state["api_key_configured"] = True64 except OpenAIError as e:65 st.error(e._message)66 67tab1, tab2 = st.tabs(["Intro", "Chat with the File"])68with tab1:69 st.markdown("### How does it work?")70 st.markdown('<p>Read the article to know how it works: <a target="_blank" href="https://medium.com/@dan.avila7/file-gpt-conversaci%C3%B3n-por-chat-con-un-archivo-698d17570358">Medium Article</a></p>', unsafe_allow_html=True)71 st.write("File GPT was written with the following tools:")72 st.markdown("#### Code GPT")73 st.write('All code was written with the help of Code GPT. Visit https://codegpt.co to get the extension.')74 st.markdown("#### Streamlit")75 st.write('The design was written with <a target="_blank" href="https://streamlit.io/">Streamlit</a>.', unsafe_allow_html=True)76 st.markdown("#### LangChain")77 st.write('Question answering with source <a target="_blank" href="https://langchain.readthedocs.io/en/latest/use_cases/question_answering.html#adding-in-sources">Langchain QA</a>.', unsafe_allow_html=True)78 st.markdown("#### Embedding")79 st.write('<a target="_blank" href="https://platform.openai.com/docs/guides/embeddings">Embedding</a> is done via the OpenAI API with "text-embedding-ada-002"', unsafe_allow_html=True)80 st.write("Please note that you must have credits in your OpenAI account to use this tool. Each file uploaded to the platform consumes credits for embedding and each query consumes credits to obtain the response.")81 st.markdown("""---""")82 st.write('Author: <a target="_blank" href="https://www.linkedin.com/in/daniel-avila-arias/">Daniel Avila</a>', unsafe_allow_html=True)83 st.write('Repo: <a target="_blank" href="https://github.com/davila7/file-gpt">Github</a>', unsafe_allow_html=True)84 st.write("This software was developed with Code GPT, for more information visit: https://codegpt.co", unsafe_allow_html=True)85 86with tab2:87 st.write('To obtain an API Key you must create an OpenAI account at the following link: https://openai.com/api/')88 if 'generated' not in st.session_state:89 st.session_state['generated'] = []90 91 if 'past' not in st.session_state:92 st.session_state['past'] = []93 94 def get_text():95 if user_secret:96 st.header("Ask me something about the document:")97 input_text = st.text_area("You:", on_change=clear_submit)98 return input_text99 user_input = get_text()100 101 button = st.button("Submit")102 if button or st.session_state.get("submit"):103 if not user_input:104 st.error("Please enter a question!")105 else:106 st.session_state["submit"] = True107 sources = search_docs(index, user_input)108 try:109 answer = get_answer(sources, user_input)110 st.session_state.past.append(user_input)111 st.session_state.generated.append(answer["output_text"].split("SOURCES: ")[0])112 except OpenAIError as e:113 st.error(e._message)114 if st.session_state['generated']:115 for i in range(len(st.session_state['generated'])-1, -1, -1):116 message(st.session_state["generated"][i], key=str(i))117 message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')