umangchaudhry commited on
Commit
bfabbde
1 Parent(s): 974df2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -33
app.py CHANGED
@@ -1,8 +1,10 @@
 
1
  import openai
2
  import random
3
  import time
4
- import gradio as gr
5
- import os
 
6
  from langchain.embeddings.openai import OpenAIEmbeddings
7
  from langchain.vectorstores import DeepLake
8
  from langchain.chat_models import ChatOpenAI
@@ -13,114 +15,145 @@ from langchain.document_loaders import PyPDFDirectoryLoader
13
  from langchain.memory import ConversationBufferMemory
14
  from langchain.llms import OpenAI
15
 
 
16
  def set_api_key(key):
17
- os.environ["OPENAI_API_KEY"] = key
18
- return f"Your API Key has been set to: {key}"
19
 
 
20
  def reset_api_key():
21
- os.environ["OPENAI_API_KEY"] = ""
22
- return "Your API Key has been reset"
23
 
 
24
  def get_api_key():
25
- api_key = os.getenv("OPENAI_API_KEY")
26
  return api_key
27
 
 
28
  def set_model(model):
29
- os.environ["OPENAI_MODEL"] = model
30
- return f"{model} selected"
31
 
 
32
  def get_model():
33
- model = os.getenv("OPENAI_MODEL")
34
- return model
35
 
 
36
  def upload_file(files):
37
- file_paths = [file.name for file in files]
38
  return file_paths
39
 
 
40
  def create_vectorstore(files):
41
- pdf_dir = files.name
42
- pdf_loader = PyPDFDirectoryLoader(pdf_dir)
43
- pdf_docs = pdf_loader.load_and_split()
44
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
45
- texts = text_splitter.split_documents(pdf_docs)
46
- embeddings = OpenAIEmbeddings()
 
 
47
  db = DeepLake.from_documents(texts, dataset_path="./documentation_db", embedding=embeddings, overwrite=True)
48
- return "Vectorstore Successfully Created"
49
 
 
50
  def respond(message, chat_history):
51
 
52
  # Get embeddings
53
  embeddings = OpenAIEmbeddings()
54
 
55
- #Connect to existing vectorstore
56
  db = DeepLake(dataset_path="./documentation_db", embedding_function=embeddings, read_only=True)
57
- #Set retriever settings
58
  retriever = db.as_retriever(search_kwargs={"distance_metric":'cos',
59
  "fetch_k":10,
60
  "maximal_marginal_relevance":True,
61
  "k":10})
62
 
 
63
  if len(chat_history) != 0:
64
  chat_history = [(chat_history[0][0], chat_history[0][1])]
65
 
 
66
  model = get_model()
67
  # Create ChatOpenAI and ConversationalRetrievalChain
68
  model = ChatOpenAI(model=model)
69
  qa = ConversationalRetrievalChain.from_llm(model, retriever)
70
 
 
71
  bot_message = qa({"question": message, "chat_history": chat_history})
 
72
  chat_history = [(message, bot_message["answer"])]
73
- time.sleep(1)
74
- return "", chat_history
75
 
 
76
  with gr.Blocks() as demo:
77
 
 
78
  gr.Markdown("<h1 style='text-align: center;'>Langchain Coding Assistant</h1>")
79
 
 
80
  gr.Markdown("## This Gradio app is powered by ChatGPT and LangChain. You can submit your OpenAI API key and use the chatbot to get assistance with grant writing. \n ### 1. Enter your OpenAI API key. \n ### 2. Click 'Submit' to set your API key.\n ### 3. Upload the documents that you would like the model to be aware of and then create the vectorstore. Please note that once the vectorstore is created, it will persist and the documents will remain in the vectorstore. \n 4. Provide your prompt.")
81
 
 
82
  with gr.Row():
83
- #create textbox for API input
84
  api_input = gr.Textbox(label = "API Key",
85
  placeholder = "Please provide your OpenAI API key here.")
86
- #create textbox to validate API
87
  api_key_status = gr.Textbox(label = "API Key Status",
88
  placeholder = "Your API Key has not be set yet. Please enter your key.",
89
  interactive = False)
90
- #create button to submit API key
91
  api_submit_button = gr.Button("Submit")
92
- #set api_submit_button functionality
93
  api_submit_button.click(set_api_key, inputs=api_input, outputs=api_key_status)
94
- #create button to reset API key
95
  api_reset_button = gr.Button("Clear API Key from session")
96
- #set api_reset_button functionality
97
  api_reset_button.click(reset_api_key, outputs=api_key_status)
98
 
 
99
  with gr.Row():
100
  with gr.Column():
101
- #create dropdown to select model (gpt-3.5-turbo or gpt4)
102
  model_selection = gr.Dropdown(
103
  ["gpt-3.5-turbo", "gpt-4"], label="Model Selection", info="Please ensure you provide the API Key that corresponds to the Model you select!"
104
  )
105
- #create button to submit model selection
106
  model_submit_button = gr.Button("Submit Model Selection")
 
107
  model_status = gr.Textbox(label = "Selected Model", interactive = False, lines=4)
108
- #set model_submit_button functionality
109
  model_submit_button.click(set_model, inputs=model_selection, outputs=model_status)
110
 
 
111
  file_output = gr.File(label = "Uploaded files - Please note these files are persistent and will not be automatically deleted")
 
112
  upload_button = gr.UploadButton("Click to Upload a PDF File", file_types=["pdf"], file_count="multiple")
 
113
  upload_button.upload(upload_file, upload_button, file_output)
 
114
  create_vectorstore_button = gr.Button("Click to create the vectorstore for your uploaded documents")
 
115
  db_output = gr.Textbox(label = "Vectorstore Status")
 
116
  create_vectorstore_button.click(create_vectorstore, inputs=file_output, outputs = db_output)
117
 
 
118
  chatbot = gr.Chatbot(label="ChatGPT Powered Grant Writing Assistant")
 
119
  msg = gr.Textbox(label="User Prompt", placeholder="Your Query Here")
 
120
  clear = gr.Button("Clear")
121
 
 
122
  msg.submit(respond, inputs = [msg, chatbot], outputs = [msg, chatbot])
 
123
  clear.click(lambda: None, None, chatbot, queue=False)
124
 
125
-
126
  demo.launch()
 
1
+ # Import necessary libraries
2
  import openai
3
  import random
4
  import time
5
+ import gradio as gr # Gradio is a library for creating UIs for ML models
6
+ import os # This module provides functions to interact with the operating system
7
+ # Importing various classes and functions from the langchain package
8
  from langchain.embeddings.openai import OpenAIEmbeddings
9
  from langchain.vectorstores import DeepLake
10
  from langchain.chat_models import ChatOpenAI
 
15
  from langchain.memory import ConversationBufferMemory
16
  from langchain.llms import OpenAI
17
 
18
+ # Function to set the OpenAI API key
19
  def set_api_key(key):
20
+ os.environ["OPENAI_API_KEY"] = key # Sets an environment variable with the key
21
+ return f"Your API Key has been set to: {key}" # Returns a confirmation message
22
 
23
+ # Function to reset the OpenAI API key
24
  def reset_api_key():
25
+ os.environ["OPENAI_API_KEY"] = "" # Clears the environment variable storing the key
26
+ return "Your API Key has been reset" # Returns a confirmation message
27
 
28
+ # Function to get the current OpenAI API key
29
  def get_api_key():
30
+ api_key = os.getenv("OPENAI_API_KEY") # Fetches the value of the environment variable
31
  return api_key
32
 
33
+ # Function to set the model (GPT-3.5-turbo or GPT-4)
34
  def set_model(model):
35
+ os.environ["OPENAI_MODEL"] = model # Sets an environment variable with the model
36
+ return f"{model} selected" # Returns a confirmation message
37
 
38
+ # Function to get the current model
39
  def get_model():
40
+ model = os.getenv("OPENAI_MODEL") # Fetches the value of the environment variable
41
+ return model
42
 
43
+ # Function to get file paths of uploaded files
44
  def upload_file(files):
45
+ file_paths = [file.name for file in files] # List comprehension to get all file paths
46
  return file_paths
47
 
48
+ # Function to create a Vectorstore
49
  def create_vectorstore(files):
50
+ # Vectorstore is a searchable store of vector representations for text passages.
51
+ pdf_dir = files.name # Get the file name
52
+ pdf_loader = PyPDFDirectoryLoader(pdf_dir) # Load the PDFs in the directory
53
+ pdf_docs = pdf_loader.load_and_split() # Load and split the PDFs into sections
54
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) # Set up a text splitter
55
+ texts = text_splitter.split_documents(pdf_docs) # Split the documents into chunks
56
+ embeddings = OpenAIEmbeddings() # Set up the OpenAI embeddings
57
+ # Create the Vectorstore from the documents, using the specified path, embeddings, and overwrite if it exists.
58
  db = DeepLake.from_documents(texts, dataset_path="./documentation_db", embedding=embeddings, overwrite=True)
59
+ return "Vectorstore Successfully Created" # Returns a confirmation message
60
 
61
+ # Function to generate a response given a user's message and previous chat history
62
  def respond(message, chat_history):
63
 
64
  # Get embeddings
65
  embeddings = OpenAIEmbeddings()
66
 
67
+ # Connect to existing Vectorstore
68
  db = DeepLake(dataset_path="./documentation_db", embedding_function=embeddings, read_only=True)
69
+ # Set retriever settings
70
  retriever = db.as_retriever(search_kwargs={"distance_metric":'cos',
71
  "fetch_k":10,
72
  "maximal_marginal_relevance":True,
73
  "k":10})
74
 
75
+ # Check if chat history is not empty
76
  if len(chat_history) != 0:
77
  chat_history = [(chat_history[0][0], chat_history[0][1])]
78
 
79
+ # Get model
80
  model = get_model()
81
  # Create ChatOpenAI and ConversationalRetrievalChain
82
  model = ChatOpenAI(model=model)
83
  qa = ConversationalRetrievalChain.from_llm(model, retriever)
84
 
85
+ # Generate a bot message
86
  bot_message = qa({"question": message, "chat_history": chat_history})
87
+ # Update chat history
88
  chat_history = [(message, bot_message["answer"])]
89
+ time.sleep(1) # Wait for a second to simulate real-time interaction
90
+ return "", chat_history # Return updated chat history
91
 
92
+ # Start building the Gradio UI
93
  with gr.Blocks() as demo:
94
 
95
+ # Write some HTML for a header
96
  gr.Markdown("<h1 style='text-align: center;'>Langchain Coding Assistant</h1>")
97
 
98
+ # Write some explanatory text
99
  gr.Markdown("## This Gradio app is powered by ChatGPT and LangChain. You can submit your OpenAI API key and use the chatbot to get assistance with grant writing. \n ### 1. Enter your OpenAI API key. \n ### 2. Click 'Submit' to set your API key.\n ### 3. Upload the documents that you would like the model to be aware of and then create the vectorstore. Please note that once the vectorstore is created, it will persist and the documents will remain in the vectorstore. \n 4. Provide your prompt.")
100
 
101
+ # Set up a row of UI elements
102
  with gr.Row():
103
+ # Create a textbox for API key input
104
  api_input = gr.Textbox(label = "API Key",
105
  placeholder = "Please provide your OpenAI API key here.")
106
+ # Create a non-interactive textbox to display API key status
107
  api_key_status = gr.Textbox(label = "API Key Status",
108
  placeholder = "Your API Key has not be set yet. Please enter your key.",
109
  interactive = False)
110
+ # Create a button to submit API key
111
  api_submit_button = gr.Button("Submit")
112
+ # Set the button to call set_api_key when clicked, updating the API key status
113
  api_submit_button.click(set_api_key, inputs=api_input, outputs=api_key_status)
114
+ # Create a button to reset API key
115
  api_reset_button = gr.Button("Clear API Key from session")
116
+ # Set the button to call reset_api_key when clicked, updating the API key status
117
  api_reset_button.click(reset_api_key, outputs=api_key_status)
118
 
119
+ # Set up a row of UI elements
120
  with gr.Row():
121
  with gr.Column():
122
+ # Create a dropdown to select a model
123
  model_selection = gr.Dropdown(
124
  ["gpt-3.5-turbo", "gpt-4"], label="Model Selection", info="Please ensure you provide the API Key that corresponds to the Model you select!"
125
  )
126
+ # Create a button to submit model selection
127
  model_submit_button = gr.Button("Submit Model Selection")
128
+ # Create a non-interactive textbox to display model selection status
129
  model_status = gr.Textbox(label = "Selected Model", interactive = False, lines=4)
130
+ # Set the button to call set_model when clicked, updating the model status
131
  model_submit_button.click(set_model, inputs=model_selection, outputs=model_status)
132
 
133
+ # Create a File output UI element for displaying uploaded files
134
  file_output = gr.File(label = "Uploaded files - Please note these files are persistent and will not be automatically deleted")
135
+ # Create an Upload button for PDF file(s)
136
  upload_button = gr.UploadButton("Click to Upload a PDF File", file_types=["pdf"], file_count="multiple")
137
+ # Set the button to call upload_file when clicked, updating the file_output with the uploaded files
138
  upload_button.upload(upload_file, upload_button, file_output)
139
+ # Create a button to create the vectorstore
140
  create_vectorstore_button = gr.Button("Click to create the vectorstore for your uploaded documents")
141
+ # Create a textbox to display Vectorstore status
142
  db_output = gr.Textbox(label = "Vectorstore Status")
143
+ # Set the button to call create_vectorstore when clicked, updating the Vectorstore status
144
  create_vectorstore_button.click(create_vectorstore, inputs=file_output, outputs = db_output)
145
 
146
+ # Create a Chatbot UI element
147
  chatbot = gr.Chatbot(label="ChatGPT Powered Grant Writing Assistant")
148
+ # Create a textbox for user's prompt
149
  msg = gr.Textbox(label="User Prompt", placeholder="Your Query Here")
150
+ # Create a button to clear the chat history
151
  clear = gr.Button("Clear")
152
 
153
+ # Set the textbox to call respond when submitted, updating the chatbot with the response
154
  msg.submit(respond, inputs = [msg, chatbot], outputs = [msg, chatbot])
155
+ # Set the button to clear the chat history when clicked
156
  clear.click(lambda: None, None, chatbot, queue=False)
157
 
158
+ # Launch the Gradio interface
159
  demo.launch()