Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ import pandas as pd
|
|
7 |
import sqlalchemy
|
8 |
from typing import Any, Dict, List
|
9 |
|
10 |
-
# Provider clients – ensure these libraries are installed
|
11 |
try:
|
12 |
from openai import OpenAI
|
13 |
except ImportError:
|
@@ -34,7 +34,7 @@ class QADataGenerator:
|
|
34 |
self._setup_input_handlers()
|
35 |
self._initialize_session_state()
|
36 |
# This prompt instructs the LLM to generate a configurable number of Q&A pairs.
|
37 |
-
# Note: Literal curly braces
|
38 |
self.custom_prompt_template = (
|
39 |
"You are an expert in extracting question and answer pairs from documents. "
|
40 |
"Generate {num_pairs} Q&A pairs from the following data, formatted as a JSON list of dictionaries. "
|
@@ -87,12 +87,13 @@ class QADataGenerator:
|
|
87 |
"provider": "OpenAI",
|
88 |
"model": "gpt-4-turbo",
|
89 |
"temperature": DEFAULT_TEMPERATURE,
|
90 |
-
"num_pairs": 3, # Default
|
91 |
},
|
92 |
"api_key": "",
|
93 |
"inputs": [], # List to store input sources
|
94 |
-
"qa_pairs":
|
95 |
"error_logs": [], # To store any error messages
|
|
|
96 |
}
|
97 |
for key, value in defaults.items():
|
98 |
if key not in st.session_state:
|
@@ -198,6 +199,7 @@ class QADataGenerator:
|
|
198 |
|
199 |
st.write("### Raw API Response")
|
200 |
st.write(response)
|
|
|
201 |
|
202 |
qa_pairs = self._parse_response(response, provider_name)
|
203 |
st.write("### Parsed Q&A Pairs")
|
@@ -245,7 +247,7 @@ class QADataGenerator:
|
|
245 |
"""
|
246 |
Parse the LLM response and return a list of Q&A pairs.
|
247 |
Expects the response to be in a JSON-like format.
|
248 |
-
If JSON parsing fails (e.g. due to single quotes),
|
249 |
"""
|
250 |
st.write("Parsing response for provider:", provider)
|
251 |
try:
|
@@ -303,8 +305,8 @@ def config_ui(generator: QADataGenerator):
|
|
303 |
temperature = st.slider("Temperature", 0.0, 1.0, DEFAULT_TEMPERATURE)
|
304 |
st.session_state.config["temperature"] = temperature
|
305 |
|
306 |
-
# New: Number of Q&A pairs
|
307 |
-
num_pairs = st.number_input("Number of Q&A Pairs", min_value=1, max_value=
|
308 |
st.session_state.config["num_pairs"] = num_pairs
|
309 |
|
310 |
api_key = st.text_input(f"{provider} API Key", type="password")
|
@@ -357,13 +359,27 @@ def input_ui(generator: QADataGenerator):
|
|
357 |
st.success("Database input added!")
|
358 |
|
359 |
def output_ui(generator: QADataGenerator):
|
360 |
-
"""Display the generated Q&A pairs and provide
|
361 |
st.subheader("Q&A Pairs Output")
|
362 |
if st.session_state.qa_pairs:
|
363 |
-
st.write("### Generated Q&A Pairs")
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
st.download_button(
|
366 |
-
"Download
|
367 |
json.dumps(st.session_state.qa_pairs, indent=2),
|
368 |
file_name="qa_pairs.json",
|
369 |
mime="application/json"
|
|
|
7 |
import sqlalchemy
|
8 |
from typing import Any, Dict, List
|
9 |
|
10 |
+
# Provider clients – ensure these libraries are installed if needed.
|
11 |
try:
|
12 |
from openai import OpenAI
|
13 |
except ImportError:
|
|
|
34 |
self._setup_input_handlers()
|
35 |
self._initialize_session_state()
|
36 |
# This prompt instructs the LLM to generate a configurable number of Q&A pairs.
|
37 |
+
# Note: Literal curly braces in the example are escaped with double braces.
|
38 |
self.custom_prompt_template = (
|
39 |
"You are an expert in extracting question and answer pairs from documents. "
|
40 |
"Generate {num_pairs} Q&A pairs from the following data, formatted as a JSON list of dictionaries. "
|
|
|
87 |
"provider": "OpenAI",
|
88 |
"model": "gpt-4-turbo",
|
89 |
"temperature": DEFAULT_TEMPERATURE,
|
90 |
+
"num_pairs": 3, # Default number of Q&A pairs; can be increased
|
91 |
},
|
92 |
"api_key": "",
|
93 |
"inputs": [], # List to store input sources
|
94 |
+
"qa_pairs": [], # Generated Q&A pairs output
|
95 |
"error_logs": [], # To store any error messages
|
96 |
+
"raw_response": "", # Store raw API response (if needed)
|
97 |
}
|
98 |
for key, value in defaults.items():
|
99 |
if key not in st.session_state:
|
|
|
199 |
|
200 |
st.write("### Raw API Response")
|
201 |
st.write(response)
|
202 |
+
st.session_state.raw_response = response
|
203 |
|
204 |
qa_pairs = self._parse_response(response, provider_name)
|
205 |
st.write("### Parsed Q&A Pairs")
|
|
|
247 |
"""
|
248 |
Parse the LLM response and return a list of Q&A pairs.
|
249 |
Expects the response to be in a JSON-like format.
|
250 |
+
If JSON parsing fails (e.g. due to single quotes), falls back to ast.literal_eval.
|
251 |
"""
|
252 |
st.write("Parsing response for provider:", provider)
|
253 |
try:
|
|
|
305 |
temperature = st.slider("Temperature", 0.0, 1.0, DEFAULT_TEMPERATURE)
|
306 |
st.session_state.config["temperature"] = temperature
|
307 |
|
308 |
+
# New: Number of Q&A pairs (allow up to 50)
|
309 |
+
num_pairs = st.number_input("Number of Q&A Pairs", min_value=1, max_value=50, value=3, step=1)
|
310 |
st.session_state.config["num_pairs"] = num_pairs
|
311 |
|
312 |
api_key = st.text_input(f"{provider} API Key", type="password")
|
|
|
359 |
st.success("Database input added!")
|
360 |
|
361 |
def output_ui(generator: QADataGenerator):
|
362 |
+
"""Display the generated Q&A pairs in a readable table and provide download options."""
|
363 |
st.subheader("Q&A Pairs Output")
|
364 |
if st.session_state.qa_pairs:
|
365 |
+
st.write("### Generated Q&A Pairs (Table)")
|
366 |
+
try:
|
367 |
+
df = pd.DataFrame(st.session_state.qa_pairs)
|
368 |
+
st.dataframe(df)
|
369 |
+
csv_data = df.to_csv(index=False).encode("utf-8")
|
370 |
+
except Exception as e:
|
371 |
+
st.write("Could not convert output to table format, displaying raw output.")
|
372 |
+
csv_data = json.dumps(st.session_state.qa_pairs, indent=2).encode("utf-8")
|
373 |
+
st.write(st.session_state.qa_pairs)
|
374 |
+
|
375 |
+
st.download_button(
|
376 |
+
"Download as CSV",
|
377 |
+
csv_data,
|
378 |
+
file_name="qa_pairs.csv",
|
379 |
+
mime="text/csv"
|
380 |
+
)
|
381 |
st.download_button(
|
382 |
+
"Download as JSON",
|
383 |
json.dumps(st.session_state.qa_pairs, indent=2),
|
384 |
file_name="qa_pairs.json",
|
385 |
mime="application/json"
|