Update app.py
Browse files
app.py
CHANGED
@@ -105,24 +105,28 @@ def load_sample_pdf():
|
|
105 |
|
106 |
return "PDF(s) indexed successfully!"
|
107 |
|
|
|
108 |
|
109 |
def format_docs(docs):
|
110 |
return "\n\n".join(doc.page_content for doc in docs)
|
111 |
-
|
112 |
|
113 |
def generate_response(query, history, model, temperature, max_tokens, top_p, seed):
|
|
|
|
|
114 |
if vector_store is None:
|
115 |
return "Please upload and index a PDF at the Indexing tab."
|
116 |
|
117 |
if seed == 0:
|
118 |
seed = random.randint(1, 100000)
|
119 |
|
|
|
120 |
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 16})
|
121 |
llm = ChatGroq(groq_api_key=os.environ.get("GROQ_API_KEY"), model=model)
|
122 |
custom_rag_prompt = PromptTemplate.from_template(template)
|
123 |
|
124 |
docs = retriever.invoke(query)
|
125 |
-
relevant_info = format_docs(docs)
|
|
|
126 |
|
127 |
rag_chain = (
|
128 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
@@ -135,9 +139,8 @@ def generate_response(query, history, model, temperature, max_tokens, top_p, see
|
|
135 |
|
136 |
return relevant_info
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
|
142 |
additional_inputs = [
|
143 |
gr.Dropdown(choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant", "llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma2-9b-it"], value="gemma2-9b-it", label="Model"),
|
@@ -148,7 +151,8 @@ additional_inputs = [
|
|
148 |
]
|
149 |
|
150 |
# Create the Gradio interface
|
151 |
-
with gr.Blocks(theme=
|
|
|
152 |
with gr.Tab("Indexing"):
|
153 |
gr.Markdown(desc)
|
154 |
# pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
@@ -171,12 +175,17 @@ with gr.Blocks(theme="Nymbo/Alyx_Theme") as demo:
|
|
171 |
# additional_outputs=[relevant_info],
|
172 |
cache_examples=False,
|
173 |
)
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
181 |
# Launch the Gradio app
|
182 |
demo.launch(share=True)
|
|
|
105 |
|
106 |
return "PDF(s) indexed successfully!"
|
107 |
|
108 |
+
last_relevant_info_state = gr.State("")
|
109 |
|
110 |
def format_docs(docs):
|
111 |
return "\n\n".join(doc.page_content for doc in docs)
|
|
|
112 |
|
113 |
def generate_response(query, history, model, temperature, max_tokens, top_p, seed):
|
114 |
+
global last_relevant_info_state # Declare global to modify the state value
|
115 |
+
|
116 |
if vector_store is None:
|
117 |
return "Please upload and index a PDF at the Indexing tab."
|
118 |
|
119 |
if seed == 0:
|
120 |
seed = random.randint(1, 100000)
|
121 |
|
122 |
+
last_relevant_info_state.value = ""
|
123 |
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 16})
|
124 |
llm = ChatGroq(groq_api_key=os.environ.get("GROQ_API_KEY"), model=model)
|
125 |
custom_rag_prompt = PromptTemplate.from_template(template)
|
126 |
|
127 |
docs = retriever.invoke(query)
|
128 |
+
relevant_info = format_docs(docs)
|
129 |
+
last_relevant_info_state.value = relevant_info
|
130 |
|
131 |
rag_chain = (
|
132 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
|
|
139 |
|
140 |
return relevant_info
|
141 |
|
142 |
+
def get_relevant_info(state):
|
143 |
+
return state # The state's value is what we want to display
|
|
|
144 |
|
145 |
additional_inputs = [
|
146 |
gr.Dropdown(choices=["llama-3.3-70b-versatile", "llama-3.1-8b-instant", "llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma2-9b-it"], value="gemma2-9b-it", label="Model"),
|
|
|
151 |
]
|
152 |
|
153 |
# Create the Gradio interface
|
154 |
+
with gr.Blocks(theme=gr.themes.Default()) as demo:
|
155 |
+
gr_last_relevant_info_state = gr.State("")
|
156 |
with gr.Tab("Indexing"):
|
157 |
gr.Markdown(desc)
|
158 |
# pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
|
|
175 |
# additional_outputs=[relevant_info],
|
176 |
cache_examples=False,
|
177 |
)
|
178 |
+
with gr.Column():
|
179 |
+
retrieve_button = gr.Button("Retrieve Relevant Info")
|
180 |
+
relevant_info = gr.Textbox(
|
181 |
+
label="Retrieved Information",
|
182 |
+
interactive=False,
|
183 |
+
lines=20,
|
184 |
+
)
|
185 |
+
retrieve_button.click(
|
186 |
+
fn=get_relevant_info,
|
187 |
+
inputs=[gr_last_relevant_info_state], # Input is the state component
|
188 |
+
outputs=[relevant_info] # Output updates the relevant_info textbox
|
189 |
+
|
190 |
# Launch the Gradio app
|
191 |
demo.launch(share=True)
|