add tab 'pdf' to parse pdf
Browse files- app.py +48 -0
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import openai
|
| 3 |
import os
|
|
@@ -53,6 +54,38 @@ def respond(chat_history, message, system_message, key_txt, url_txt, model, temp
|
|
| 53 |
result = chat_history + [[message, response]]
|
| 54 |
return result
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
with gr.Tab("Config"):
|
| 58 |
with gr.Row():
|
|
@@ -80,6 +113,21 @@ with gr.Blocks() as demo:
|
|
| 80 |
[chatbot, message, system_message, key_txt, url_txt, model, temperature],
|
| 81 |
chatbot,
|
| 82 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
demo.launch()
|
|
|
|
| 1 |
+
from PyPDF2 import PdfReader
|
| 2 |
import gradio as gr
|
| 3 |
import openai
|
| 4 |
import os
|
|
|
|
| 54 |
result = chat_history + [[message, response]]
|
| 55 |
return result
|
| 56 |
|
| 57 |
+
def parse_pdf(prompt, pdfs, system_message, key_txt, url_txt, model, temperature):
|
| 58 |
+
result = ""
|
| 59 |
+
full_text = ""
|
| 60 |
+
for pdf in pdfs:
|
| 61 |
+
print("parse: ", pdf)
|
| 62 |
+
text = ""
|
| 63 |
+
reader = PdfReader(pdf.name)
|
| 64 |
+
for page in reader.pages:
|
| 65 |
+
text = text + page.extract_text()
|
| 66 |
+
full_text = text + "\n----------\n"
|
| 67 |
+
messages = [
|
| 68 |
+
{"role": "system", "content": system_message},
|
| 69 |
+
{"role": "user", "content": prompt + "\n\n###\n\n + full_text}
|
| 70 |
+
]
|
| 71 |
+
openai.api_key = key_txt if key_txt else api_key_from_config
|
| 72 |
+
if url_txt:
|
| 73 |
+
openai.api_base = url_txt
|
| 74 |
+
if DEBUG:
|
| 75 |
+
print("messages:", messages)
|
| 76 |
+
print("model:", model)
|
| 77 |
+
print("temperature:", temperature)
|
| 78 |
+
completion = openai.ChatCompletion.create(
|
| 79 |
+
model=model,
|
| 80 |
+
messages=messages,
|
| 81 |
+
temperature=temperature,
|
| 82 |
+
)
|
| 83 |
+
if DEBUG:
|
| 84 |
+
print("completion:", completion)
|
| 85 |
+
response = completion['choices'][0]['message']['content']
|
| 86 |
+
|
| 87 |
+
return response
|
| 88 |
+
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
with gr.Tab("Config"):
|
| 91 |
with gr.Row():
|
|
|
|
| 113 |
[chatbot, message, system_message, key_txt, url_txt, model, temperature],
|
| 114 |
chatbot,
|
| 115 |
)
|
| 116 |
+
with gr.Tab("PDF"):
|
| 117 |
+
gr.Markdown("## Parse PDF with GPT")
|
| 118 |
+
prompt = gr.Text(label="Prompt")
|
| 119 |
+
pdfs = gr.File(label="Upload PDF", file_count="multiple", file_types=[".pdf"])
|
| 120 |
+
markdown = gr.Markdown(label="Output")
|
| 121 |
+
with gr.Row():
|
| 122 |
+
clear = gr.Button("Clear")
|
| 123 |
+
clear.click(lambda: None, None, markdown)
|
| 124 |
+
submit = gr.Button("Upload")
|
| 125 |
+
submit.click(
|
| 126 |
+
parse_pdf,
|
| 127 |
+
[prompt, pdfs, system_message, key_txt, url_txt, model, temperature],
|
| 128 |
+
markdown
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
openai
|
| 2 |
gradio
|
|
|
|
|
|
| 1 |
openai
|
| 2 |
gradio
|
| 3 |
+
PyPDF2
|