Spaces:
Running
Running
Updated selection operations.
Browse files
app/streamlit_sample_generator.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import pandas as pd
|
2 |
import streamlit as st
|
3 |
import json
|
4 |
-
from
|
5 |
from meta_prompt.sample_generator import TaskDescriptionGenerator
|
6 |
|
7 |
def process_json(input_json, model_name, generating_batch_size, temperature):
|
@@ -17,7 +17,8 @@ def process_json(input_json, model_name, generating_batch_size, temperature):
|
|
17 |
examples = [[example["input"], example["output"]] for example in result["additional_examples"]]
|
18 |
return description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples
|
19 |
except Exception as e:
|
20 |
-
st.
|
|
|
21 |
|
22 |
def generate_description_only(input_json, model_name, temperature):
|
23 |
try:
|
@@ -66,6 +67,34 @@ def generate_examples_directly(description, raw_example, generating_batch_size,
|
|
66 |
except Exception as e:
|
67 |
st.error(f"An error occurred: {str(e)}")
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
# Session State
|
70 |
if 'description_output_text' not in st.session_state:
|
71 |
st.session_state.description_output_text = ''
|
@@ -85,6 +114,9 @@ if 'examples_directly_dataframe' not in st.session_state:
|
|
85 |
if 'examples_dataframe' not in st.session_state:
|
86 |
st.session_state.examples_dataframe = pd.DataFrame(columns=["Input", "Output"])
|
87 |
|
|
|
|
|
|
|
88 |
def update_description_output_text():
|
89 |
st.session_state.description_output_text = generate_description_only(input_json, model_name, temperature)
|
90 |
|
@@ -95,20 +127,23 @@ def update_example_briefs_output_text():
|
|
95 |
st.session_state.example_briefs_output_text = generate_briefs(description_output, input_analysis_output, generating_batch_size, model_name, temperature)
|
96 |
|
97 |
def update_examples_from_briefs_dataframe():
|
98 |
-
|
|
|
99 |
|
100 |
def update_examples_directly_dataframe():
|
101 |
-
|
|
|
102 |
|
103 |
def generate_examples_dataframe():
|
104 |
result = process_json(input_json, model_name, generating_batch_size, temperature)
|
105 |
description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples = result
|
106 |
st.session_state.description_output_text = description
|
107 |
-
st.session_state.examples_directly_dataframe = examples_directly
|
108 |
st.session_state.input_analysis_output_text = input_analysis
|
109 |
st.session_state.example_briefs_output_text = new_example_briefs
|
110 |
-
st.session_state.examples_from_briefs_dataframe = examples_from_briefs
|
111 |
-
st.session_state.examples_dataframe = examples
|
|
|
112 |
|
113 |
# Streamlit UI
|
114 |
st.title("Task Description Generator")
|
@@ -141,14 +176,14 @@ with col3:
|
|
141 |
with col4:
|
142 |
analyze_input_button = st.button("Analyze Input", on_click=update_input_analysis_output_text)
|
143 |
|
144 |
-
examples_directly_output = st.dataframe(st.session_state.examples_directly_dataframe, use_container_width=True)
|
145 |
input_analysis_output = st.text_area("Input Analysis", value=st.session_state.input_analysis_output_text, height=100)
|
146 |
generate_briefs_button = st.button("Generate Briefs", on_click=update_example_briefs_output_text)
|
147 |
example_briefs_output = st.text_area("Example Briefs", value=st.session_state.example_briefs_output_text, height=100)
|
148 |
generate_examples_from_briefs_button = st.button("Generate Examples from Briefs", on_click=update_examples_from_briefs_dataframe)
|
149 |
-
examples_from_briefs_output = st.dataframe(st.session_state.examples_from_briefs_dataframe, use_container_width=True)
|
150 |
-
examples_output = st.dataframe(st.session_state.examples_dataframe, use_container_width=True)
|
151 |
-
new_example_json = st.text_area("New Example JSON", height=100)
|
152 |
|
153 |
# Button actions
|
154 |
if submit_button:
|
|
|
1 |
import pandas as pd
|
2 |
import streamlit as st
|
3 |
import json
|
4 |
+
from langchain_openai import ChatOpenAI
|
5 |
from meta_prompt.sample_generator import TaskDescriptionGenerator
|
6 |
|
7 |
def process_json(input_json, model_name, generating_batch_size, temperature):
|
|
|
17 |
examples = [[example["input"], example["output"]] for example in result["additional_examples"]]
|
18 |
return description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples
|
19 |
except Exception as e:
|
20 |
+
st.warning(f"An error occurred: {str(e)}. Returning default values.")
|
21 |
+
return "", [], "", [], [], []
|
22 |
|
23 |
def generate_description_only(input_json, model_name, temperature):
|
24 |
try:
|
|
|
67 |
except Exception as e:
|
68 |
st.error(f"An error occurred: {str(e)}")
|
69 |
|
70 |
+
def example_directly_selected():
|
71 |
+
if 'selected_example_directly_id' in st.session_state:
|
72 |
+
try:
|
73 |
+
selected_example_id = st.session_state.selected_example_directly_id['selection']['rows'][0]
|
74 |
+
selected_example = st.session_state.examples_directly_dataframe.iloc[selected_example_id].to_dict()
|
75 |
+
st.session_state.selected_example = json.dumps({k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
|
76 |
+
except Exception as e:
|
77 |
+
st.session_state.selected_example = None
|
78 |
+
|
79 |
+
def example_from_briefs_selected():
|
80 |
+
if 'selected_example_from_briefs_id' in st.session_state:
|
81 |
+
try:
|
82 |
+
selected_example_id = st.session_state.selected_example_from_briefs_id['selection']['rows'][0]
|
83 |
+
selected_example = st.session_state.examples_from_briefs_dataframe.iloc[selected_example_id].to_dict()
|
84 |
+
st.session_state.selected_example = json.dumps({k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
|
85 |
+
except Exception as e:
|
86 |
+
st.session_state.selected_example = None
|
87 |
+
|
88 |
+
def example_selected():
|
89 |
+
if 'selected_example_id' in st.session_state:
|
90 |
+
try:
|
91 |
+
selected_example_id = st.session_state.selected_example_id['selection']['rows'][0]
|
92 |
+
selected_example = st.session_state.examples_dataframe.iloc[selected_example_id].to_dict()
|
93 |
+
st.session_state.selected_example = json.dumps({k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
|
94 |
+
except Exception as e:
|
95 |
+
st.session_state.selected_example = None
|
96 |
+
|
97 |
+
|
98 |
# Session State
|
99 |
if 'description_output_text' not in st.session_state:
|
100 |
st.session_state.description_output_text = ''
|
|
|
114 |
if 'examples_dataframe' not in st.session_state:
|
115 |
st.session_state.examples_dataframe = pd.DataFrame(columns=["Input", "Output"])
|
116 |
|
117 |
+
if 'selected_example' not in st.session_state:
|
118 |
+
st.session_state.selected_example = None
|
119 |
+
|
120 |
def update_description_output_text():
|
121 |
st.session_state.description_output_text = generate_description_only(input_json, model_name, temperature)
|
122 |
|
|
|
127 |
st.session_state.example_briefs_output_text = generate_briefs(description_output, input_analysis_output, generating_batch_size, model_name, temperature)
|
128 |
|
129 |
def update_examples_from_briefs_dataframe():
|
130 |
+
examples = generate_examples_from_briefs(description_output, example_briefs_output, input_json, generating_batch_size, model_name, temperature)
|
131 |
+
st.session_state.examples_from_briefs_dataframe = pd.DataFrame(examples, columns=["Input", "Output"])
|
132 |
|
133 |
def update_examples_directly_dataframe():
|
134 |
+
examples = generate_examples_directly(description_output, input_json, generating_batch_size, model_name, temperature)
|
135 |
+
st.session_state.examples_directly_dataframe = pd.DataFrame(examples, columns=["Input", "Output"])
|
136 |
|
137 |
def generate_examples_dataframe():
|
138 |
result = process_json(input_json, model_name, generating_batch_size, temperature)
|
139 |
description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples = result
|
140 |
st.session_state.description_output_text = description
|
141 |
+
st.session_state.examples_directly_dataframe = pd.DataFrame(examples_directly, columns=["Input", "Output"])
|
142 |
st.session_state.input_analysis_output_text = input_analysis
|
143 |
st.session_state.example_briefs_output_text = new_example_briefs
|
144 |
+
st.session_state.examples_from_briefs_dataframe = pd.DataFrame(examples_from_briefs, columns=["Input", "Output"])
|
145 |
+
st.session_state.examples_dataframe = pd.DataFrame(examples, columns=["Input", "Output"])
|
146 |
+
st.session_state.selected_example = None
|
147 |
|
148 |
# Streamlit UI
|
149 |
st.title("Task Description Generator")
|
|
|
176 |
with col4:
|
177 |
analyze_input_button = st.button("Analyze Input", on_click=update_input_analysis_output_text)
|
178 |
|
179 |
+
examples_directly_output = st.dataframe(st.session_state.examples_directly_dataframe, use_container_width=True, selection_mode="single-row", key="selected_example_directly_id", on_select=example_directly_selected)
|
180 |
input_analysis_output = st.text_area("Input Analysis", value=st.session_state.input_analysis_output_text, height=100)
|
181 |
generate_briefs_button = st.button("Generate Briefs", on_click=update_example_briefs_output_text)
|
182 |
example_briefs_output = st.text_area("Example Briefs", value=st.session_state.example_briefs_output_text, height=100)
|
183 |
generate_examples_from_briefs_button = st.button("Generate Examples from Briefs", on_click=update_examples_from_briefs_dataframe)
|
184 |
+
examples_from_briefs_output = st.dataframe(st.session_state.examples_from_briefs_dataframe, use_container_width=True, selection_mode="single-row", key="selected_example_from_briefs_id", on_select=example_from_briefs_selected)
|
185 |
+
examples_output = st.dataframe(st.session_state.examples_dataframe, use_container_width=True, selection_mode="single-row", key="selected_example_id", on_select=example_selected)
|
186 |
+
new_example_json = st.text_area("New Example JSON", value=st.session_state.selected_example, height=100)
|
187 |
|
188 |
# Button actions
|
189 |
if submit_button:
|