Spaces:
Runtime error
Runtime error
Commit
·
f82a1c7
1
Parent(s):
9297d37
Update app.py
Browse files
app.py
CHANGED
|
@@ -100,14 +100,26 @@ def load_recommender(path, start_page=1):
|
|
| 100 |
|
| 101 |
def generate_text(openAI_key, prompt, model="gpt-3.5-turbo"):
|
| 102 |
openai.api_key = openAI_key
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
def generate_answer(question, openAI_key):
|
| 113 |
topn_chunks = recommender(question)
|
|
@@ -129,22 +141,19 @@ def generate_answer(question, openAI_key):
|
|
| 129 |
answer = generate_text(openAI_key, prompt, "gpt-3.5-turbo")
|
| 130 |
return answer
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def question_answer(url, file, question,openAI_key):
|
| 135 |
if openAI_key.strip()=='':
|
| 136 |
return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
|
| 137 |
if url.strip() == '' and file == None:
|
| 138 |
-
return '[ERROR]: Both URL and PDF is empty. Provide
|
| 139 |
|
| 140 |
if url.strip() != '' and file != None:
|
| 141 |
-
return '[ERROR]: Both URL and PDF is provided. Please provide only one (
|
| 142 |
|
| 143 |
if url.strip() != '':
|
| 144 |
glob_url = url
|
| 145 |
download_pdf(glob_url, 'corpus.pdf')
|
| 146 |
load_recommender('corpus.pdf')
|
| 147 |
-
|
| 148 |
else:
|
| 149 |
old_file_name = file.name
|
| 150 |
file_name = file.name
|
|
@@ -155,7 +164,45 @@ def question_answer(url, file, question,openAI_key):
|
|
| 155 |
if question.strip() == '':
|
| 156 |
return '[ERROR]: Question field is empty'
|
| 157 |
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
recommender = SemanticSearch()
|
|
@@ -178,13 +225,15 @@ with gr.Blocks() as demo:
|
|
| 178 |
gr.Markdown("<center><h4>OR<h4></center>")
|
| 179 |
file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
|
| 180 |
question = gr.Textbox(label='Enter your question here')
|
|
|
|
| 181 |
btn = gr.Button(value='Submit')
|
| 182 |
btn.style(full_width=True)
|
| 183 |
|
| 184 |
with gr.Group():
|
| 185 |
answer = gr.Textbox(label='The answer to your question is :')
|
| 186 |
|
| 187 |
-
btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
|
|
|
|
| 188 |
#openai.api_key = os.getenv('Your_Key_Here')
|
| 189 |
demo.launch()
|
| 190 |
|
|
|
|
| 100 |
|
| 101 |
def generate_text(openAI_key, prompt, model="gpt-3.5-turbo"):
|
| 102 |
openai.api_key = openAI_key
|
| 103 |
+
if model == "text-davinci-003":
|
| 104 |
+
completions = openai.Completion.create(
|
| 105 |
+
engine=model,
|
| 106 |
+
prompt=prompt,
|
| 107 |
+
max_tokens=512,
|
| 108 |
+
n=1,
|
| 109 |
+
stop=None,
|
| 110 |
+
temperature=0.7,
|
| 111 |
+
)
|
| 112 |
+
message = completions.choices[0].text
|
| 113 |
+
else:
|
| 114 |
+
message = openai.ChatCompletion.create(
|
| 115 |
+
model=model,
|
| 116 |
+
messages=[
|
| 117 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 118 |
+
{"role": "user", "content": prompt}
|
| 119 |
+
]
|
| 120 |
+
).choices[0].message['content']
|
| 121 |
+
return message
|
| 122 |
+
|
| 123 |
|
| 124 |
def generate_answer(question, openAI_key):
|
| 125 |
topn_chunks = recommender(question)
|
|
|
|
| 141 |
answer = generate_text(openAI_key, prompt, "gpt-3.5-turbo")
|
| 142 |
return answer
|
| 143 |
|
| 144 |
+
def question_answer(url, file, question, openAI_key, model):
|
|
|
|
|
|
|
| 145 |
if openAI_key.strip()=='':
|
| 146 |
return '[ERROR]: Please enter you Open AI Key. Get your key here : https://platform.openai.com/account/api-keys'
|
| 147 |
if url.strip() == '' and file == None:
|
| 148 |
+
return '[ERROR]: Both URL and PDF is empty. Provide at least one.'
|
| 149 |
|
| 150 |
if url.strip() != '' and file != None:
|
| 151 |
+
return '[ERROR]: Both URL and PDF is provided. Please provide only one (either URL or PDF).'
|
| 152 |
|
| 153 |
if url.strip() != '':
|
| 154 |
glob_url = url
|
| 155 |
download_pdf(glob_url, 'corpus.pdf')
|
| 156 |
load_recommender('corpus.pdf')
|
|
|
|
| 157 |
else:
|
| 158 |
old_file_name = file.name
|
| 159 |
file_name = file.name
|
|
|
|
| 164 |
if question.strip() == '':
|
| 165 |
return '[ERROR]: Question field is empty'
|
| 166 |
|
| 167 |
+
if model == "text-davinci-003":
|
| 168 |
+
return generate_answer_text-davinci-003(question, openAI_key)
|
| 169 |
+
else:
|
| 170 |
+
return generate_answer(question, openAI_key, model)
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
def generate_text_text_davinci_003(openAI_key,prompt, engine="text-davinci-003"):
|
| 174 |
+
openai.api_key = openAI_key
|
| 175 |
+
completions = openai.Completion.create(
|
| 176 |
+
engine=engine,
|
| 177 |
+
prompt=prompt,
|
| 178 |
+
max_tokens=512,
|
| 179 |
+
n=1,
|
| 180 |
+
stop=None,
|
| 181 |
+
temperature=0.7,
|
| 182 |
+
)
|
| 183 |
+
message = completions.choices[0].text
|
| 184 |
+
return message
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def generate_answer_text_davinci_003(question,openAI_key):
|
| 188 |
+
topn_chunks = recommender(question)
|
| 189 |
+
prompt = ""
|
| 190 |
+
prompt += 'search results:\n\n'
|
| 191 |
+
for c in topn_chunks:
|
| 192 |
+
prompt += c + '\n\n'
|
| 193 |
+
|
| 194 |
+
prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
|
| 195 |
+
"Cite each reference using [ Page Number] notation (every result has this number at the beginning). "\
|
| 196 |
+
"Citation should be done at the end of each sentence. If the search results mention multiple subjects "\
|
| 197 |
+
"with the same name, create separate answers for each. Only include information found in the results and "\
|
| 198 |
+
"don't add any additional information. Make sure the answer is correct and don't output false content. "\
|
| 199 |
+
"If the text does not relate to the query, simply state 'Found Nothing'. Ignore outlier "\
|
| 200 |
+
"search results which has nothing to do with the question. Only answer what is asked. The "\
|
| 201 |
+
"answer should be short and concise. \n\nQuery: {question}\nAnswer: "
|
| 202 |
+
|
| 203 |
+
prompt += f"Query: {question}\nAnswer:"
|
| 204 |
+
answer = generate_text(openAI_key, prompt,"text-davinci-003")
|
| 205 |
+
return answer
|
| 206 |
|
| 207 |
|
| 208 |
recommender = SemanticSearch()
|
|
|
|
| 225 |
gr.Markdown("<center><h4>OR<h4></center>")
|
| 226 |
file = gr.File(label='Upload your PDF/ Research Paper / Book here', file_types=['.pdf'])
|
| 227 |
question = gr.Textbox(label='Enter your question here')
|
| 228 |
+
model = gr.Radio(['gpt-3.5-turbo', 'gpt-3.5-turbo-16k', 'gpt-3.5-turbo-0613', 'gpt-3.5-turbo-16k-0613', 'text-davinci-003'], label='Select Model', default='gpt-3.5-turbo')
|
| 229 |
btn = gr.Button(value='Submit')
|
| 230 |
btn.style(full_width=True)
|
| 231 |
|
| 232 |
with gr.Group():
|
| 233 |
answer = gr.Textbox(label='The answer to your question is :')
|
| 234 |
|
| 235 |
+
#btn.click(question_answer, inputs=[url, file, question,openAI_key], outputs=[answer])
|
| 236 |
+
btn.click(question_answer, inputs=[url, file, question, openAI_key, model], outputs=[answer])
|
| 237 |
#openai.api_key = os.getenv('Your_Key_Here')
|
| 238 |
demo.launch()
|
| 239 |
|