bhaskartripathi commited on
Commit
3aae3ba
1 Parent(s): e0a4aa4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -11
app.py CHANGED
@@ -103,7 +103,7 @@ def load_recommender(directory_path, start_page=1):
103
  recommender.fit(chunks)
104
  return 'Corpus Loaded.'
105
 
106
- def generate_text(openAI_key,prompt, engine="text-davinci-003"):
107
  openai.api_key = openAI_key
108
  completions = openai.Completion.create(
109
  engine=engine,
@@ -116,7 +116,7 @@ def generate_text(openAI_key,prompt, engine="text-davinci-003"):
116
  message = completions.choices[0].text
117
  return message
118
 
119
- def generate_answer(question,openAI_key):
120
  topn_chunks = recommender(question)
121
  prompt = ""
122
  prompt += 'search results:\n\n'
@@ -133,7 +133,7 @@ def generate_answer(question,openAI_key):
133
  "answer should be short and concise. Answer step-by-step. \n\nQuery: {question}\nAnswer: "
134
 
135
  prompt += f"Query: {question}\nAnswer:"
136
- answer = generate_text(openAI_key, prompt,"text-davinci-003")
137
  return answer
138
 
139
 
@@ -149,7 +149,8 @@ def question_answer(files, question, openAI_key):
149
  os.makedirs(directory_path)
150
 
151
  for file in files:
152
- file.save(os.path.join(directory_path, file.name))
 
153
 
154
  load_recommender(directory_path)
155
 
@@ -159,6 +160,7 @@ def question_answer(files, question, openAI_key):
159
  return generate_answer(question, openAI_key)
160
 
161
 
 
162
  recommender = SemanticSearch()
163
 
164
  title = 'PDF GPT'
@@ -170,17 +172,19 @@ with gr.Blocks() as demo:
170
  gr.Markdown(description)
171
 
172
  with gr.Row():
173
-
174
  with gr.Group():
175
  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>')
176
- openAI_key = gr.Textbox(label='Enter your OpenAI API key here')
177
- files = gr.Filebox(label='Upload PDF files', accept='.pdf', multiple=True)
178
- question = gr.Textbox(label='Enter your question here')
179
  btn = gr.Button(value='Submit')
180
 
181
  with gr.Group():
182
- answer = gr.Textbox(label='The answer to your question is :')
183
 
184
  btn.click(question_answer, inputs=[files, question, openAI_key], outputs=[answer])
185
- #openai.api_key = os.getenv('Your_Key_Here')
186
- demo.launch()
 
 
 
103
  recommender.fit(chunks)
104
  return 'Corpus Loaded.'
105
 
106
+ def generate_text(openAI_key, prompt, engine="text-davinci-003"):
107
  openai.api_key = openAI_key
108
  completions = openai.Completion.create(
109
  engine=engine,
 
116
  message = completions.choices[0].text
117
  return message
118
 
119
+ def generate_answer(question, openAI_key):
120
  topn_chunks = recommender(question)
121
  prompt = ""
122
  prompt += 'search results:\n\n'
 
133
  "answer should be short and concise. Answer step-by-step. \n\nQuery: {question}\nAnswer: "
134
 
135
  prompt += f"Query: {question}\nAnswer:"
136
+ answer = generate_text(openAI_key, prompt, "text-davinci-003")
137
  return answer
138
 
139
 
 
149
  os.makedirs(directory_path)
150
 
151
  for file in files:
152
+ with open(os.path.join(directory_path, file.name), 'wb') as f:
153
+ f.write(file.read())
154
 
155
  load_recommender(directory_path)
156
 
 
160
  return generate_answer(question, openAI_key)
161
 
162
 
163
+
164
  recommender = SemanticSearch()
165
 
166
  title = 'PDF GPT'
 
172
  gr.Markdown(description)
173
 
174
  with gr.Row():
175
+
176
  with gr.Group():
177
  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>')
178
+ openAI_key = gr.inputs.Textbox(label='Enter your OpenAI API key here')
179
+ files = gr.inputs.File(file_count="multiple", file_types=[".pdf"], label="Upload PDF files")
180
+ question = gr.inputs.Textbox(label='Enter your question here')
181
  btn = gr.Button(value='Submit')
182
 
183
  with gr.Group():
184
+ answer = gr.outputs.Textbox(label='The answer to your question is :')
185
 
186
  btn.click(question_answer, inputs=[files, question, openAI_key], outputs=[answer])
187
+
188
+ demo.launch()
189
+
190
+