Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, List, Tuple
|
2 |
+
import gradio as gr
|
3 |
+
from langchain_openai import OpenAIEmbeddings
|
4 |
+
from langchain_community.vectorstores import Chroma
|
5 |
+
from langchain.chains import ConversationalRetrievalChain
|
6 |
+
from langchain_openai import ChatOpenAI
|
7 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
8 |
+
import fitz
|
9 |
+
from PIL import Image
|
10 |
+
import os
|
11 |
+
import openai
|
12 |
+
|
13 |
+
# MyApp class to handle the processes
|
14 |
+
class MyApp:
|
15 |
+
def __init__(self) -> None:
|
16 |
+
self.OPENAI_API_KEY: str = None # Initialize with None
|
17 |
+
self.chain = None
|
18 |
+
self.chat_history: list = []
|
19 |
+
self.documents = None
|
20 |
+
self.file_name = None
|
21 |
+
|
22 |
+
def set_api_key(self, api_key: str):
|
23 |
+
self.OPENAI_API_KEY = api_key
|
24 |
+
openai.api_key = api_key
|
25 |
+
|
26 |
+
def process_file(self, file) -> Image.Image:
|
27 |
+
loader = PyMuPDFLoader(file.name)
|
28 |
+
self.documents = loader.load()
|
29 |
+
self.file_name = os.path.basename(file.name)
|
30 |
+
doc = fitz.open(file.name)
|
31 |
+
page = doc[0]
|
32 |
+
pix = page.get_pixmap(dpi=150)
|
33 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
34 |
+
return image
|
35 |
+
|
36 |
+
def build_chain(self, file) -> str:
|
37 |
+
embeddings = OpenAIEmbeddings(openai_api_key=self.OPENAI_API_KEY)
|
38 |
+
pdfsearch = Chroma.from_documents(
|
39 |
+
self.documents,
|
40 |
+
embeddings,
|
41 |
+
collection_name=self.file_name,
|
42 |
+
)
|
43 |
+
self.chain = ConversationalRetrievalChain.from_llm(
|
44 |
+
ChatOpenAI(temperature=0.0, openai_api_key=self.OPENAI_API_KEY),
|
45 |
+
retriever=pdfsearch.as_retriever(search_kwargs={"k": 1}),
|
46 |
+
return_source_documents=True,
|
47 |
+
)
|
48 |
+
return "Vector database built successfully!"
|
49 |
+
|
50 |
+
# Function to add text to chat history
|
51 |
+
def add_text(history: List[Tuple[str, str]], text: str) -> List[Tuple[str, str]]:
|
52 |
+
if not text:
|
53 |
+
raise gr.Error("Enter text")
|
54 |
+
history.append((text, ""))
|
55 |
+
return history
|
56 |
+
|
57 |
+
# Function to get response from the model
|
58 |
+
def get_response(history, query):
|
59 |
+
if app.chain is None:
|
60 |
+
raise gr.Error("The chain has not been built yet. Please ensure the vector database is built before querying.")
|
61 |
+
|
62 |
+
try:
|
63 |
+
result = app.chain.invoke(
|
64 |
+
{"question": query, "chat_history": app.chat_history}
|
65 |
+
)
|
66 |
+
app.chat_history.append((query, result["answer"]))
|
67 |
+
source_docs = result["source_documents"]
|
68 |
+
source_texts = []
|
69 |
+
for doc in source_docs:
|
70 |
+
source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
|
71 |
+
source_texts_str = "\n\n".join(source_texts)
|
72 |
+
history[-1] = (history[-1][0], result["answer"])
|
73 |
+
return history, source_texts_str
|
74 |
+
except Exception as e:
|
75 |
+
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
76 |
+
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
77 |
+
|
78 |
+
# Function to get response for the current RAG tab
|
79 |
+
def get_response_current(history, query):
|
80 |
+
if app.chain is None:
|
81 |
+
raise gr.Error("The chain has not been built yet. Please ensure the vector database is built before querying.")
|
82 |
+
|
83 |
+
try:
|
84 |
+
result = app.chain.invoke(
|
85 |
+
{"question": query, "chat_history": app.chat_history}
|
86 |
+
)
|
87 |
+
app.chat_history.append((query, result["answer"]))
|
88 |
+
source_docs = result["source_documents"]
|
89 |
+
source_texts = []
|
90 |
+
for doc in source_docs:
|
91 |
+
source_texts.append(f"Page {doc.metadata['page'] + 1}: {doc.page_content}")
|
92 |
+
source_texts_str = "\n\n".join(source_texts)
|
93 |
+
history[-1] = (history[-1][0], result["answer"])
|
94 |
+
return history, source_texts_str
|
95 |
+
except Exception as e:
|
96 |
+
app.chat_history.append((query, "I have no information about it. Feed me knowledge, please!"))
|
97 |
+
return history, f"I have no information about it. Feed me knowledge, please! Error: {str(e)}"
|
98 |
+
|
99 |
+
# Function to render file
|
100 |
+
def render_file(file) -> Image.Image:
|
101 |
+
doc = fitz.open(file.name)
|
102 |
+
page = doc[0]
|
103 |
+
pix = page.get_pixmap(dpi=150)
|
104 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
105 |
+
return image
|
106 |
+
|
107 |
+
# Function to purge chat and render first page of PDF
|
108 |
+
def purge_chat_and_render_first(file) -> Image.Image:
|
109 |
+
app.chat_history = []
|
110 |
+
doc = fitz.open(file.name)
|
111 |
+
page = doc[0]
|
112 |
+
pix = page.get_pixmap(dpi=150)
|
113 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
114 |
+
return image
|
115 |
+
|
116 |
+
# Function to refresh chat
|
117 |
+
def refresh_chat():
|
118 |
+
app.chat_history = []
|
119 |
+
return []
|
120 |
+
|
121 |
+
app = MyApp()
|
122 |
+
|
123 |
+
# Function to set API key
|
124 |
+
def set_api_key(api_key):
|
125 |
+
app.set_api_key(api_key)
|
126 |
+
# Pre-process the saved PDF file after setting the API key
|
127 |
+
saved_file_path = "THEDIA1.pdf"
|
128 |
+
with open(saved_file_path, 'rb') as saved_file:
|
129 |
+
app.process_file(saved_file)
|
130 |
+
app.build_chain(saved_file)
|
131 |
+
return f"API Key set to {api_key[:4]}...{api_key[-4:]} and vector database built successfully!"
|
132 |
+
|
133 |
+
# List of determined questions
|
134 |
+
questions = [
|
135 |
+
"What is the primary goal of Dialectical Behaviour Therapy?",
|
136 |
+
"How can mindfulness help in managing emotions?",
|
137 |
+
"What are some techniques to handle distressing situations?",
|
138 |
+
"Can you explain the concept of radical acceptance?",
|
139 |
+
"How does DBT differ from other types of therapy?"
|
140 |
+
]
|
141 |
+
|
142 |
+
# Gradio interface
|
143 |
+
with gr.Blocks() as demo:
|
144 |
+
gr.Markdown("🧘♀️ **Dialectical Behaviour Therapy**")
|
145 |
+
gr.Markdown(
|
146 |
+
"Disclaimer: This chatbot is based on a DBT exercise book that is publicly available. "
|
147 |
+
"We are not medical practitioners, and the use of this chatbot is at your own responsibility."
|
148 |
+
)
|
149 |
+
|
150 |
+
api_key_input = gr.Textbox(label="OpenAI API Key", type="password", placeholder="Enter your OpenAI API Key")
|
151 |
+
api_key_btn = gr.Button("Set API Key")
|
152 |
+
api_key_status = gr.Textbox(value="API Key status", interactive=False)
|
153 |
+
|
154 |
+
api_key_btn.click(
|
155 |
+
fn=set_api_key,
|
156 |
+
inputs=[api_key_input],
|
157 |
+
outputs=[api_key_status]
|
158 |
+
)
|
159 |
+
|
160 |
+
with gr.Tab("Take a Dialectical Behaviour Therapy with Me"):
|
161 |
+
with gr.Column():
|
162 |
+
chatbot_current = gr.Chatbot(elem_id="chatbot_current")
|
163 |
+
txt_current = gr.Textbox(
|
164 |
+
show_label=False,
|
165 |
+
placeholder="Enter text and press submit",
|
166 |
+
scale=2
|
167 |
+
)
|
168 |
+
submit_btn_current = gr.Button("Submit", scale=1)
|
169 |
+
refresh_btn_current = gr.Button("Refresh Chat", scale=1)
|
170 |
+
source_texts_output_current = gr.Textbox(label="Source Texts", interactive=False)
|
171 |
+
|
172 |
+
submit_btn_current.click(
|
173 |
+
fn=add_text,
|
174 |
+
inputs=[chatbot_current, txt_current],
|
175 |
+
outputs=[chatbot_current],
|
176 |
+
queue=False,
|
177 |
+
).success(
|
178 |
+
fn=get_response_current, inputs=[chatbot_current, txt_current], outputs=[chatbot_current, source_texts_output_current]
|
179 |
+
)
|
180 |
+
|
181 |
+
refresh_btn_current.click(
|
182 |
+
fn=refresh_chat,
|
183 |
+
inputs=[],
|
184 |
+
outputs=[chatbot_current],
|
185 |
+
)
|
186 |
+
|
187 |
+
with gr.Tab("Questions"):
|
188 |
+
gr.Markdown("### Example Questions")
|
189 |
+
for question in questions:
|
190 |
+
gr.Markdown(f"- {question}")
|
191 |
+
|
192 |
+
demo.queue()
|
193 |
+
demo.launch()
|