Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -7,9 +7,9 @@ import os
|
|
| 7 |
GOOGLE_API_KEY = 'AIzaSyDnvz-l-Hq_tNkRkx2j_W8pd41XT08nH6Y' # Ensure you handle your API key securely
|
| 8 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 9 |
|
| 10 |
-
|
| 11 |
# Create a model object
|
| 12 |
model = genai.GenerativeModel("gemini-pro")
|
|
|
|
| 13 |
# Function to save prompts to a JSON file
|
| 14 |
def save_prompts_to_json(prompt_format, raw_prompt, model_prompt):
|
| 15 |
data = {
|
|
@@ -19,36 +19,15 @@ def save_prompts_to_json(prompt_format, raw_prompt, model_prompt):
|
|
| 19 |
}
|
| 20 |
|
| 21 |
file_path = 'prompts.json'
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
existing_data = [] # Start with an empty list if file is corrupted or empty
|
| 32 |
-
|
| 33 |
-
# Append new data
|
| 34 |
-
existing_data.append(data)
|
| 35 |
-
|
| 36 |
-
# Move the pointer to the start of the file
|
| 37 |
-
file.seek(0)
|
| 38 |
-
|
| 39 |
-
# Write back all data
|
| 40 |
-
json.dump(existing_data, file, indent=4)
|
| 41 |
-
# Truncate the file to the new size
|
| 42 |
-
file.truncate()
|
| 43 |
-
else:
|
| 44 |
-
# Create new file and write data
|
| 45 |
-
with open(file_path, 'w') as file:
|
| 46 |
-
json.dump([data], file, indent=4)
|
| 47 |
-
|
| 48 |
-
except Exception as e:
|
| 49 |
-
# Handle any other exceptions (like permission errors)
|
| 50 |
-
print(f"An error occurred while saving to JSON: {e}")
|
| 51 |
-
|
| 52 |
|
| 53 |
# Streamlit app layout
|
| 54 |
def main():
|
|
@@ -59,11 +38,7 @@ def main():
|
|
| 59 |
st.session_state.selection_enabled = False
|
| 60 |
|
| 61 |
st.sidebar.title("Select the best Response")
|
| 62 |
-
|
| 63 |
-
prompt_format = st.sidebar.radio("Options", ("Response 1 (RISE)", "Response 2 (RTF)"))
|
| 64 |
-
else:
|
| 65 |
-
st.sidebar.write("Responses will appear here after generation.")
|
| 66 |
-
|
| 67 |
# Chat section
|
| 68 |
st.header("Chat Section")
|
| 69 |
|
|
@@ -165,7 +140,6 @@ def main():
|
|
| 165 |
# **Display user input and both responses**
|
| 166 |
with st.chat_message("user"):
|
| 167 |
st.markdown(f"**User Input:** \n\n {st.session_state.raw_prompt}") # **Show user input**
|
| 168 |
-
|
| 169 |
|
| 170 |
# **Display both responses**
|
| 171 |
with st.chat_message("assistant"):
|
|
@@ -181,10 +155,15 @@ def main():
|
|
| 181 |
|
| 182 |
# Handle response selection once responses are generated
|
| 183 |
if st.session_state.selection_enabled:
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
if selected_option == "Response 1 (RISE)":
|
| 189 |
st.session_state.selected_response = st.session_state.model_prompt_1
|
| 190 |
label = "RISE"
|
|
|
|
| 7 |
GOOGLE_API_KEY = 'AIzaSyDnvz-l-Hq_tNkRkx2j_W8pd41XT08nH6Y' # Ensure you handle your API key securely
|
| 8 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 9 |
|
|
|
|
| 10 |
# Create a model object
|
| 11 |
model = genai.GenerativeModel("gemini-pro")
|
| 12 |
+
|
| 13 |
# Function to save prompts to a JSON file
|
| 14 |
def save_prompts_to_json(prompt_format, raw_prompt, model_prompt):
|
| 15 |
data = {
|
|
|
|
| 19 |
}
|
| 20 |
|
| 21 |
file_path = 'prompts.json'
|
| 22 |
+
if os.path.exists(file_path):
|
| 23 |
+
with open(file_path, 'r+') as file:
|
| 24 |
+
existing_data = json.load(file)
|
| 25 |
+
existing_data.append(data)
|
| 26 |
+
file.seek(0)
|
| 27 |
+
json.dump(existing_data, file, indent=4)
|
| 28 |
+
else:
|
| 29 |
+
with open(file_path, 'w') as file:
|
| 30 |
+
json.dump([data], file, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Streamlit app layout
|
| 33 |
def main():
|
|
|
|
| 38 |
st.session_state.selection_enabled = False
|
| 39 |
|
| 40 |
st.sidebar.title("Select the best Response")
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# Chat section
|
| 43 |
st.header("Chat Section")
|
| 44 |
|
|
|
|
| 140 |
# **Display user input and both responses**
|
| 141 |
with st.chat_message("user"):
|
| 142 |
st.markdown(f"**User Input:** \n\n {st.session_state.raw_prompt}") # **Show user input**
|
|
|
|
| 143 |
|
| 144 |
# **Display both responses**
|
| 145 |
with st.chat_message("assistant"):
|
|
|
|
| 155 |
|
| 156 |
# Handle response selection once responses are generated
|
| 157 |
if st.session_state.selection_enabled:
|
| 158 |
+
# **Use selectbox with placeholder to prevent pre-selection**
|
| 159 |
+
selected_option = st.sidebar.selectbox(
|
| 160 |
+
"Select a Response",
|
| 161 |
+
["Select a Response", "Response 1 (RISE)", "Response 2 (RTF)"],
|
| 162 |
+
index=0
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# Ensure user has selected a valid response (not the placeholder)
|
| 166 |
+
if selected_option != "Select a Response":
|
| 167 |
if selected_option == "Response 1 (RISE)":
|
| 168 |
st.session_state.selected_response = st.session_state.model_prompt_1
|
| 169 |
label = "RISE"
|