Spaces:
Sleeping
Sleeping
Update create.py
Browse files95 unmodified lines9697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218395 unmodified linesprint (e)return ""st.set_page_config(page_title="ChatbotGuide", layout="wide")st.title("Chatbot Guide")if 'submit' not in st.session_state:st.session_state.submit = Falseif 'error' not in st.session_state:st.session_state.error = ""if 'success' not in st.session_state:st.session_state.success = Noneif st.session_state.error != "":st.error(st.session_state.error)if st.session_state.success != None:st.success("Success! Download the Q/A pairs below / Click reset to upload more PDFs")st.download_button(label="Download CSV",data=st.session_state.success,file_name='questions_answers.csv',mime='text/csv',)if st.button('Reset'):st.session_state.success = Nonest.rerun()else:uploaded_files = st.file_uploader("Upload PDFs Here", type="pdf", accept_multiple_files=True)question_protocol = st.text_input("Provide instructions for how questions should be generated", "Write a question based on the text")answer_protocol = st.text_input("Provide instructions for how answers should be generated", "Write an answer based on the text")sentence_chunks = st.number_input("Number sentences per Q/A pair", value=2, step=1, min_value=1, max_value=3)openai_api_key = st.text_input("Enter your OpenAI API key", type="password")submit = st.button("Submit")if submit:st.session_state.submit = Trueif st.session_state.submit:if uploaded_files:client = OpenAI(api_key=openai_api_key)# test api keytry:response = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Say this is a test"}])except:st.session_state.error = "OpenAI API key is invalid"st.session_state.success = Nonest.session_state.submit = Falsest.rerun()textify_output = read_and_textify_advanced(uploaded_files, sentence_chunks)df = pd.DataFrame(textify_output)df.columns = ['context']if question_protocol == "":question_protocol = "Write questions based on the text"df['questions'] = df.apply(lambda row: get_questions(row['context'], question_protocol), axis=1)if answer_protocol == "":answer_protocol = "Write answers based on the text"df['answers'] = df.apply(lambda row: get_answers(row, answer_protocol), axis=1)df = df.drop('context', axis=1)csv = df.to_csv(index=False).encode('utf-8')st.session_state.error = ""st.session_state.success = csvst.session_state.submit = Falsest.rerun()else:st.session_state.error = "Please upload at least 1 PDF"st.session_state.success = Nonest.session_state.submit = Falsest.rerun()95 unmodified lines9697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318495 unmodified linesprint (e)return ""def run():st.set_page_config(page_title="ChatbotGuide", layout="wide")st.title("Chatbot Guide")if 'submit' not in st.session_state:st.session_state.submit = Falseif 'error' not in st.session_state:st.session_state.error = ""if 'success' not in st.session_state:st.session_state.success = Noneif st.session_state.error != "":st.error(st.session_state.error)if st.session_state.success != None:st.success("Success! Download the Q/A pairs below / Click reset to upload more PDFs")st.download_button(label="Download CSV",data=st.session_state.success,file_name='questions_answers.csv',mime='text/csv',)if st.button('Reset'):st.session_state.success = Nonest.rerun()else:uploaded_files = st.file_uploader("Upload PDFs Here", type="pdf", accept_multiple_files=True)question_protocol = st.text_input("Provide instructions for how questions should be generated", "Write a question based on the text")answer_protocol = st.text_input("Provide instructions for how answers should be generated", "Write an answer based on the text")sentence_chunks = st.number_input("Number sentences per Q/A pair", value=2, step=1, min_value=1, max_value=3)openai_api_key = st.text_input("Enter your OpenAI API key", type="password")submit = st.button("Submit")if submit:st.session_state.submit = Trueif st.session_state.submit:if uploaded_files:client = OpenAI(api_key=openai_api_key)# test api keytry:response = client.chat.completions.create(model="gpt-4o-mini",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "Say this is a test"}])except:st.session_state.error = "OpenAI API key is invalid"st.session_state.success = Nonest.session_state.submit = Falserun()textify_output = read_and_textify_advanced(uploaded_files, sentence_chunks)df = pd.DataFrame(textify_output)df.columns = ['context']if question_protocol == "":question_protocol = "Write questions based on the text"df['questions'] = df.apply(lambda row: get_questions(row['context'], question_protocol), axis=1)if answer_protocol == "":answer_protocol = "Write answers based on the text"df['answers'] = df.apply(lambda row: get_answers(row, answer_protocol), axis=1)df = df.drop('context', axis=1)csv = df.to_csv(index=False).encode('utf-8')st.session_state.error = ""st.session_state.success = csvst.session_state.submit = Falserun()else:st.session_state.error = "Please upload at least 1 PDF"st.session_state.success = Nonest.session_state.submit = Falserun()
create.py+79-78