Upload app.py
Browse files
app.py
CHANGED
@@ -1,243 +1,244 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import logging
|
3 |
-
import time
|
4 |
-
from generator.compute_metrics import get_attributes_text
|
5 |
-
from generator.generate_metrics import generate_metrics, retrieve_and_generate_response
|
6 |
-
from config import AppConfig, ConfigConstants
|
7 |
-
from generator.initialize_llm import initialize_generation_llm, initialize_validation_llm
|
8 |
-
from generator.document_utils import get_logs, initialize_logging
|
9 |
-
from retriever.load_selected_datasets import load_selected_datasets
|
10 |
-
|
11 |
-
def launch_gradio(config : AppConfig):
|
12 |
-
"""
|
13 |
-
Launch the Gradio app with pre-initialized objects.
|
14 |
-
"""
|
15 |
-
initialize_logging()
|
16 |
-
|
17 |
-
# **🔹 Always get the latest loaded datasets**
|
18 |
-
config.detect_loaded_datasets()
|
19 |
-
|
20 |
-
def update_logs_periodically():
|
21 |
-
while True:
|
22 |
-
time.sleep(2) # Wait for 2 seconds
|
23 |
-
yield get_logs()
|
24 |
-
|
25 |
-
def answer_question(query, state):
|
26 |
-
try:
|
27 |
-
# Ensure vector store is updated before use
|
28 |
-
if config.vector_store is None:
|
29 |
-
return "Please load a dataset first.", state
|
30 |
-
|
31 |
-
# Generate response using the passed objects
|
32 |
-
response, source_docs = retrieve_and_generate_response(config.gen_llm, config.vector_store, query)
|
33 |
-
|
34 |
-
# Update state with the response and source documents
|
35 |
-
state["query"] = query
|
36 |
-
state["response"] = response
|
37 |
-
state["source_docs"] = source_docs
|
38 |
-
|
39 |
-
response_text = f"Response from Model : {response}\n\n"
|
40 |
-
return response_text, state
|
41 |
-
except Exception as e:
|
42 |
-
logging.error(f"Error processing query: {e}")
|
43 |
-
return f"An error occurred: {e}", state
|
44 |
-
|
45 |
-
def compute_metrics(state):
|
46 |
-
try:
|
47 |
-
logging.info(f"Computing metrics")
|
48 |
-
|
49 |
-
# Retrieve response and source documents from state
|
50 |
-
response = state.get("response", "")
|
51 |
-
source_docs = state.get("source_docs", {})
|
52 |
-
query = state.get("query", "")
|
53 |
-
|
54 |
-
# Generate metrics using the passed objects
|
55 |
-
attributes, metrics = generate_metrics(config.val_llm, response, source_docs, query, 1)
|
56 |
-
|
57 |
-
attributes_text = get_attributes_text(attributes)
|
58 |
-
|
59 |
-
metrics_text = ""
|
60 |
-
for key, value in metrics.items():
|
61 |
-
if key != 'response':
|
62 |
-
metrics_text += f"{key}: {value}\n"
|
63 |
-
|
64 |
-
return attributes_text, metrics_text
|
65 |
-
except Exception as e:
|
66 |
-
logging.error(f"Error computing metrics: {e}")
|
67 |
-
return f"An error occurred: {e}", ""
|
68 |
-
|
69 |
-
def reinitialize_llm(model_type, model_name):
|
70 |
-
"""Reinitialize the specified LLM (generation or validation) and return updated model info."""
|
71 |
-
if model_name.strip(): # Only update if input is not empty
|
72 |
-
if model_type == "generation":
|
73 |
-
config.gen_llm = initialize_generation_llm(model_name)
|
74 |
-
elif model_type == "validation":
|
75 |
-
config.val_llm = initialize_validation_llm(model_name)
|
76 |
-
|
77 |
-
return get_updated_model_info()
|
78 |
-
|
79 |
-
def get_updated_model_info():
|
80 |
-
loaded_datasets_str = ", ".join(config.loaded_datasets) if config.loaded_datasets else "None"
|
81 |
-
"""Generate and return the updated model information string."""
|
82 |
-
return (
|
83 |
-
f"Embedding Model: {ConfigConstants.EMBEDDING_MODEL_NAME}\n"
|
84 |
-
f"Generation LLM: {config.gen_llm.name if hasattr(config.gen_llm, 'name') else 'Unknown'}\n"
|
85 |
-
f"Re-ranking LLM: {ConfigConstants.RE_RANKER_MODEL_NAME}\n"
|
86 |
-
f"Validation LLM: {config.val_llm.name if hasattr(config.val_llm, 'name') else 'Unknown'}\n"
|
87 |
-
f"Loaded Datasets: {loaded_datasets_str}\n"
|
88 |
-
)
|
89 |
-
|
90 |
-
# Wrappers for event listeners
|
91 |
-
def reinitialize_gen_llm(gen_llm_name):
|
92 |
-
return reinitialize_llm("generation", gen_llm_name)
|
93 |
-
|
94 |
-
def reinitialize_val_llm(val_llm_name):
|
95 |
-
return reinitialize_llm("validation", val_llm_name)
|
96 |
-
|
97 |
-
# Function to update query input when a question is selected from the dropdown
|
98 |
-
def update_query_input(selected_question):
|
99 |
-
return selected_question
|
100 |
-
|
101 |
-
# Define Gradio Blocks layout
|
102 |
-
with gr.Blocks() as interface:
|
103 |
-
interface.title = "Real Time RAG Pipeline Q&A"
|
104 |
-
gr.Markdown("""
|
105 |
-
# Real Time RAG Pipeline Q&A
|
106 |
-
The **Retrieval-Augmented Generation (RAG) Pipeline** combines retrieval-based and generative AI models to provide accurate and context-aware answers to your questions.
|
107 |
-
It retrieves relevant documents from a dataset (e.g., COVIDQA, TechQA, FinQA) and uses a generative model to synthesize a response.
|
108 |
-
Metrics are computed to evaluate the quality of the response and the retrieval process.
|
109 |
-
""")
|
110 |
-
# Model Configuration
|
111 |
-
with gr.Accordion("System Information", open=False):
|
112 |
-
with gr.Accordion("DataSet", open=False):
|
113 |
-
with gr.Row():
|
114 |
-
dataset_selector = gr.CheckboxGroup(ConfigConstants.DATA_SET_NAMES, label="Select Datasets to Load")
|
115 |
-
load_button = gr.Button("Load", scale= 0)
|
116 |
-
|
117 |
-
with gr.Row():
|
118 |
-
# Column for Generation Model Dropdown
|
119 |
-
with gr.Column(scale=1):
|
120 |
-
new_gen_llm_input = gr.Dropdown(
|
121 |
-
label="Generation Model",
|
122 |
-
choices=ConfigConstants.GENERATION_MODELS,
|
123 |
-
value=ConfigConstants.GENERATION_MODELS[0] if ConfigConstants.GENERATION_MODELS else None,
|
124 |
-
interactive=True,
|
125 |
-
info="Select the generative model for response generation."
|
126 |
-
)
|
127 |
-
|
128 |
-
# Column for Validation Model Dropdown
|
129 |
-
with gr.Column(scale=1):
|
130 |
-
new_val_llm_input = gr.Dropdown(
|
131 |
-
label="Validation Model",
|
132 |
-
choices=ConfigConstants.VALIDATION_MODELS,
|
133 |
-
value=ConfigConstants.VALIDATION_MODELS[0] if ConfigConstants.VALIDATION_MODELS else None,
|
134 |
-
interactive=True,
|
135 |
-
info="Select the model for validating the response quality."
|
136 |
-
)
|
137 |
-
|
138 |
-
# Column for Model Information
|
139 |
-
with gr.Column(scale=2):
|
140 |
-
model_info_display = gr.Textbox(
|
141 |
-
value=get_updated_model_info(), # Use the helper function
|
142 |
-
label="Model Configuration",
|
143 |
-
interactive=False, # Read-only textbox
|
144 |
-
lines=5
|
145 |
-
)
|
146 |
-
|
147 |
-
# Query Section
|
148 |
-
gr.Markdown("Ask a question and get a response with metrics calculated from the RAG pipeline.")
|
149 |
-
all_questions = [
|
150 |
-
"
|
151 |
-
"
|
152 |
-
"Is one party required to deposit its source code into escrow with a third party, which can be released to the counterparty upon the occurrence of certain events (bankruptcy, insolvency, etc.)?",
|
153 |
-
"Explain the concept of blockchain.",
|
154 |
-
"What is the capital of France?",
|
155 |
-
"Do Surface Porosity and Pore Size Influence Mechanical Properties and Cellular Response to PEEK??",
|
156 |
-
"How does a vaccine work?",
|
157 |
-
"
|
158 |
-
"What are the risk factors for heart disease?",
|
159 |
-
"What is the
|
160 |
-
# Add more questions as needed
|
161 |
-
]
|
162 |
-
|
163 |
-
# Subset of questions to display as examples
|
164 |
-
example_questions = [
|
165 |
-
"When was the first case of COVID-19 identified?",
|
166 |
-
"What are the ages of the patients in this study?",
|
167 |
-
"
|
168 |
-
"Explain the concept of blockchain.",
|
169 |
-
"What is the capital of France?"
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
interface.
|
241 |
-
interface.load(
|
242 |
-
|
|
|
243 |
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import logging
|
3 |
+
import time
|
4 |
+
from generator.compute_metrics import get_attributes_text
|
5 |
+
from generator.generate_metrics import generate_metrics, retrieve_and_generate_response
|
6 |
+
from config import AppConfig, ConfigConstants
|
7 |
+
from generator.initialize_llm import initialize_generation_llm, initialize_validation_llm
|
8 |
+
from generator.document_utils import get_logs, initialize_logging
|
9 |
+
from retriever.load_selected_datasets import load_selected_datasets
|
10 |
+
|
11 |
+
def launch_gradio(config : AppConfig):
|
12 |
+
"""
|
13 |
+
Launch the Gradio app with pre-initialized objects.
|
14 |
+
"""
|
15 |
+
initialize_logging()
|
16 |
+
|
17 |
+
# **🔹 Always get the latest loaded datasets**
|
18 |
+
config.detect_loaded_datasets()
|
19 |
+
|
20 |
+
def update_logs_periodically():
|
21 |
+
while True:
|
22 |
+
time.sleep(2) # Wait for 2 seconds
|
23 |
+
yield get_logs()
|
24 |
+
|
25 |
+
def answer_question(query, state):
|
26 |
+
try:
|
27 |
+
# Ensure vector store is updated before use
|
28 |
+
if config.vector_store is None:
|
29 |
+
return "Please load a dataset first.", state
|
30 |
+
|
31 |
+
# Generate response using the passed objects
|
32 |
+
response, source_docs = retrieve_and_generate_response(config.gen_llm, config.vector_store, query)
|
33 |
+
|
34 |
+
# Update state with the response and source documents
|
35 |
+
state["query"] = query
|
36 |
+
state["response"] = response
|
37 |
+
state["source_docs"] = source_docs
|
38 |
+
|
39 |
+
response_text = f"Response from Model ({config.gen_llm.name}) : {response}\n\n"
|
40 |
+
return response_text, state
|
41 |
+
except Exception as e:
|
42 |
+
logging.error(f"Error processing query: {e}")
|
43 |
+
return f"An error occurred: {e}", state
|
44 |
+
|
45 |
+
def compute_metrics(state):
|
46 |
+
try:
|
47 |
+
logging.info(f"Computing metrics")
|
48 |
+
|
49 |
+
# Retrieve response and source documents from state
|
50 |
+
response = state.get("response", "")
|
51 |
+
source_docs = state.get("source_docs", {})
|
52 |
+
query = state.get("query", "")
|
53 |
+
|
54 |
+
# Generate metrics using the passed objects
|
55 |
+
attributes, metrics = generate_metrics(config.val_llm, response, source_docs, query, 1)
|
56 |
+
|
57 |
+
attributes_text = get_attributes_text(attributes)
|
58 |
+
|
59 |
+
metrics_text = ""
|
60 |
+
for key, value in metrics.items():
|
61 |
+
if key != 'response':
|
62 |
+
metrics_text += f"{key}: {value}\n"
|
63 |
+
|
64 |
+
return attributes_text, metrics_text
|
65 |
+
except Exception as e:
|
66 |
+
logging.error(f"Error computing metrics: {e}")
|
67 |
+
return f"An error occurred: {e}", ""
|
68 |
+
|
69 |
+
def reinitialize_llm(model_type, model_name):
|
70 |
+
"""Reinitialize the specified LLM (generation or validation) and return updated model info."""
|
71 |
+
if model_name.strip(): # Only update if input is not empty
|
72 |
+
if model_type == "generation":
|
73 |
+
config.gen_llm = initialize_generation_llm(model_name)
|
74 |
+
elif model_type == "validation":
|
75 |
+
config.val_llm = initialize_validation_llm(model_name)
|
76 |
+
|
77 |
+
return get_updated_model_info()
|
78 |
+
|
79 |
+
def get_updated_model_info():
|
80 |
+
loaded_datasets_str = ", ".join(config.loaded_datasets) if config.loaded_datasets else "None"
|
81 |
+
"""Generate and return the updated model information string."""
|
82 |
+
return (
|
83 |
+
f"Embedding Model: {ConfigConstants.EMBEDDING_MODEL_NAME}\n"
|
84 |
+
f"Generation LLM: {config.gen_llm.name if hasattr(config.gen_llm, 'name') else 'Unknown'}\n"
|
85 |
+
f"Re-ranking LLM: {ConfigConstants.RE_RANKER_MODEL_NAME}\n"
|
86 |
+
f"Validation LLM: {config.val_llm.name if hasattr(config.val_llm, 'name') else 'Unknown'}\n"
|
87 |
+
f"Loaded Datasets: {loaded_datasets_str}\n"
|
88 |
+
)
|
89 |
+
|
90 |
+
# Wrappers for event listeners
|
91 |
+
def reinitialize_gen_llm(gen_llm_name):
|
92 |
+
return reinitialize_llm("generation", gen_llm_name)
|
93 |
+
|
94 |
+
def reinitialize_val_llm(val_llm_name):
|
95 |
+
return reinitialize_llm("validation", val_llm_name)
|
96 |
+
|
97 |
+
# Function to update query input when a question is selected from the dropdown
|
98 |
+
def update_query_input(selected_question):
|
99 |
+
return selected_question
|
100 |
+
|
101 |
+
# Define Gradio Blocks layout
|
102 |
+
with gr.Blocks() as interface:
|
103 |
+
interface.title = "Real Time RAG Pipeline Q&A"
|
104 |
+
gr.Markdown("""
|
105 |
+
# Real Time RAG Pipeline Q&A
|
106 |
+
The **Retrieval-Augmented Generation (RAG) Pipeline** combines retrieval-based and generative AI models to provide accurate and context-aware answers to your questions.
|
107 |
+
It retrieves relevant documents from a dataset (e.g., COVIDQA, TechQA, FinQA) and uses a generative model to synthesize a response.
|
108 |
+
Metrics are computed to evaluate the quality of the response and the retrieval process.
|
109 |
+
""")
|
110 |
+
# Model Configuration
|
111 |
+
with gr.Accordion("System Information", open=False):
|
112 |
+
with gr.Accordion("DataSet", open=False):
|
113 |
+
with gr.Row():
|
114 |
+
dataset_selector = gr.CheckboxGroup(ConfigConstants.DATA_SET_NAMES, label="Select Datasets to Load")
|
115 |
+
load_button = gr.Button("Load", scale= 0)
|
116 |
+
|
117 |
+
with gr.Row():
|
118 |
+
# Column for Generation Model Dropdown
|
119 |
+
with gr.Column(scale=1):
|
120 |
+
new_gen_llm_input = gr.Dropdown(
|
121 |
+
label="Generation Model",
|
122 |
+
choices=ConfigConstants.GENERATION_MODELS,
|
123 |
+
value=ConfigConstants.GENERATION_MODELS[0] if ConfigConstants.GENERATION_MODELS else None,
|
124 |
+
interactive=True,
|
125 |
+
info="Select the generative model for response generation."
|
126 |
+
)
|
127 |
+
|
128 |
+
# Column for Validation Model Dropdown
|
129 |
+
with gr.Column(scale=1):
|
130 |
+
new_val_llm_input = gr.Dropdown(
|
131 |
+
label="Validation Model",
|
132 |
+
choices=ConfigConstants.VALIDATION_MODELS,
|
133 |
+
value=ConfigConstants.VALIDATION_MODELS[0] if ConfigConstants.VALIDATION_MODELS else None,
|
134 |
+
interactive=True,
|
135 |
+
info="Select the model for validating the response quality."
|
136 |
+
)
|
137 |
+
|
138 |
+
# Column for Model Information
|
139 |
+
with gr.Column(scale=2):
|
140 |
+
model_info_display = gr.Textbox(
|
141 |
+
value=get_updated_model_info(), # Use the helper function
|
142 |
+
label="Model Configuration",
|
143 |
+
interactive=False, # Read-only textbox
|
144 |
+
lines=5
|
145 |
+
)
|
146 |
+
|
147 |
+
# Query Section
|
148 |
+
gr.Markdown("Ask a question and get a response with metrics calculated from the RAG pipeline.")
|
149 |
+
all_questions = [
|
150 |
+
"Does the ignition button have multiple modes?",
|
151 |
+
"Why does the other instance of my multi-instance qmgr seem to hang after a failover? Queue manager will not start after failover.",
|
152 |
+
"Is one party required to deposit its source code into escrow with a third party, which can be released to the counterparty upon the occurrence of certain events (bankruptcy, insolvency, etc.)?",
|
153 |
+
"Explain the concept of blockchain.",
|
154 |
+
"What is the capital of France?",
|
155 |
+
"Do Surface Porosity and Pore Size Influence Mechanical Properties and Cellular Response to PEEK??",
|
156 |
+
"How does a vaccine work?",
|
157 |
+
"Tell me the step-by-step instruction for front-door installation.",
|
158 |
+
"What are the risk factors for heart disease?",
|
159 |
+
"What is the % change in total property and equipment from 2018 to 2019?",
|
160 |
+
# Add more questions as needed
|
161 |
+
]
|
162 |
+
|
163 |
+
# Subset of questions to display as examples
|
164 |
+
example_questions = [
|
165 |
+
"When was the first case of COVID-19 identified?",
|
166 |
+
"What are the ages of the patients in this study?",
|
167 |
+
"Why cant I load and AEL when using IE 11 JRE 8 Application Blocked by Java Security",
|
168 |
+
"Explain the concept of blockchain.",
|
169 |
+
"What is the capital of France?",
|
170 |
+
"What was the change in Current deferred income?"
|
171 |
+
]
|
172 |
+
with gr.Row():
|
173 |
+
with gr.Column():
|
174 |
+
with gr.Row():
|
175 |
+
query_input = gr.Textbox(
|
176 |
+
label="Ask a question ",
|
177 |
+
placeholder="Type your query here or select from examples/dropdown",
|
178 |
+
lines=2
|
179 |
+
)
|
180 |
+
with gr.Row():
|
181 |
+
submit_button = gr.Button("Submit", variant="primary", scale=0)
|
182 |
+
clear_query_button = gr.Button("Clear", scale=0)
|
183 |
+
with gr.Column():
|
184 |
+
gr.Examples(
|
185 |
+
examples=example_questions, # Make sure the variable name matches
|
186 |
+
inputs=query_input,
|
187 |
+
label="Try these examples:"
|
188 |
+
)
|
189 |
+
question_dropdown = gr.Dropdown(
|
190 |
+
label="",
|
191 |
+
choices=all_questions,
|
192 |
+
interactive=True,
|
193 |
+
info="Choose a question from the dropdown to populate the query box."
|
194 |
+
)
|
195 |
+
|
196 |
+
# Attach event listener to dropdown
|
197 |
+
question_dropdown.change(
|
198 |
+
fn=update_query_input,
|
199 |
+
inputs=question_dropdown,
|
200 |
+
outputs=query_input
|
201 |
+
)
|
202 |
+
|
203 |
+
# Response and Metrics
|
204 |
+
with gr.Row():
|
205 |
+
answer_output = gr.Textbox(label="Response", placeholder="Response will appear here", lines=2)
|
206 |
+
|
207 |
+
with gr.Row():
|
208 |
+
compute_metrics_button = gr.Button("Compute metrics", variant="primary" , scale = 0)
|
209 |
+
attr_output = gr.Textbox(label="Attributes", placeholder="Attributes will appear here")
|
210 |
+
metrics_output = gr.Textbox(label="Metrics", placeholder="Metrics will appear here")
|
211 |
+
|
212 |
+
# State to store response and source documents
|
213 |
+
state = gr.State(value={"query": "","response": "", "source_docs": {}})
|
214 |
+
|
215 |
+
# Pass config to update vector store
|
216 |
+
load_button.click(lambda datasets: (load_selected_datasets(datasets, config), get_updated_model_info()), inputs=dataset_selector, outputs=model_info_display)
|
217 |
+
# Attach event listeners to update model info on change
|
218 |
+
new_gen_llm_input.change(reinitialize_gen_llm, inputs=new_gen_llm_input, outputs=model_info_display)
|
219 |
+
new_val_llm_input.change(reinitialize_val_llm, inputs=new_val_llm_input, outputs=model_info_display)
|
220 |
+
|
221 |
+
# Define button actions
|
222 |
+
submit_button.click(
|
223 |
+
fn=answer_question,
|
224 |
+
inputs=[query_input, state],
|
225 |
+
outputs=[answer_output, state]
|
226 |
+
)
|
227 |
+
clear_query_button.click(fn=lambda: "", outputs=[query_input]) # Clear query input
|
228 |
+
compute_metrics_button.click(
|
229 |
+
fn=compute_metrics,
|
230 |
+
inputs=[state],
|
231 |
+
outputs=[attr_output, metrics_output]
|
232 |
+
)
|
233 |
+
|
234 |
+
# Section to display logs
|
235 |
+
with gr.Accordion("View Live Logs", open=False):
|
236 |
+
with gr.Row():
|
237 |
+
log_section = gr.Textbox(label="Logs", interactive=False, visible=True, lines=10 , every=2) # Log section
|
238 |
+
|
239 |
+
# Update UI when logs_state changes
|
240 |
+
interface.queue()
|
241 |
+
interface.load(update_logs_periodically, outputs=log_section)
|
242 |
+
interface.load(get_updated_model_info, outputs=model_info_display)
|
243 |
+
|
244 |
interface.launch()
|