Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,8 @@ import io
|
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
-
# Add a title and description
|
| 9 |
st.title("File Q&A AI Assistant")
|
| 10 |
-
st.write("This app allows you to upload a CSV or PDF file, or enter text and ask questions related to the content. The app uses OpenAI's ChatGPT model to assist you in answering your questions about the uploaded content.")
|
| 11 |
|
| 12 |
messages = [
|
| 13 |
{"role": "system", "content": "You are a professional Question and Answer AI Assistant helping with information in regards to a csv, pdf, and text input file."},
|
|
@@ -19,57 +18,50 @@ def chatbot(api_key, query_text, file_data):
|
|
| 19 |
messages.append({"role": "user", "content": query_text})
|
| 20 |
if file_data:
|
| 21 |
messages.append({"role": "user", "content": f"{file_type} File Type: {file_data}"})
|
|
|
|
| 22 |
chat = openai.ChatCompletion.create(
|
| 23 |
-
model="gpt-3.5-turbo", messages=messages
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
api_key = st.text_input("OpenAI API Key", type="password", key=2)
|
| 32 |
query_text = st.text_area("Question", key="input", height=150)
|
| 33 |
file_type = st.selectbox("Select File Type", options=["CSV", "PDF", "Text"])
|
| 34 |
|
| 35 |
-
# Initialize file_data variable with a default value of None
|
| 36 |
file_data = None
|
| 37 |
|
| 38 |
if file_type == "CSV":
|
| 39 |
file = st.file_uploader("Upload CSV file", type="csv")
|
| 40 |
if file:
|
| 41 |
-
# read the csv file into a pandas dataframe
|
| 42 |
df = pd.read_csv(file)
|
| 43 |
st.write("Uploaded CSV file:")
|
| 44 |
-
# display the dataframe as a table
|
| 45 |
st.write(df)
|
| 46 |
-
# update file_data with csv data
|
| 47 |
file_data = df.to_csv(index=False)
|
| 48 |
elif file_type == "PDF":
|
| 49 |
file = st.file_uploader("Upload PDF file", type="pdf")
|
| 50 |
if file:
|
| 51 |
-
# Initialize a PyPDF2 PdfReader object
|
| 52 |
pdf_reader = PdfReader(file)
|
| 53 |
-
|
| 54 |
-
# Extract the text from the PDF file
|
| 55 |
file_data = ""
|
| 56 |
for page in pdf_reader.pages:
|
| 57 |
file_data += page.extract_text()
|
| 58 |
-
|
| 59 |
-
# Display the uploaded PDF file
|
| 60 |
st.write("Uploaded PDF file:")
|
| 61 |
st.write(file_data)
|
| 62 |
-
|
| 63 |
else:
|
| 64 |
file_data = st.text_area("Enter text here")
|
| 65 |
|
| 66 |
-
output_text = st.empty()
|
| 67 |
-
|
| 68 |
if st.button("Send"):
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
-
st.write("Response:")
|
| 72 |
-
st.write(response)
|
| 73 |
except Exception as e:
|
| 74 |
st.error(str(e))
|
| 75 |
|
|
@@ -77,3 +69,11 @@ st.markdown("")
|
|
| 77 |
st.markdown("---")
|
| 78 |
st.markdown("")
|
| 79 |
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from PyPDF2 import PdfReader
|
| 6 |
from PIL import Image
|
| 7 |
|
|
|
|
| 8 |
st.title("File Q&A AI Assistant")
|
| 9 |
+
st.write("This app allows you to upload a CSV or PDF file, or enter text and ask questions related to the content. The app uses OpenAI's ChatGPT model to assist you in answering your questions about the uploaded content, which are streamed back in real time similar to the ChatGPT interface.")
|
| 10 |
|
| 11 |
messages = [
|
| 12 |
{"role": "system", "content": "You are a professional Question and Answer AI Assistant helping with information in regards to a csv, pdf, and text input file."},
|
|
|
|
| 18 |
messages.append({"role": "user", "content": query_text})
|
| 19 |
if file_data:
|
| 20 |
messages.append({"role": "user", "content": f"{file_type} File Type: {file_data}"})
|
| 21 |
+
|
| 22 |
chat = openai.ChatCompletion.create(
|
| 23 |
+
model="gpt-3.5-turbo", messages=messages, stream=True
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
response_text = st.empty()
|
| 27 |
+
response_line = ""
|
| 28 |
+
|
| 29 |
+
for chunk in chat:
|
| 30 |
+
chunk_message = chunk['choices'][0]['delta']
|
| 31 |
+
if chunk_message.get('content'):
|
| 32 |
+
response_line += chunk_message['content']
|
| 33 |
+
response_text.write("Response: " + response_line)
|
| 34 |
+
|
| 35 |
+
messages.append({"role": "assistant", "content": response_line})
|
| 36 |
|
| 37 |
api_key = st.text_input("OpenAI API Key", type="password", key=2)
|
| 38 |
query_text = st.text_area("Question", key="input", height=150)
|
| 39 |
file_type = st.selectbox("Select File Type", options=["CSV", "PDF", "Text"])
|
| 40 |
|
|
|
|
| 41 |
file_data = None
|
| 42 |
|
| 43 |
if file_type == "CSV":
|
| 44 |
file = st.file_uploader("Upload CSV file", type="csv")
|
| 45 |
if file:
|
|
|
|
| 46 |
df = pd.read_csv(file)
|
| 47 |
st.write("Uploaded CSV file:")
|
|
|
|
| 48 |
st.write(df)
|
|
|
|
| 49 |
file_data = df.to_csv(index=False)
|
| 50 |
elif file_type == "PDF":
|
| 51 |
file = st.file_uploader("Upload PDF file", type="pdf")
|
| 52 |
if file:
|
|
|
|
| 53 |
pdf_reader = PdfReader(file)
|
|
|
|
|
|
|
| 54 |
file_data = ""
|
| 55 |
for page in pdf_reader.pages:
|
| 56 |
file_data += page.extract_text()
|
|
|
|
|
|
|
| 57 |
st.write("Uploaded PDF file:")
|
| 58 |
st.write(file_data)
|
|
|
|
| 59 |
else:
|
| 60 |
file_data = st.text_area("Enter text here")
|
| 61 |
|
|
|
|
|
|
|
| 62 |
if st.button("Send"):
|
| 63 |
try:
|
| 64 |
+
chatbot(api_key, query_text, file_data)
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
st.error(str(e))
|
| 67 |
|
|
|
|
| 69 |
st.markdown("---")
|
| 70 |
st.markdown("")
|
| 71 |
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True)
|
| 72 |
+
|
| 73 |
+
hide_streamlit_style = """
|
| 74 |
+
<style>
|
| 75 |
+
#MainMenu {visibility: hidden;}
|
| 76 |
+
footer {visibility: hidden;}
|
| 77 |
+
</style>
|
| 78 |
+
"""
|
| 79 |
+
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
|