Spaces:
Running
Running
Sean Hamill
commited on
Commit
·
74748ba
1
Parent(s):
cddc5c7
app.py
Browse files
app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
import time
|
5 |
+
import shutil
|
6 |
+
from gpt_index import GPTSimpleVectorIndex, SimpleDirectoryReader
|
7 |
+
from threading import Lock
|
8 |
+
from typing import Optional, Tuple
|
9 |
+
from azure.ai.formrecognizer import DocumentAnalysisClient
|
10 |
+
from azure.core.credentials import AzureKeyCredential
|
11 |
+
|
12 |
+
os.environ['OPENAI_API_KEY'] = "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"
|
13 |
+
endpoint = "https://eastus.api.cognitive.microsoft.com/"
|
14 |
+
credential = AzureKeyCredential("844948341c6d4596b77b770cf12e386b")
|
15 |
+
|
16 |
+
form_recognizer_client = DocumentAnalysisClient(endpoint=endpoint, credential=credential)
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
class ChatWrapper:
|
21 |
+
def __init__(self):
|
22 |
+
self.lock = Lock()
|
23 |
+
|
24 |
+
def __call__(self, input, history: Optional[Tuple[str, str]]):
|
25 |
+
self.lock.acquire()
|
26 |
+
try:
|
27 |
+
history = history or []
|
28 |
+
new_index = GPTSimpleVectorIndex.load_from_disk('index.json')
|
29 |
+
|
30 |
+
response = new_index.query(input, verbose=True)
|
31 |
+
history.append((input, str(response)))
|
32 |
+
except Exception as e:
|
33 |
+
|
34 |
+
return gr.HTML(f"Error: {e}")
|
35 |
+
finally:
|
36 |
+
self.lock.release()
|
37 |
+
return history, history
|
38 |
+
|
39 |
+
def make_status_box_visible():
|
40 |
+
return gr.update(visible=True), gr.update(visible=False)
|
41 |
+
|
42 |
+
def create_index():
|
43 |
+
documents = SimpleDirectoryReader('data').load_data()
|
44 |
+
index = GPTSimpleVectorIndex(documents)
|
45 |
+
index.save_to_disk('index.json')
|
46 |
+
|
47 |
+
def pdf_to_text(file_obj, progress=gr.Progress()):
|
48 |
+
progress(0.2, desc="Uploading file...")
|
49 |
+
|
50 |
+
with open(file_obj.name, "rb") as f:
|
51 |
+
progress(0.5, desc="Analyzing file...")
|
52 |
+
poller = form_recognizer_client.begin_analyze_document("prebuilt-document", f)
|
53 |
+
progress(0.8, desc="Applying OCR...")
|
54 |
+
result = poller.result()
|
55 |
+
f.close()
|
56 |
+
progress(0.9, desc="Azure OpenAI Magic...")
|
57 |
+
#save the result.content in a text file
|
58 |
+
with open("data/text.txt", "w") as f:
|
59 |
+
f.write(str(result.content))
|
60 |
+
f.close()
|
61 |
+
create_index()
|
62 |
+
progress(1.0, desc="Done!")
|
63 |
+
time.sleep(1.5)
|
64 |
+
return str(result.content), gr.update(visible=True), gr.update(visible=False)
|
65 |
+
|
66 |
+
chat = ChatWrapper()
|
67 |
+
|
68 |
+
with gr.Blocks(css="footer {visibility: hidden;}") as demo:
|
69 |
+
chat_history_state = gr.State()
|
70 |
+
pdf_content = gr.State()
|
71 |
+
|
72 |
+
gr.Markdown("""
|
73 |
+
<sub><sup>created by [@shamill](https://whoplus.microsoft.com/?_vwp=true&_vwpAlias=SHAMILL)</sup></sub>
|
74 |
+
# Customized GPT-3 Chatbot
|
75 |
+
|
76 |
+
GPT-3.5 is a powerful language model, it can be used to create a chatbot that can have a conversation with you. This demo allows you to customize the context of the conversation, and the chatbot will stick to the confines of the context you provide, avoiding made up answers. The chatbot is powered by Azure's OpenAI GPT-3 API.""")
|
77 |
+
### this is where they will upload the pdf
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
with gr.Column(visible=False) as chat_interface:
|
82 |
+
with gr.Row():
|
83 |
+
chatbot = gr.Chatbot()
|
84 |
+
with gr.Row():
|
85 |
+
message_box = gr.Textbox(lines=2, placeholder="Type a message...", default="Hi there!")
|
86 |
+
submit_button = gr.Button("Submit").style(full_width=False)
|
87 |
+
submit_button.click(chat, inputs=[message_box, chat_history_state], outputs=[chatbot, chat_history_state])
|
88 |
+
with gr.Column(visible=True) as upload_interface:
|
89 |
+
with gr.Row():
|
90 |
+
upload = gr.File(fn=pdf_to_text, label="Upload a context pdf file", type="file")
|
91 |
+
with gr.Row():
|
92 |
+
button = gr.Button("Upload").style(full_width=False)
|
93 |
+
with gr.Row():
|
94 |
+
loadingbox = gr.Textbox("Status", visible=False)
|
95 |
+
button.click(make_status_box_visible, outputs=[loadingbox, button])
|
96 |
+
button.click(pdf_to_text, inputs=[upload], outputs=[loadingbox, chat_interface, upload_interface])
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
demo.queue(concurrency_count=20).launch()
|