naman1102 commited on
Commit
1ac7e9d
·
1 Parent(s): 1351057

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -5
app.py CHANGED
@@ -104,12 +104,27 @@ def analyze_and_update_single_repo(repo_id: str) -> Tuple[str, str, pd.DataFrame
104
 
105
  # --- NEW: Helper for Chat History Conversion ---
106
  def convert_messages_to_tuples(history: List[Dict[str, str]]) -> List[Tuple[str, str]]:
107
- """Converts Gradio's 'messages' format to the old 'tuple' format for compatibility."""
 
 
 
108
  tuple_history = []
109
- # Assumes a strict user-assistant-user-assistant turn structure.
110
- for i in range(0, len(history), 2):
111
- if i + 1 < len(history) and history[i]['role'] == 'user' and history[i+1]['role'] == 'assistant':
112
- tuple_history.append((history[i]['content'], history[i+1]['content']))
 
 
 
 
 
 
 
 
 
 
 
 
113
  return tuple_history
114
 
115
  # --- Gradio UI ---
 
104
 
105
  # --- NEW: Helper for Chat History Conversion ---
106
  def convert_messages_to_tuples(history: List[Dict[str, str]]) -> List[Tuple[str, str]]:
107
+ """
108
+ Converts Gradio's 'messages' format to the old 'tuple' format for compatibility.
109
+ This robust version correctly handles histories that start with an assistant message.
110
+ """
111
  tuple_history = []
112
+ # Find the start of the actual conversation (the first user message)
113
+ start_index = 0
114
+ for i, msg in enumerate(history):
115
+ if msg['role'] == 'user':
116
+ start_index = i
117
+ break
118
+
119
+ # Group the rest of the messages into (user, assistant) pairs
120
+ user_msg_content = None
121
+ for i in range(start_index, len(history)):
122
+ if history[i]['role'] == 'user':
123
+ user_msg_content = history[i]['content']
124
+ elif history[i]['role'] == 'assistant' and user_msg_content is not None:
125
+ tuple_history.append((user_msg_content, history[i]['content']))
126
+ user_msg_content = None # Reset to find the next pair
127
+
128
  return tuple_history
129
 
130
  # --- Gradio UI ---