joshuadunlop commited on
Commit
0f98abb
·
1 Parent(s): afb85a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -51
app.py CHANGED
@@ -1,11 +1,10 @@
 
1
  import urllib.request
2
  import fitz
3
  import re
4
  import numpy as np
5
  import tensorflow_hub as hub
6
  import openai
7
- import gradio as gr
8
- import os
9
  from sklearn.neighbors import NearestNeighbors
10
 
11
  def download_pdf(url, output_path):
@@ -122,62 +121,37 @@ def generate_answer(question,openAI_key):
122
  answer = generate_text(openAI_key, prompt,"text-davinci-003")
123
  return answer
124
 
125
- def question_answer(url, file, question,openAI_key):
126
- if openAI_key.strip()=='':
127
- return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
128
- if url.strip() == '' and file == None:
129
- return '[ERROR]: Both URL and PDF is empty. Provide atleast one.'
130
-
131
- if url.strip() != '' and file != None:
132
- return '[ERROR]: Both URL and PDF is provided. Please provide only one (eiter URL or PDF).'
133
 
134
- if url.strip() != '':
 
 
 
 
 
 
 
 
 
 
 
 
135
  glob_url = url
136
  download_pdf(glob_url, 'corpus.pdf')
137
  load_recommender('corpus.pdf')
138
-
 
139
  else:
140
  old_file_name = file.name
141
  file_name = file.name
142
  file_name = file_name[:-12] + file_name[-4:]
143
  os.rename(old_file_name, file_name)
144
  load_recommender(file_name)
145
-
146
- if question.strip() == '':
147
- return '[ERROR]: Question field is empty'
148
-
149
- return generate_answer(question,openAI_key)
150
-
151
- recommender = SemanticSearch()
152
-
153
- title = 'PDF GPT'
154
- description = """ PDF GPT allows you to chat with your PDF file using Universal Sentence Encoder and Open AI. It gives hallucination free response than other tools as the embeddings are better than OpenAI. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly."""
155
-
156
- openAI_key=gr.Textbox(label='Enter your OpenAI API key here')
157
-
158
- with gr.Blocks() as demo:
159
-
160
- gr.Markdown(f'<center><h1>{title}</h1></center>')
161
- gr.Markdown(description)
162
-
163
- with gr.Row():
164
-
165
- with gr.Group():
166
- gr.Markdown(f'<p style="text-align:center">Get your Open AI API key <a href="https://platform.openai.com/account/api-keys">here</a></p>')
167
- url = gr.Textbox(label='Enter PDF URL here')
168
- gr.Markdown("<center><h4>OR<h4></center>")
169
- file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
170
- question = gr.Textbox(label='Enter your question here')
171
- btn = gr.Button(value='Submit')
172
- btn.style(full_width=True)
173
-
174
- with gr.Group():
175
- answer = gr.Textbox(label='The answer to your question is :')
176
-
177
- btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
178
-
179
- with gr.Sidebar():
180
- gr.Interface(inputs=openAI_key)
181
-
182
- #openai.api_key = os.getenv('Your_Key_Here')
183
- demo.launch()
 
1
+ import streamlit as st
2
  import urllib.request
3
  import fitz
4
  import re
5
  import numpy as np
6
  import tensorflow_hub as hub
7
  import openai
 
 
8
  from sklearn.neighbors import NearestNeighbors
9
 
10
  def download_pdf(url, output_path):
 
121
  answer = generate_text(openAI_key, prompt,"text-davinci-003")
122
  return answer
123
 
124
+ recommender = SemanticSearch()
125
+
126
+ st.title('PDF GPT')
127
+
128
+ description = """ PDF GPT allows you to chat with your PDF file using Universal Sentence Encoder and Open AI. It gives hallucination free response than other tools as the embeddings are better than OpenAI. The returned response can even cite the page number in square brackets([]) where the information is located, adding credibility to the responses and helping to locate pertinent information quickly."""
129
+
130
+ st.markdown(description)
 
131
 
132
+ openAI_key = st.text_input('Enter your OpenAI API key here')
133
+ url = st.text_input('Enter PDF URL here')
134
+ file = st.file_uploader('Upload your PDF/ Research Paper / Book here', type=['pdf'])
135
+ question = st.text_input('Enter your question here')
136
+
137
+ if st.button('Submit'):
138
+ if openAI_key.strip()=='':
139
+ st.error('Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys')
140
+ elif url.strip() == '' and file == None:
141
+ st.error('Both URL and PDF is empty. Provide atleast one.')
142
+ elif url.strip() != '' and file != None:
143
+ st.error('Both URL and PDF is provided. Please provide only one (eiter URL or PDF).')
144
+ elif url.strip() != '':
145
  glob_url = url
146
  download_pdf(glob_url, 'corpus.pdf')
147
  load_recommender('corpus.pdf')
148
+ elif question.strip() == '':
149
+ st.error('Question field is empty')
150
  else:
151
  old_file_name = file.name
152
  file_name = file.name
153
  file_name = file_name[:-12] + file_name[-4:]
154
  os.rename(old_file_name, file_name)
155
  load_recommender(file_name)
156
+ answer = generate_answer(question,openAI_key)
157
+ st.text_area('The answer to your question is :', value=answer, height=200)