File size: 3,211 Bytes
71d129a
 
 
 
 
 
 
 
86ece49
71d129a
 
 
 
 
 
 
 
 
 
 
603a503
71d129a
603a503
71d129a
603a503
 
 
 
 
 
 
 
 
 
 
71d129a
a40632b
dd72b01
71d129a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c07608c
71d129a
c07608c
 
 
 
 
 
 
 
 
 
 
 
 
 
71d129a
 
 
 
a40632b
603a503
a40632b
 
 
 
 
 
 
603a503
 
 
 
 
 
 
c07608c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import openai
import pandas as pd
import streamlit as st
import io
from PyPDF2 import PdfReader
from PIL import Image

st.title("File Q&A AI Assistant")
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 is streamed back in real time similar to the ChatGPT interface.")

messages = [
    {"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."},
]

def chatbot(api_key, query_text, file_data):
    openai.api_key = api_key
    if query_text:
        messages.append({"role": "user", "content": query_text})
    if file_data:
        messages.append({"role": "user", "content": f"{file_type} File Type: {file_data}"})
    
    chat = openai.ChatCompletion.create(
        model="gpt-3.5-turbo", messages=messages, stream=True
    )
    
    response_text = st.empty()
    response_line = ""
    
    for chunk in chat:
        chunk_message = chunk['choices'][0]['delta']
        if chunk_message.get('content'):
            response_line += chunk_message['content']
            response_text.write("Response: " + response_line)
    
    messages.append({"role": "assistant", "content": response_line})

api_key = st.text_input("OpenAI API Key", type="password", key=2)
query_text = st.text_area("Question", key="input", height=100)
file_type = st.selectbox("Select File Type", options=["CSV", "PDF", "Text"])

file_data = None

if file_type == "CSV":
    file = st.file_uploader("Upload CSV file", type="csv")
    if file:
        df = pd.read_csv(file)
        st.write("Uploaded CSV file:")
        st.write(df)       
        file_data = df.to_csv(index=False)
elif file_type == "PDF":
    file = st.file_uploader("Upload PDF file", type="pdf")
    if file:
        pdf_reader = PdfReader(file)
        file_data = ""
        for page in pdf_reader.pages:
            file_data += page.extract_text()

        st.write("Uploaded PDF file:")
        with st.container():
            st.markdown(
                "<style>"
                ".scrollable {"
                "    max-height: 300px;"
                "    overflow-y: auto;"
                "}"
                "</style>"
                '<div class="scrollable">'
                + file_data.replace("\n", "<br>")
                + "</div>",
                unsafe_allow_html=True,
            )
            st.markdown("")
else:
    file_data = st.text_area("Enter text here")

if st.button("Send"):
    try:
        chatbot(api_key, query_text, file_data)
    except Exception as e:
        st.error(str(e))

st.markdown("")
st.markdown("---")
st.markdown("")
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)

hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)