Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -33,7 +33,7 @@ class QADataGenerator:
|
|
33 |
self._setup_providers()
|
34 |
self._setup_input_handlers()
|
35 |
self._initialize_session_state()
|
36 |
-
#
|
37 |
self.custom_prompt_template: str = (
|
38 |
"You are an expert in extracting question and answer pairs from documents. "
|
39 |
"Generate {num_examples} Q&A pairs from the following data, formatted as a JSON list of dictionaries. "
|
@@ -90,28 +90,11 @@ class QADataGenerator:
|
|
90 |
"api_key": "",
|
91 |
"inputs": [], # List to store input sources
|
92 |
"qa_pairs": None, # Generated Q&A pairs output
|
93 |
-
"error_logs": [], # To store error messages
|
94 |
}
|
95 |
for key, value in defaults.items():
|
96 |
if key not in st.session_state:
|
97 |
st.session_state[key] = value
|
98 |
-
|
99 |
-
# Pre-populate configuration from URL query parameters (if provided)
|
100 |
-
params = st.query_params
|
101 |
-
if "provider" in params:
|
102 |
-
st.session_state.config["provider"] = params["provider"][0]
|
103 |
-
if "model" in params:
|
104 |
-
st.session_state.config["model"] = params["model"][0]
|
105 |
-
if "temperature" in params:
|
106 |
-
try:
|
107 |
-
st.session_state.config["temperature"] = float(params["temperature"][0])
|
108 |
-
except ValueError:
|
109 |
-
pass
|
110 |
-
if "num_examples" in params:
|
111 |
-
try:
|
112 |
-
st.session_state.config["num_examples"] = int(params["num_examples"][0])
|
113 |
-
except ValueError:
|
114 |
-
pass
|
115 |
|
116 |
def log_error(self, message: str) -> None:
|
117 |
"""Log an error message to session state and display it."""
|
@@ -302,44 +285,24 @@ class QADataGenerator:
|
|
302 |
# ============ UI Components ============
|
303 |
|
304 |
def config_ui(generator: QADataGenerator) -> None:
|
305 |
-
"""Display configuration options in the sidebar
|
306 |
with st.sidebar:
|
307 |
st.header("Configuration")
|
308 |
-
|
309 |
-
params = st.query_params
|
310 |
-
default_provider = params.get("provider", ["OpenAI"])[0]
|
311 |
-
default_model = params.get("model", ["gpt-4-turbo"])[0]
|
312 |
-
default_temperature = float(params.get("temperature", [DEFAULT_TEMPERATURE])[0])
|
313 |
-
default_num_examples = int(params.get("num_examples", [3])[0])
|
314 |
-
|
315 |
-
provider_options = list(generator.providers.keys())
|
316 |
-
provider = st.selectbox("Select Provider", provider_options,
|
317 |
-
index=provider_options.index(default_provider) if default_provider in provider_options else 0)
|
318 |
st.session_state.config["provider"] = provider
|
319 |
provider_cfg = generator.providers[provider]
|
320 |
|
321 |
-
|
322 |
-
model = st.selectbox("Select Model", model_options,
|
323 |
-
index=model_options.index(default_model) if default_model in model_options else 0)
|
324 |
st.session_state.config["model"] = model
|
325 |
|
326 |
-
temperature = st.slider("Temperature", 0.0, 1.0,
|
327 |
st.session_state.config["temperature"] = temperature
|
328 |
|
329 |
-
num_examples = st.number_input("Number of Q&A Pairs", min_value=1, max_value=10,
|
330 |
-
value=default_num_examples, step=1)
|
331 |
st.session_state.config["num_examples"] = num_examples
|
332 |
|
333 |
api_key = st.text_input(f"{provider} API Key", type="password")
|
334 |
st.session_state.api_key = api_key
|
335 |
-
|
336 |
-
# Update the URL query parameters for sharing/pre-populating configuration
|
337 |
-
st.experimental_set_query_params(
|
338 |
-
provider=st.session_state.config["provider"],
|
339 |
-
model=st.session_state.config["model"],
|
340 |
-
temperature=st.session_state.config["temperature"],
|
341 |
-
num_examples=st.session_state.config["num_examples"],
|
342 |
-
)
|
343 |
|
344 |
def input_ui(generator: QADataGenerator) -> None:
|
345 |
"""Display input data source options using tabs."""
|
@@ -388,7 +351,7 @@ def input_ui(generator: QADataGenerator) -> None:
|
|
388 |
st.success("Database input added!")
|
389 |
|
390 |
def output_ui(generator: QADataGenerator) -> None:
|
391 |
-
"""Display the generated Q&A pairs and provide download options
|
392 |
st.subheader("Q&A Pairs Output")
|
393 |
if st.session_state.qa_pairs:
|
394 |
st.write("### Generated Q&A Pairs")
|
|
|
33 |
self._setup_providers()
|
34 |
self._setup_input_handlers()
|
35 |
self._initialize_session_state()
|
36 |
+
# Updated prompt template with dynamic {num_examples} parameter and escaped curly braces
|
37 |
self.custom_prompt_template: str = (
|
38 |
"You are an expert in extracting question and answer pairs from documents. "
|
39 |
"Generate {num_examples} Q&A pairs from the following data, formatted as a JSON list of dictionaries. "
|
|
|
90 |
"api_key": "",
|
91 |
"inputs": [], # List to store input sources
|
92 |
"qa_pairs": None, # Generated Q&A pairs output
|
93 |
+
"error_logs": [], # To store any error messages
|
94 |
}
|
95 |
for key, value in defaults.items():
|
96 |
if key not in st.session_state:
|
97 |
st.session_state[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
def log_error(self, message: str) -> None:
|
100 |
"""Log an error message to session state and display it."""
|
|
|
285 |
# ============ UI Components ============
|
286 |
|
287 |
def config_ui(generator: QADataGenerator) -> None:
|
288 |
+
"""Display configuration options in the sidebar."""
|
289 |
with st.sidebar:
|
290 |
st.header("Configuration")
|
291 |
+
provider = st.selectbox("Select Provider", list(generator.providers.keys()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
st.session_state.config["provider"] = provider
|
293 |
provider_cfg = generator.providers[provider]
|
294 |
|
295 |
+
model = st.selectbox("Select Model", provider_cfg["models"])
|
|
|
|
|
296 |
st.session_state.config["model"] = model
|
297 |
|
298 |
+
temperature = st.slider("Temperature", 0.0, 1.0, DEFAULT_TEMPERATURE)
|
299 |
st.session_state.config["temperature"] = temperature
|
300 |
|
301 |
+
num_examples = st.number_input("Number of Q&A Pairs", min_value=1, max_value=10, value=3, step=1)
|
|
|
302 |
st.session_state.config["num_examples"] = num_examples
|
303 |
|
304 |
api_key = st.text_input(f"{provider} API Key", type="password")
|
305 |
st.session_state.api_key = api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
|
307 |
def input_ui(generator: QADataGenerator) -> None:
|
308 |
"""Display input data source options using tabs."""
|
|
|
351 |
st.success("Database input added!")
|
352 |
|
353 |
def output_ui(generator: QADataGenerator) -> None:
|
354 |
+
"""Display the generated Q&A pairs and provide download options."""
|
355 |
st.subheader("Q&A Pairs Output")
|
356 |
if st.session_state.qa_pairs:
|
357 |
st.write("### Generated Q&A Pairs")
|