rodrisouza commited on
Commit
d71dca1
·
verified ·
1 Parent(s): ee9ffa5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -10
app.py CHANGED
@@ -68,12 +68,11 @@ tokenizer, model = load_model(selected_model)
68
 
69
  # Chat history and interaction counter
70
  chat_history = []
71
- interaction_count = 0
72
 
73
  # Function to handle interaction with model
74
  @spaces.GPU
75
- def interact(user_input, history):
76
- global tokenizer, model, interaction_count
77
  try:
78
  if tokenizer is None or model is None:
79
  raise ValueError("Tokenizer or model is not initialized.")
@@ -104,7 +103,7 @@ def interact(user_input, history):
104
  history[-1]["content"] = response
105
 
106
  formatted_history = [(entry["content"], None) if entry["role"] == "user" else (None, entry["content"]) for entry in history if entry["role"] in ["user", "assistant"]]
107
- return "", formatted_history, history
108
  except Exception as e:
109
  if torch.cuda.is_available():
110
  torch.cuda.empty_cache()
@@ -113,9 +112,8 @@ def interact(user_input, history):
113
 
114
  # Function to send selected story and initial message
115
  def send_selected_story(title, model_name, system_prompt):
116
- global chat_history, selected_story, data, interaction_count
117
  data = [] # Reset data for new story
118
- interaction_count = 0 # Reset interaction counter
119
  tokenizer, model = load_model(model_name)
120
  selected_story = title
121
  for story in stories:
@@ -134,9 +132,9 @@ Here is the story:
134
 
135
  # Generate the first question based on the story
136
  question_prompt = "Please ask a simple question about the story to encourage interaction."
137
- _, formatted_history, chat_history = interact(question_prompt, chat_history)
138
 
139
- return formatted_history, chat_history, gr.update(value=[]), story["story"] # Reset the data table and return the story
140
  else:
141
  print("Combined message is empty.")
142
  else:
@@ -217,9 +215,10 @@ with gr.Blocks() as demo:
217
  data_table = gr.DataFrame(headers=["User Input", "Chat Response", "Score", "Comment"])
218
 
219
  chat_history_json = gr.JSON(value=[], visible=False)
 
220
 
221
- 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_textbox])
222
- send_message_button.click(fn=interact, inputs=[chatbot_input, chat_history_json], outputs=[chatbot_input, chatbot_output, chat_history_json])
223
  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])
224
 
225
  demo.launch()
 
68
 
69
  # Chat history and interaction counter
70
  chat_history = []
 
71
 
72
  # Function to handle interaction with model
73
  @spaces.GPU
74
+ def interact(user_input, history, interaction_count):
75
+ global tokenizer, model
76
  try:
77
  if tokenizer is None or model is None:
78
  raise ValueError("Tokenizer or model is not initialized.")
 
103
  history[-1]["content"] = response
104
 
105
  formatted_history = [(entry["content"], None) if entry["role"] == "user" else (None, entry["content"]) for entry in history if entry["role"] in ["user", "assistant"]]
106
+ return "", formatted_history, history, interaction_count
107
  except Exception as e:
108
  if torch.cuda.is_available():
109
  torch.cuda.empty_cache()
 
112
 
113
  # Function to send selected story and initial message
114
  def send_selected_story(title, model_name, system_prompt):
115
+ global chat_history, selected_story, data
116
  data = [] # Reset data for new story
 
117
  tokenizer, model = load_model(model_name)
118
  selected_story = title
119
  for story in stories:
 
132
 
133
  # Generate the first question based on the story
134
  question_prompt = "Please ask a simple question about the story to encourage interaction."
135
+ formatted_history, chat_history, interaction_count = interact(question_prompt, chat_history, 0)
136
 
137
+ return formatted_history, chat_history, gr.update(value=[]), story["story"], interaction_count # Reset the data table and return the story
138
  else:
139
  print("Combined message is empty.")
140
  else:
 
215
  data_table = gr.DataFrame(headers=["User Input", "Chat Response", "Score", "Comment"])
216
 
217
  chat_history_json = gr.JSON(value=[], visible=False)
218
+ interaction_count_state = gr.State(0)
219
 
220
+ 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_textbox, interaction_count_state])
221
+ send_message_button.click(fn=interact, inputs=[chatbot_input, chat_history_json, interaction_count_state], outputs=[chatbot_input, chatbot_output, chat_history_json, interaction_count_state])
222
  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])
223
 
224
  demo.launch()