Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
@@ -1,181 +1,156 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import logging
|
3 |
-
import threading
|
4 |
-
import time
|
5 |
-
from generator.compute_metrics import get_attributes_text
|
6 |
-
from generator.generate_metrics import generate_metrics, retrieve_and_generate_response
|
7 |
-
from config import AppConfig, ConfigConstants
|
8 |
-
from generator.initialize_llm import initialize_generation_llm, initialize_validation_llm
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
def
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
with
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
outputs=[attr_output, metrics_output]
|
158 |
-
)
|
159 |
-
|
160 |
-
update_gen_llm_button.click(
|
161 |
-
fn=reinitialize_gen_llm,
|
162 |
-
inputs=[new_gen_llm_input],
|
163 |
-
outputs=[model_info_display] # Update the displayed model info
|
164 |
-
)
|
165 |
-
|
166 |
-
update_val_llm_button.click(
|
167 |
-
fn=reinitialize_val_llm,
|
168 |
-
inputs=[new_val_llm_input],
|
169 |
-
outputs=[model_info_display] # Update the displayed model info
|
170 |
-
)
|
171 |
-
|
172 |
-
# Section to display logs
|
173 |
-
with gr.Row():
|
174 |
-
start_log_button = gr.Button("Start Log Update", elem_id="start_btn") # Button to start log updates
|
175 |
-
with gr.Row():
|
176 |
-
log_section = gr.Textbox(label="Logs", interactive=False, visible=True, lines=10) # Log section
|
177 |
-
|
178 |
-
# Set button click to trigger log updates
|
179 |
-
start_log_button.click(fn=get_logs, outputs=log_section)
|
180 |
-
|
181 |
-
interface.launch(share=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import logging
|
3 |
+
import threading
|
4 |
+
import time
|
5 |
+
from generator.compute_metrics import get_attributes_text
|
6 |
+
from generator.generate_metrics import generate_metrics, retrieve_and_generate_response
|
7 |
+
from config import AppConfig, ConfigConstants
|
8 |
+
from generator.initialize_llm import initialize_generation_llm, initialize_validation_llm
|
9 |
+
from generator.document_utils import get_logs, initialize_logging
|
10 |
+
|
11 |
+
def launch_gradio(config : AppConfig):
|
12 |
+
"""
|
13 |
+
Launch the Gradio app with pre-initialized objects.
|
14 |
+
"""
|
15 |
+
initialize_logging()
|
16 |
+
|
17 |
+
def update_logs_periodically():
|
18 |
+
while True:
|
19 |
+
time.sleep(2) # Wait for 2 seconds
|
20 |
+
yield get_logs()
|
21 |
+
|
22 |
+
def answer_question(query, state):
|
23 |
+
try:
|
24 |
+
# Generate response using the passed objects
|
25 |
+
response, source_docs = retrieve_and_generate_response(config.gen_llm, config.vector_store, query)
|
26 |
+
|
27 |
+
# Update state with the response and source documents
|
28 |
+
state["query"] = query
|
29 |
+
state["response"] = response
|
30 |
+
state["source_docs"] = source_docs
|
31 |
+
|
32 |
+
response_text = f"Response: {response}\n\n"
|
33 |
+
return response_text, state
|
34 |
+
except Exception as e:
|
35 |
+
logging.error(f"Error processing query: {e}")
|
36 |
+
return f"An error occurred: {e}", state
|
37 |
+
|
38 |
+
def compute_metrics(state):
|
39 |
+
try:
|
40 |
+
logging.info(f"Computing metrics")
|
41 |
+
|
42 |
+
# Retrieve response and source documents from state
|
43 |
+
response = state.get("response", "")
|
44 |
+
source_docs = state.get("source_docs", {})
|
45 |
+
query = state.get("query", "")
|
46 |
+
|
47 |
+
# Generate metrics using the passed objects
|
48 |
+
attributes, metrics = generate_metrics(config.val_llm, response, source_docs, query, 1)
|
49 |
+
|
50 |
+
attributes_text = get_attributes_text(attributes)
|
51 |
+
|
52 |
+
metrics_text = "Metrics:\n"
|
53 |
+
for key, value in metrics.items():
|
54 |
+
if key != 'response':
|
55 |
+
metrics_text += f"{key}: {value}\n"
|
56 |
+
|
57 |
+
return attributes_text, metrics_text
|
58 |
+
except Exception as e:
|
59 |
+
logging.error(f"Error computing metrics: {e}")
|
60 |
+
return f"An error occurred: {e}", ""
|
61 |
+
|
62 |
+
def reinitialize_llm(model_type, model_name):
|
63 |
+
"""Reinitialize the specified LLM (generation or validation) and return updated model info."""
|
64 |
+
if model_name.strip(): # Only update if input is not empty
|
65 |
+
if model_type == "generation":
|
66 |
+
config.gen_llm = initialize_generation_llm(model_name)
|
67 |
+
elif model_type == "validation":
|
68 |
+
config.val_llm = initialize_validation_llm(model_name)
|
69 |
+
|
70 |
+
return get_updated_model_info()
|
71 |
+
|
72 |
+
def get_updated_model_info():
|
73 |
+
"""Generate and return the updated model information string."""
|
74 |
+
return (
|
75 |
+
f"Embedding Model: {ConfigConstants.EMBEDDING_MODEL_NAME}\n"
|
76 |
+
f"Generation LLM: {config.gen_llm.name if hasattr(config.gen_llm, 'name') else 'Unknown'}\n"
|
77 |
+
f"Validation LLM: {config.val_llm.name if hasattr(config.val_llm, 'name') else 'Unknown'}\n"
|
78 |
+
)
|
79 |
+
|
80 |
+
# Wrappers for event listeners
|
81 |
+
def reinitialize_gen_llm(gen_llm_name):
|
82 |
+
return reinitialize_llm("generation", gen_llm_name)
|
83 |
+
|
84 |
+
def reinitialize_val_llm(val_llm_name):
|
85 |
+
return reinitialize_llm("validation", val_llm_name)
|
86 |
+
|
87 |
+
# Define Gradio Blocks layout
|
88 |
+
with gr.Blocks() as interface:
|
89 |
+
interface.title = "Real Time RAG Pipeline Q&A"
|
90 |
+
gr.Markdown("# Real Time RAG Pipeline Q&A") # Heading
|
91 |
+
|
92 |
+
# Textbox for new generation LLM name
|
93 |
+
with gr.Row():
|
94 |
+
new_gen_llm_input = gr.Dropdown(
|
95 |
+
label="Generation Model",
|
96 |
+
choices=ConfigConstants.GENERATION_MODELS, # Directly use the list
|
97 |
+
value=ConfigConstants.GENERATION_MODELS[0] if ConfigConstants.GENERATION_MODELS else None, # First value dynamically
|
98 |
+
interactive=True
|
99 |
+
)
|
100 |
+
|
101 |
+
new_val_llm_input = gr.Dropdown(
|
102 |
+
label="Validation Model",
|
103 |
+
choices=ConfigConstants.VALIDATION_MODELS, # Directly use the list
|
104 |
+
value=ConfigConstants.VALIDATION_MODELS[0] if ConfigConstants.VALIDATION_MODELS else None, # First value dynamically
|
105 |
+
interactive=True
|
106 |
+
)
|
107 |
+
|
108 |
+
model_info_display = gr.Textbox(
|
109 |
+
value=get_updated_model_info(), # Use the helper function
|
110 |
+
label="System Information",
|
111 |
+
interactive=False # Read-only textbox
|
112 |
+
)
|
113 |
+
|
114 |
+
# State to store response and source documents
|
115 |
+
state = gr.State(value={"query": "","response": "", "source_docs": {}})
|
116 |
+
gr.Markdown("Ask a question and get a response with metrics calculated from the RAG pipeline.") # Description
|
117 |
+
with gr.Row():
|
118 |
+
query_input = gr.Textbox(label="Ask a question", placeholder="Type your query here")
|
119 |
+
with gr.Row():
|
120 |
+
submit_button = gr.Button("Submit", variant="primary", scale = 0) # Submit button
|
121 |
+
clear_query_button = gr.Button("Clear", scale = 0) # Clear button
|
122 |
+
with gr.Row():
|
123 |
+
answer_output = gr.Textbox(label="Response", placeholder="Response will appear here")
|
124 |
+
|
125 |
+
with gr.Row():
|
126 |
+
compute_metrics_button = gr.Button("Compute metrics", variant="primary" , scale = 0)
|
127 |
+
attr_output = gr.Textbox(label="Attributes", placeholder="Attributes will appear here")
|
128 |
+
metrics_output = gr.Textbox(label="Metrics", placeholder="Metrics will appear here")
|
129 |
+
|
130 |
+
#with gr.Row():
|
131 |
+
# Attach event listeners to update model info on change
|
132 |
+
new_gen_llm_input.change(reinitialize_gen_llm, inputs=new_gen_llm_input, outputs=model_info_display)
|
133 |
+
new_val_llm_input.change(reinitialize_val_llm, inputs=new_val_llm_input, outputs=model_info_display)
|
134 |
+
|
135 |
+
# Define button actions
|
136 |
+
submit_button.click(
|
137 |
+
fn=answer_question,
|
138 |
+
inputs=[query_input, state],
|
139 |
+
outputs=[answer_output, state]
|
140 |
+
)
|
141 |
+
clear_query_button.click(fn=lambda: "", outputs=[query_input]) # Clear query input
|
142 |
+
compute_metrics_button.click(
|
143 |
+
fn=compute_metrics,
|
144 |
+
inputs=[state],
|
145 |
+
outputs=[attr_output, metrics_output]
|
146 |
+
)
|
147 |
+
|
148 |
+
# Section to display logs
|
149 |
+
with gr.Row():
|
150 |
+
log_section = gr.Textbox(label="Logs", interactive=False, visible=True, lines=10 , every=2) # Log section
|
151 |
+
|
152 |
+
# Update UI when logs_state changes
|
153 |
+
interface.queue()
|
154 |
+
interface.load(update_logs_periodically, outputs=log_section)
|
155 |
+
|
156 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|