Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import openai
import streamlit as st
import pandas as pd
import PyPDF2

def pdf_reader(file):
"""Reads text from a PDF file"""
try:
# Read the uploaded file using PyPDF2
pdf = PyPDF2.PdfReader(file)
text = ''
for page in range(len(pdf.pages)):
text += pdf.pages[page].extract_text()

return text
except Exception as e:
# Handle any exceptions that may occur
print("Error reading PDF file:", e)
return None



# Setting page title and header
st.set_page_config(page_icon=":bulb:", page_title="WAAM-GPT")
st.markdown("<div style='text-align: center;'><h1 style='display: inline-block;'> 💡WAAM-GPT</h1><h5 style='display: inline-block; margin-left: 10px; color: gray;'>homework help</h5></div>", unsafe_allow_html=True)

# Set org ID and API key
openai.organization = st.secrets["openai_org"]
openai.api_key = st.secrets["openai_key"]
openai.organization = "empty"
openai.api_key = "empty"

system_prompt = "You are a waam, a helpful large language model STEM tutor created during the 2023 5C Hackathon. You help users learn quantitative skills by guiding them through concepts and practice problems step by step instead of immediately giving away the final answer. Never give a student the direct answer. Always use markdown for your responses. Always render equations using LaTeX."

Expand Down Expand Up @@ -81,6 +99,7 @@
def generate_response(prompt):
st.session_state['messages'].append({"role": "user", "content": prompt})


completion = openai.ChatCompletion.create(
model=model,
messages=st.session_state['messages']
Expand Down Expand Up @@ -108,7 +127,15 @@ def generate_response(prompt):
user_input = st.text_area("", placeholder="What do you want to learn today?", key='input', height=10)
submit_button = st.form_submit_button(label= '⏩')

# create a file uploader for PDFs
pdf_file = st.file_uploader("Upload a PDF file", type="pdf")

# if a PDF file is uploaded, extract its text
if pdf_file is not None:
user_input = pdf_reader(pdf_file)

if submit_button and user_input:

output, total_tokens, prompt_tokens, completion_tokens = generate_response(user_input)
st.session_state['past'].append(user_input)
st.session_state['generated'].append(output)
Expand Down
16 changes: 16 additions & 0 deletions pdfreader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


def pdf_reader(myPdf):
from langchain.document_loaders import PyPDFLoader

loader = PyPDFLoader(myPDF)
pages = loader.load_and_split()
pages[0]

from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings

faiss_index = FAISS.from_documents(pages, OpenAIEmbeddings())
docs = faiss_index.similarity_search("How will the community be engaged?", k=2)
for doc in docs:
print(str(doc.metadata["page"]) + ":", doc.page_content)