Spaces:
Sleeping
Sleeping
Update create.py
Browse files
create.py
CHANGED
@@ -96,88 +96,89 @@ def get_answers(row, instructions) -> str:
|
|
96 |
print (e)
|
97 |
return ""
|
98 |
|
99 |
-
|
100 |
-
st.
|
101 |
-
|
102 |
-
if 'submit' not in st.session_state:
|
103 |
-
st.session_state.submit = False
|
104 |
-
if 'error' not in st.session_state:
|
105 |
-
st.session_state.error = ""
|
106 |
-
if 'success' not in st.session_state:
|
107 |
-
st.session_state.success = None
|
108 |
-
|
109 |
-
if st.session_state.error != "":
|
110 |
-
st.error(st.session_state.error)
|
111 |
|
112 |
-
if st.session_state
|
113 |
-
|
114 |
-
st.
|
115 |
-
|
116 |
-
|
117 |
-
file_name='questions_answers.csv',
|
118 |
-
mime='text/csv',
|
119 |
-
)
|
120 |
-
if st.button('Reset'):
|
121 |
st.session_state.success = None
|
122 |
-
st.rerun()
|
123 |
-
|
124 |
-
else:
|
125 |
-
uploaded_files = st.file_uploader("Upload PDFs Here", type="pdf", accept_multiple_files=True)
|
126 |
-
|
127 |
-
question_protocol = st.text_input("Provide instructions for how questions should be generated", "Write a question based on the text")
|
128 |
-
answer_protocol = st.text_input("Provide instructions for how answers should be generated", "Write an answer based on the text")
|
129 |
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
-
|
140 |
-
if uploaded_files:
|
141 |
-
client = OpenAI(api_key=openai_api_key)
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
st.session_state.success = None
|
155 |
st.session_state.submit = False
|
156 |
-
|
157 |
-
|
158 |
-
textify_output = read_and_textify_advanced(uploaded_files, sentence_chunks)
|
159 |
-
|
160 |
-
df = pd.DataFrame(textify_output)
|
161 |
-
df.columns = ['context']
|
162 |
-
|
163 |
-
if question_protocol == "":
|
164 |
-
question_protocol = "Write questions based on the text"
|
165 |
-
df['questions'] = df.apply(lambda row: get_questions(row['context'], question_protocol), axis=1)
|
166 |
-
|
167 |
-
if answer_protocol == "":
|
168 |
-
answer_protocol = "Write answers based on the text"
|
169 |
-
df['answers'] = df.apply(lambda row: get_answers(row, answer_protocol), axis=1)
|
170 |
-
|
171 |
-
df = df.drop('context', axis=1)
|
172 |
-
|
173 |
-
csv = df.to_csv(index=False).encode('utf-8')
|
174 |
-
|
175 |
-
st.session_state.error = ""
|
176 |
-
st.session_state.success = csv
|
177 |
-
st.session_state.submit = False
|
178 |
-
st.rerun()
|
179 |
-
else:
|
180 |
-
st.session_state.error = "Please upload at least 1 PDF"
|
181 |
-
st.session_state.success = None
|
182 |
-
st.session_state.submit = False
|
183 |
-
st.rerun()
|
|
|
96 |
print (e)
|
97 |
return ""
|
98 |
|
99 |
+
def run():
|
100 |
+
st.set_page_config(page_title="ChatbotGuide", layout="wide")
|
101 |
+
st.title("Chatbot Guide")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
if 'submit' not in st.session_state:
|
104 |
+
st.session_state.submit = False
|
105 |
+
if 'error' not in st.session_state:
|
106 |
+
st.session_state.error = ""
|
107 |
+
if 'success' not in st.session_state:
|
|
|
|
|
|
|
|
|
108 |
st.session_state.success = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
if st.session_state.error != "":
|
111 |
+
st.error(st.session_state.error)
|
112 |
+
|
113 |
+
if st.session_state.success != None:
|
114 |
+
st.success("Success! Download the Q/A pairs below / Click reset to upload more PDFs")
|
115 |
+
st.download_button(
|
116 |
+
label="Download CSV",
|
117 |
+
data=st.session_state.success,
|
118 |
+
file_name='questions_answers.csv',
|
119 |
+
mime='text/csv',
|
120 |
+
)
|
121 |
+
if st.button('Reset'):
|
122 |
+
st.session_state.success = None
|
123 |
+
st.rerun()
|
124 |
+
|
125 |
+
else:
|
126 |
+
uploaded_files = st.file_uploader("Upload PDFs Here", type="pdf", accept_multiple_files=True)
|
127 |
+
|
128 |
+
question_protocol = st.text_input("Provide instructions for how questions should be generated", "Write a question based on the text")
|
129 |
+
answer_protocol = st.text_input("Provide instructions for how answers should be generated", "Write an answer based on the text")
|
130 |
+
|
131 |
+
sentence_chunks = st.number_input("Number sentences per Q/A pair", value=2, step=1, min_value=1, max_value=3)
|
132 |
+
|
133 |
+
openai_api_key = st.text_input("Enter your OpenAI API key", type="password")
|
134 |
|
135 |
+
submit = st.button("Submit")
|
|
|
|
|
136 |
|
137 |
+
if submit:
|
138 |
+
st.session_state.submit = True
|
139 |
+
|
140 |
+
if st.session_state.submit:
|
141 |
+
if uploaded_files:
|
142 |
+
client = OpenAI(api_key=openai_api_key)
|
143 |
+
|
144 |
+
# test api key
|
145 |
+
try:
|
146 |
+
response = client.chat.completions.create(
|
147 |
+
model="gpt-4o-mini",
|
148 |
+
messages=[
|
149 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
150 |
+
{"role": "user", "content": "Say this is a test"}
|
151 |
+
]
|
152 |
+
)
|
153 |
+
except:
|
154 |
+
st.session_state.error = "OpenAI API key is invalid"
|
155 |
+
st.session_state.success = None
|
156 |
+
st.session_state.submit = False
|
157 |
+
run()
|
158 |
+
|
159 |
+
textify_output = read_and_textify_advanced(uploaded_files, sentence_chunks)
|
160 |
+
|
161 |
+
df = pd.DataFrame(textify_output)
|
162 |
+
df.columns = ['context']
|
163 |
+
|
164 |
+
if question_protocol == "":
|
165 |
+
question_protocol = "Write questions based on the text"
|
166 |
+
df['questions'] = df.apply(lambda row: get_questions(row['context'], question_protocol), axis=1)
|
167 |
+
|
168 |
+
if answer_protocol == "":
|
169 |
+
answer_protocol = "Write answers based on the text"
|
170 |
+
df['answers'] = df.apply(lambda row: get_answers(row, answer_protocol), axis=1)
|
171 |
+
|
172 |
+
df = df.drop('context', axis=1)
|
173 |
+
|
174 |
+
csv = df.to_csv(index=False).encode('utf-8')
|
175 |
+
|
176 |
+
st.session_state.error = ""
|
177 |
+
st.session_state.success = csv
|
178 |
+
st.session_state.submit = False
|
179 |
+
run()
|
180 |
+
else:
|
181 |
+
st.session_state.error = "Please upload at least 1 PDF"
|
182 |
st.session_state.success = None
|
183 |
st.session_state.submit = False
|
184 |
+
run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|