Spaces:
Sleeping
Sleeping
File size: 9,850 Bytes
55b0a04 |
1 2 3 4 5 6 7 8 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 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 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 158 159 160 161 162 163 164 165 166 167 168 169 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
import gradio as gr
from dotenv import load_dotenv
# Import from other modules
from ibfs import start_ibfs, handle_choice
from zero_shot import start_zero_shot
# Load environment variables
load_dotenv()
# Define dependent variables for user evaluation
DV_QUESTIONS = [
"How satisfied are you with the answer? (1-5)",
"How clear was the explanation? (1-5)",
"How relevant was the answer to your query? (1-5)",
"How confident are you in the accuracy of the answer? (1-5)",
"Would you use this method again for similar questions? (Yes/No)"
]
def save_dv_responses(user_id, method, responses):
"""Save user's responses to dependent variable questions."""
from utils import save_results
import json
from datetime import datetime
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"ibfs_results/{user_id}_{method}_dv_{timestamp}.json"
with open(filename, "w") as f:
json.dump(responses, f, indent=2)
return filename
def process_dv_responses(state, *responses):
"""Process and save the user's responses to dependent variables."""
if not state:
return "No active session"
user_id = state.get("user_id", "unknown")
method = state.get("method", "unknown")
# Create a dictionary of responses
response_dict = {
"user_id": user_id,
"method": method,
"responses": {}
}
for i, response in enumerate(responses):
response_dict["responses"][f"question_{i + 1}"] = {
"question": DV_QUESTIONS[i],
"response": response
}
# Save responses
save_path = save_dv_responses(user_id, method, response_dict)
return f"Thank you for your feedback! Responses saved to {save_path}"
def create_ibfs_interface():
"""Create the IBFS tab interface."""
with gr.Column():
gr.Markdown("# IBFS")
gr.Markdown("Enter your query and set parameters to explore different strategies.")
with gr.Row():
with gr.Column(scale=3):
ibfs_query_input = gr.Textbox(
label="Query",
placeholder="Enter your question here...",
lines=3
)
with gr.Column(scale=1):
k_slider = gr.Slider(
minimum=2,
maximum=5,
step=1,
value=3,
label="k (Branching Factor - options per step)"
)
m_slider = gr.Slider(
minimum=1,
maximum=3,
step=1,
value=2,
label="m (Depth - number of iterations)"
)
ibfs_start_btn = gr.Button("Start IBFS Process", variant="primary")
# State for maintaining context between steps
ibfs_state = gr.State(None)
# Chat display
ibfs_chatbot = gr.Chatbot(
label="Interactive Search Process",
height=600,
type="messages"
)
# User choice input for selecting strategies
ibfs_choice_input = gr.Textbox(
label="Enter the number of your choice (e.g., 1, 2, 3...)",
placeholder="Type a number and press Enter",
lines=1
)
# Event handlers for IBFS
ibfs_start_btn.click(
fn=start_ibfs,
inputs=[ibfs_query_input, k_slider, m_slider],
outputs=[ibfs_state, ibfs_chatbot],
show_progress="minimal"
)
ibfs_choice_input.submit(
fn=handle_choice,
inputs=[ibfs_state, ibfs_choice_input],
outputs=[ibfs_state, ibfs_chatbot],
show_progress="minimal"
)
ibfs_choice_input.submit(
fn=lambda: "",
inputs=[],
outputs=[ibfs_choice_input]
)
# Dependent Variables Section (initially hidden)
with gr.Accordion("Evaluation Questions", open=False, visible=False) as ibfs_dv_accordion:
ibfs_dv_inputs = []
for i, question in enumerate(DV_QUESTIONS):
if "1-5" in question:
dv_input = gr.Slider(
minimum=1,
maximum=5,
step=1,
value=3,
label=question
)
elif "Yes/No" in question:
dv_input = gr.Radio(
choices=["Yes", "No"],
label=question
)
else:
dv_input = gr.Textbox(
label=question
)
ibfs_dv_inputs.append(dv_input)
ibfs_submit_dv_btn = gr.Button("Submit Evaluation", variant="primary")
ibfs_dv_result = gr.Markdown("")
# Function to show DV accordion after final answer is generated
def show_dv_section(state):
if state and state.get("current_step", 0) == 0 and state.get("strategy_path", []):
# This means we've completed a cycle and have a final answer
state["method"] = "ibfs" # Add method to state for DV processing
return gr.update(visible=True, open=True)
return gr.update(visible=False, open=False)
# Update visibility of DV section when state changes
ibfs_state.change(
fn=show_dv_section,
inputs=[ibfs_state],
outputs=[ibfs_dv_accordion]
)
# Handle DV submission
ibfs_submit_dv_btn.click(
fn=process_dv_responses,
inputs=[ibfs_state] + ibfs_dv_inputs,
outputs=ibfs_dv_result
)
return (ibfs_query_input, k_slider, m_slider, ibfs_start_btn,
ibfs_state, ibfs_chatbot, ibfs_choice_input)
def create_zero_shot_interface():
"""Create the Zero-Shot tab interface."""
with gr.Column():
gr.Markdown("# Zero-Shot Direct Answer")
gr.Markdown("Enter your query to get a direct answer without interactive exploration.")
zero_query_input = gr.Textbox(
label="Query",
placeholder="Enter your question here...",
lines=3
)
zero_shot_btn = gr.Button("Get Direct Answer", variant="primary")
# State for maintaining context for zero-shot
zero_state = gr.State(None)
# Chat display
zero_chatbot = gr.Chatbot(
label="Direct Answer",
height=600,
type="messages"
)
# Modified zero-shot function to return state
def zero_shot_with_state(query):
"""Wrapper for zero_shot to also return state"""
from utils import generate_user_id
chat_history = start_zero_shot(query)
# Create state with user_id and method for DV processing
state = {
"user_id": generate_user_id(),
"method": "zero_shot",
"has_answer": True # Flag to indicate answer is ready
}
return state, chat_history
# Event handler for zero-shot
zero_shot_btn.click(
fn=zero_shot_with_state,
inputs=zero_query_input,
outputs=[zero_state, zero_chatbot],
show_progress="minimal"
)
# Dependent Variables Section (initially hidden)
with gr.Accordion("Evaluation Questions", open=False, visible=False) as zero_dv_accordion:
zero_dv_inputs = []
for i, question in enumerate(DV_QUESTIONS):
if "1-5" in question:
dv_input = gr.Slider(
minimum=1,
maximum=5,
step=1,
value=3,
label=question
)
elif "Yes/No" in question:
dv_input = gr.Radio(
choices=["Yes", "No"],
label=question
)
else:
dv_input = gr.Textbox(
label=question
)
zero_dv_inputs.append(dv_input)
zero_submit_dv_btn = gr.Button("Submit Evaluation", variant="primary")
zero_dv_result = gr.Markdown("")
# Function to show DV accordion after answer is generated
def show_zero_dv_section(state):
if state and state.get("has_answer", False):
return gr.update(visible=True, open=True)
return gr.update(visible=False, open=False)
# Update visibility of DV section when state changes
zero_state.change(
fn=show_zero_dv_section,
inputs=[zero_state],
outputs=[zero_dv_accordion]
)
# Handle DV submission
zero_submit_dv_btn.click(
fn=process_dv_responses,
inputs=[zero_state] + zero_dv_inputs,
outputs=zero_dv_result
)
return zero_query_input, zero_shot_btn, zero_chatbot
def create_gradio_app():
"""Create the main Gradio application with tabs."""
with gr.Blocks() as app:
with gr.Tabs() as tabs:
# IBFS Tab
with gr.Tab("IBFS"):
create_ibfs_interface()
# Zero-Shot Tab
with gr.Tab("Zero-Shot"):
create_zero_shot_interface()
return app
# Create and launch the app
if __name__ == "__main__":
ibfs_app = create_gradio_app()
ibfs_app.launch(share=True) |