bhaskartripathi commited on
Commit
ede4814
1 Parent(s): ed3b881

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -4
app.py CHANGED
@@ -53,6 +53,7 @@ def text_to_chunks(texts, word_length=150, start_page=1):
53
  chunks.append(chunk)
54
  return chunks
55
 
 
56
  class SemanticSearch:
57
 
58
  def __init__(self):
@@ -97,6 +98,7 @@ def load_recommender(path, start_page=1):
97
  recommender.fit(chunks)
98
  return 'Corpus Loaded.'
99
 
 
100
  def generate_text(openAI_key,prompt, engine="text-davinci-003"):
101
  openai.api_key = openAI_key
102
  completions = openai.Completion.create(
@@ -110,6 +112,7 @@ def generate_text(openAI_key,prompt, engine="text-davinci-003"):
110
  message = completions.choices[0].text
111
  return message
112
 
 
113
  def generate_answer(question,openAI_key):
114
  topn_chunks = recommender(question)
115
  prompt = ""
@@ -118,13 +121,13 @@ def generate_answer(question,openAI_key):
118
  prompt += c + '\n\n'
119
 
120
  prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
121
- "Cite each reference using [ Page Number] notation (every result has this number at the beginning). "\
122
  "Citation should be done at the end of each sentence. If the search results mention multiple subjects "\
123
  "with the same name, create separate answers for each. Only include information found in the results and "\
124
  "don't add any additional information. Make sure the answer is correct and don't output false content. "\
125
- "If the text does not relate to the query, simply state 'Text Not Found in PDF'. Ignore outlier "\
126
  "search results which has nothing to do with the question. Only answer what is asked. The "\
127
- "answer should be short and concise. Answer step-by-step. \n\nQuery: {question}\nAnswer: "
128
 
129
  prompt += f"Query: {question}\nAnswer:"
130
  answer = generate_text(openAI_key, prompt,"text-davinci-003")
@@ -163,6 +166,7 @@ recommender = SemanticSearch()
163
  title = 'PDF GPT'
164
  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."""
165
 
 
166
  with gr.Blocks() as demo:
167
 
168
  gr.Markdown(f'<center><h1>{title}</h1></center>')
@@ -185,4 +189,5 @@ with gr.Blocks() as demo:
185
 
186
  btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
187
  #openai.api_key = os.getenv('Your_Key_Here')
188
- demo.launch()
 
 
53
  chunks.append(chunk)
54
  return chunks
55
 
56
+
57
  class SemanticSearch:
58
 
59
  def __init__(self):
 
98
  recommender.fit(chunks)
99
  return 'Corpus Loaded.'
100
 
101
+
102
  def generate_text(openAI_key,prompt, engine="text-davinci-003"):
103
  openai.api_key = openAI_key
104
  completions = openai.Completion.create(
 
112
  message = completions.choices[0].text
113
  return message
114
 
115
+
116
  def generate_answer(question,openAI_key):
117
  topn_chunks = recommender(question)
118
  prompt = ""
 
121
  prompt += c + '\n\n'
122
 
123
  prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
124
+ "Cite each reference using [number] notation (every result has this number at the beginning). "\
125
  "Citation should be done at the end of each sentence. If the search results mention multiple subjects "\
126
  "with the same name, create separate answers for each. Only include information found in the results and "\
127
  "don't add any additional information. Make sure the answer is correct and don't output false content. "\
128
+ "If the text does not relate to the query, simply state 'Found Nothing'. Ignore outlier "\
129
  "search results which has nothing to do with the question. Only answer what is asked. The "\
130
+ "answer should be short and concise.Answer step-by-step. \n\nQuery: {question}\nAnswer: "
131
 
132
  prompt += f"Query: {question}\nAnswer:"
133
  answer = generate_text(openAI_key, prompt,"text-davinci-003")
 
166
  title = 'PDF GPT'
167
  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."""
168
 
169
+
170
  with gr.Blocks() as demo:
171
 
172
  gr.Markdown(f'<center><h1>{title}</h1></center>')
 
189
 
190
  btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
191
  #openai.api_key = os.getenv('Your_Key_Here')
192
+ demo.launch()
193
+