Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +70 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import streamlit as st
|
| 4 |
+
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."},
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
def chatbot(api_key, query_text, file_data):
|
| 17 |
+
openai.api_key = api_key
|
| 18 |
+
if query_text:
|
| 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 |
+
reply = chat.choices[0].message.content
|
| 26 |
+
messages.append({"role": "assistant", "content": reply})
|
| 27 |
+
return reply
|
| 28 |
+
|
| 29 |
+
api_key = st.text_input("OpenAI API Key", key=2)
|
| 30 |
+
query_text = st.text_input("Question", key="input")
|
| 31 |
+
file_type = st.selectbox("Select File Type", options=["CSV", "PDF", "Text"])
|
| 32 |
+
|
| 33 |
+
# Initialize file_data variable with a default value of None
|
| 34 |
+
file_data = None
|
| 35 |
+
|
| 36 |
+
if file_type == "CSV":
|
| 37 |
+
file = st.file_uploader("Upload CSV file", type="csv")
|
| 38 |
+
if file:
|
| 39 |
+
# read the csv file into a pandas dataframe
|
| 40 |
+
df = pd.read_csv(file)
|
| 41 |
+
st.write("Uploaded CSV file:")
|
| 42 |
+
# display the dataframe as a table
|
| 43 |
+
st.write(df)
|
| 44 |
+
# update file_data with csv data
|
| 45 |
+
file_data = df.to_csv(index=False)
|
| 46 |
+
elif file_type == "PDF":
|
| 47 |
+
file = st.file_uploader("Upload PDF file", type="pdf")
|
| 48 |
+
if file:
|
| 49 |
+
# Initialize a PyPDF2 PdfReader object
|
| 50 |
+
pdf_reader = PdfReader(file)
|
| 51 |
+
|
| 52 |
+
# Extract the text from the PDF file
|
| 53 |
+
file_data = ""
|
| 54 |
+
for page in pdf_reader.pages:
|
| 55 |
+
file_data += page.extract_text()
|
| 56 |
+
|
| 57 |
+
# Display the uploaded PDF file
|
| 58 |
+
st.write("Uploaded PDF file:")
|
| 59 |
+
st.write(file_data)
|
| 60 |
+
|
| 61 |
+
else:
|
| 62 |
+
file_data = st.text_area("Enter text here")
|
| 63 |
+
|
| 64 |
+
output_text = st.empty()
|
| 65 |
+
|
| 66 |
+
if st.button("Send"):
|
| 67 |
+
response = chatbot(api_key, query_text, file_data)
|
| 68 |
+
if response:
|
| 69 |
+
st.write("Response:")
|
| 70 |
+
st.write(response)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
streamlit
|
| 3 |
+
PyPDF2
|
| 4 |
+
PIL
|