Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
@@ -13,8 +13,8 @@ torch.jit.script = lambda f: f
|
|
13 |
# Initialize Google Sheets client
|
14 |
client = init_google_sheets_client()
|
15 |
sheet = client.open(google_sheets_name)
|
16 |
-
stories_sheet = sheet.
|
17 |
-
prompts_sheet = sheet.
|
18 |
|
19 |
# Load stories from Google Sheets
|
20 |
def load_stories():
|
@@ -22,15 +22,17 @@ def load_stories():
|
|
22 |
stories = [{"title": story[0], "story": story[1]} for story in stories_data if story[0] != "Title"] # Skip header row
|
23 |
return stories
|
24 |
|
|
|
|
|
|
|
25 |
# Load system prompts from Google Sheets
|
26 |
-
def
|
27 |
prompts_data = prompts_sheet.get_all_values()
|
28 |
-
|
29 |
-
return
|
30 |
|
31 |
-
# Load available
|
32 |
-
|
33 |
-
prompts = load_prompts()
|
34 |
|
35 |
# Initialize the selected model
|
36 |
selected_model = default_model_name
|
@@ -106,12 +108,10 @@ def interact(user_input, history):
|
|
106 |
def send_selected_story(title, model_name, system_prompt):
|
107 |
global chat_history
|
108 |
global selected_story
|
109 |
-
global selected_system_prompt
|
110 |
global data # Ensure data is reset
|
111 |
data = [] # Reset data for new story
|
112 |
tokenizer, model = load_model(model_name)
|
113 |
selected_story = title
|
114 |
-
selected_system_prompt = system_prompt
|
115 |
for story in stories:
|
116 |
if story["title"] == title:
|
117 |
system_prompt = f"""
|
@@ -130,14 +130,14 @@ Here is the story:
|
|
130 |
question_prompt = "Please ask a simple question about the story to encourage interaction."
|
131 |
_, formatted_history, chat_history = interact(question_prompt, chat_history)
|
132 |
|
133 |
-
return formatted_history, chat_history, gr.update(value=[]
|
134 |
else:
|
135 |
print("Combined message is empty.")
|
136 |
else:
|
137 |
print("Story title does not match.")
|
138 |
|
139 |
# Function to save comment and score
|
140 |
-
def save_comment_score(chat_responses, score, comment, story_name, user_name):
|
141 |
last_user_message = ""
|
142 |
last_assistant_message = ""
|
143 |
|
@@ -155,7 +155,6 @@ def save_comment_score(chat_responses, score, comment, story_name, user_name):
|
|
155 |
timestamp = datetime.now(timezone.utc) - timedelta(hours=3) # Adjust to GMT-3
|
156 |
timestamp_str = timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
157 |
model_name = selected_model
|
158 |
-
system_prompt = selected_system_prompt
|
159 |
|
160 |
# Append data to local data storage
|
161 |
data.append([
|
@@ -171,11 +170,15 @@ def save_comment_score(chat_responses, score, comment, story_name, user_name):
|
|
171 |
])
|
172 |
|
173 |
# Append data to Google Sheets
|
174 |
-
sheet = client.open(google_sheets_name).
|
175 |
sheet.append_row([timestamp_str, user_name, model_name, system_prompt, story_name, last_user_message, last_assistant_message, score, comment])
|
176 |
|
|
|
|
|
|
|
|
|
177 |
df = pd.DataFrame(data, columns=["Timestamp", "User Name", "Model Name", "System Prompt", "Story Name", "User Input", "Chat Response", "Score", "Comment"])
|
178 |
-
return
|
179 |
|
180 |
# Create the chat interface using Gradio Blocks
|
181 |
with gr.Blocks() as demo:
|
@@ -185,10 +188,10 @@ with gr.Blocks() as demo:
|
|
185 |
user_dropdown = gr.Dropdown(choices=user_names, label="Select User Name")
|
186 |
initial_story = stories[0]["title"] if stories else None
|
187 |
story_dropdown = gr.Dropdown(choices=[story["title"] for story in stories], label="Select Story", value=initial_story)
|
188 |
-
system_prompt_dropdown = gr.Dropdown(choices=
|
189 |
|
190 |
send_story_button = gr.Button("Send Story")
|
191 |
-
|
192 |
|
193 |
with gr.Row():
|
194 |
with gr.Column(scale=1):
|
@@ -204,12 +207,12 @@ with gr.Blocks() as demo:
|
|
204 |
comment_input = gr.Textbox(placeholder="Add a comment...", label="Comment")
|
205 |
save_button = gr.Button("Save Score and Comment")
|
206 |
|
207 |
-
data_table = gr.DataFrame(headers=["
|
208 |
|
209 |
chat_history_json = gr.JSON(value=[], visible=False)
|
210 |
|
211 |
-
send_story_button.click(fn=send_selected_story, inputs=[story_dropdown, model_dropdown, system_prompt_dropdown], outputs=[chatbot_output, chat_history_json, data_table,
|
212 |
send_message_button.click(fn=interact, inputs=[chatbot_input, chat_history_json], outputs=[chatbot_input, chatbot_output, chat_history_json])
|
213 |
-
save_button.click(fn=save_comment_score, inputs=[chatbot_output, score_input, comment_input, story_dropdown, user_dropdown], outputs=[data_table, comment_input])
|
214 |
|
215 |
demo.launch()
|
|
|
13 |
# Initialize Google Sheets client
|
14 |
client = init_google_sheets_client()
|
15 |
sheet = client.open(google_sheets_name)
|
16 |
+
stories_sheet = sheet.get_worksheet(1) # Assuming stories are in the second sheet (index 1)
|
17 |
+
prompts_sheet = sheet.get_worksheet(2) # Assuming system prompts are in the third sheet (index 2)
|
18 |
|
19 |
# Load stories from Google Sheets
|
20 |
def load_stories():
|
|
|
22 |
stories = [{"title": story[0], "story": story[1]} for story in stories_data if story[0] != "Title"] # Skip header row
|
23 |
return stories
|
24 |
|
25 |
+
# Load available stories
|
26 |
+
stories = load_stories()
|
27 |
+
|
28 |
# Load system prompts from Google Sheets
|
29 |
+
def load_system_prompts():
|
30 |
prompts_data = prompts_sheet.get_all_values()
|
31 |
+
system_prompts = [row[0] for row in prompts_data if row[0] != "System Prompt"] # Skip header row
|
32 |
+
return system_prompts
|
33 |
|
34 |
+
# Load available system prompts
|
35 |
+
system_prompts = load_system_prompts()
|
|
|
36 |
|
37 |
# Initialize the selected model
|
38 |
selected_model = default_model_name
|
|
|
108 |
def send_selected_story(title, model_name, system_prompt):
|
109 |
global chat_history
|
110 |
global selected_story
|
|
|
111 |
global data # Ensure data is reset
|
112 |
data = [] # Reset data for new story
|
113 |
tokenizer, model = load_model(model_name)
|
114 |
selected_story = title
|
|
|
115 |
for story in stories:
|
116 |
if story["title"] == title:
|
117 |
system_prompt = f"""
|
|
|
130 |
question_prompt = "Please ask a simple question about the story to encourage interaction."
|
131 |
_, formatted_history, chat_history = interact(question_prompt, chat_history)
|
132 |
|
133 |
+
return formatted_history, chat_history, gr.update(value=[]), gr.update(value=story['story']) # Reset the data table and update selected story
|
134 |
else:
|
135 |
print("Combined message is empty.")
|
136 |
else:
|
137 |
print("Story title does not match.")
|
138 |
|
139 |
# Function to save comment and score
|
140 |
+
def save_comment_score(chat_responses, score, comment, story_name, user_name, system_prompt):
|
141 |
last_user_message = ""
|
142 |
last_assistant_message = ""
|
143 |
|
|
|
155 |
timestamp = datetime.now(timezone.utc) - timedelta(hours=3) # Adjust to GMT-3
|
156 |
timestamp_str = timestamp.strftime("%Y-%m-%d %H:%M:%S")
|
157 |
model_name = selected_model
|
|
|
158 |
|
159 |
# Append data to local data storage
|
160 |
data.append([
|
|
|
170 |
])
|
171 |
|
172 |
# Append data to Google Sheets
|
173 |
+
sheet = client.open(google_sheets_name).sheet1 # Assuming results are saved in sheet1
|
174 |
sheet.append_row([timestamp_str, user_name, model_name, system_prompt, story_name, last_user_message, last_assistant_message, score, comment])
|
175 |
|
176 |
+
# Create a DataFrame for display
|
177 |
+
display_data = [[last_user_message, last_assistant_message, score, comment]]
|
178 |
+
df_display = pd.DataFrame(display_data, columns=["User Input", "Chat Response", "Score", "Comment"])
|
179 |
+
|
180 |
df = pd.DataFrame(data, columns=["Timestamp", "User Name", "Model Name", "System Prompt", "Story Name", "User Input", "Chat Response", "Score", "Comment"])
|
181 |
+
return df_display, gr.update(value="") # Clear the comment input box
|
182 |
|
183 |
# Create the chat interface using Gradio Blocks
|
184 |
with gr.Blocks() as demo:
|
|
|
188 |
user_dropdown = gr.Dropdown(choices=user_names, label="Select User Name")
|
189 |
initial_story = stories[0]["title"] if stories else None
|
190 |
story_dropdown = gr.Dropdown(choices=[story["title"] for story in stories], label="Select Story", value=initial_story)
|
191 |
+
system_prompt_dropdown = gr.Dropdown(choices=system_prompts, label="Select System Prompt", value=system_prompts[0] if system_prompts else "")
|
192 |
|
193 |
send_story_button = gr.Button("Send Story")
|
194 |
+
selected_story_display = gr.Textbox(label="Selected Story", interactive=False)
|
195 |
|
196 |
with gr.Row():
|
197 |
with gr.Column(scale=1):
|
|
|
207 |
comment_input = gr.Textbox(placeholder="Add a comment...", label="Comment")
|
208 |
save_button = gr.Button("Save Score and Comment")
|
209 |
|
210 |
+
data_table = gr.DataFrame(headers=["User Input", "Chat Response", "Score", "Comment"])
|
211 |
|
212 |
chat_history_json = gr.JSON(value=[], visible=False)
|
213 |
|
214 |
+
send_story_button.click(fn=send_selected_story, inputs=[story_dropdown, model_dropdown, system_prompt_dropdown], outputs=[chatbot_output, chat_history_json, data_table, selected_story_display])
|
215 |
send_message_button.click(fn=interact, inputs=[chatbot_input, chat_history_json], outputs=[chatbot_input, chatbot_output, chat_history_json])
|
216 |
+
save_button.click(fn=save_comment_score, inputs=[chatbot_output, score_input, comment_input, story_dropdown, user_dropdown, system_prompt_dropdown], outputs=[data_table, comment_input])
|
217 |
|
218 |
demo.launch()
|