Update app.py
Browse files
app.py
CHANGED
|
@@ -46,7 +46,7 @@ def initialize_hf_dataset():
|
|
| 46 |
with open(TEMP_JSON, "w") as f:
|
| 47 |
pass
|
| 48 |
|
| 49 |
-
return True, f"
|
| 50 |
except Exception as e:
|
| 51 |
return False, f"Error initializing HuggingFace dataset: {str(e)}"
|
| 52 |
|
|
@@ -77,7 +77,7 @@ def push_to_hf_dataset(data_row):
|
|
| 77 |
except Exception as e:
|
| 78 |
return False, f"Error pushing to HuggingFace: {str(e)}"
|
| 79 |
|
| 80 |
-
def save_choice(text_id, original_text, summary_a, summary_b, choice, notes=""):
|
| 81 |
"""Save the user's choice locally and to HuggingFace dataset"""
|
| 82 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 83 |
chosen_summary = "A" if choice == "Summary A" else "B"
|
|
@@ -90,7 +90,8 @@ def save_choice(text_id, original_text, summary_a, summary_b, choice, notes=""):
|
|
| 90 |
"summary_a": summary_a,
|
| 91 |
"summary_b": summary_b,
|
| 92 |
"chosen_summary": chosen_summary,
|
| 93 |
-
"notes": notes
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
# Save locally
|
|
@@ -105,10 +106,12 @@ def save_choice(text_id, original_text, summary_a, summary_b, choice, notes=""):
|
|
| 105 |
# Push to HuggingFace
|
| 106 |
success, message = push_to_hf_dataset(new_row)
|
| 107 |
|
|
|
|
|
|
|
| 108 |
if success:
|
| 109 |
-
return f"Selection saved for text ID: {text_id}! You chose {'Summary A' if choice == 'Summary A' else 'Summary B'}. Pushed to HuggingFace."
|
| 110 |
else:
|
| 111 |
-
return f"Selection saved locally for text ID: {text_id}. HuggingFace push failed: {message}"
|
| 112 |
|
| 113 |
class SummaryChooser:
|
| 114 |
def __init__(self):
|
|
@@ -116,6 +119,17 @@ class SummaryChooser:
|
|
| 116 |
self.current_index = 0
|
| 117 |
self.total_items = len(self.df)
|
| 118 |
self.hf_status = initialize_hf_dataset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
def get_current_item(self):
|
| 121 |
"""Get the current item from the dataframe"""
|
|
@@ -134,8 +148,8 @@ class SummaryChooser:
|
|
| 134 |
# Get current values
|
| 135 |
text_id, text, summary_a, summary_b, _ = self.get_current_item()
|
| 136 |
|
| 137 |
-
# Save the choice
|
| 138 |
-
result_message = save_choice(text_id, text, summary_a, summary_b, choice, notes)
|
| 139 |
|
| 140 |
# Move to next item or wrap around
|
| 141 |
self.current_index = (self.current_index + 1) % self.total_items
|
|
@@ -159,7 +173,7 @@ class SummaryChooser:
|
|
| 159 |
def get_hf_status(self):
|
| 160 |
"""Get the status of HuggingFace integration"""
|
| 161 |
success, message = self.hf_status
|
| 162 |
-
return f"
|
| 163 |
|
| 164 |
# Create the application
|
| 165 |
app = SummaryChooser()
|
|
@@ -171,9 +185,14 @@ with gr.Blocks(title="Summary Chooser") as interface:
|
|
| 171 |
|
| 172 |
with gr.Row():
|
| 173 |
with gr.Column():
|
| 174 |
-
progress_label = gr.Label(label="Progress")
|
|
|
|
| 175 |
with gr.Column():
|
| 176 |
hf_status = gr.Label(label="HuggingFace Status", value=app.get_hf_status())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
with gr.Column():
|
| 178 |
text_id_box = gr.Textbox(label="Text ID", interactive=False)
|
| 179 |
|
|
@@ -222,6 +241,13 @@ with gr.Blocks(title="Summary Chooser") as interface:
|
|
| 222 |
inputs=[],
|
| 223 |
outputs=[text_id_box, text_box, summary_a, summary_b, progress_label, result_box]
|
| 224 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
# Launch the application
|
| 227 |
if __name__ == "__main__":
|
|
|
|
| 46 |
with open(TEMP_JSON, "w") as f:
|
| 47 |
pass
|
| 48 |
|
| 49 |
+
return True, f"{repo_id}"
|
| 50 |
except Exception as e:
|
| 51 |
return False, f"Error initializing HuggingFace dataset: {str(e)}"
|
| 52 |
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return False, f"Error pushing to HuggingFace: {str(e)}"
|
| 79 |
|
| 80 |
+
def save_choice(text_id, original_text, summary_a, summary_b, choice, notes="", request_id=""):
|
| 81 |
"""Save the user's choice locally and to HuggingFace dataset"""
|
| 82 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 83 |
chosen_summary = "A" if choice == "Summary A" else "B"
|
|
|
|
| 90 |
"summary_a": summary_a,
|
| 91 |
"summary_b": summary_b,
|
| 92 |
"chosen_summary": chosen_summary,
|
| 93 |
+
"notes": notes,
|
| 94 |
+
"request_id": request_id
|
| 95 |
}
|
| 96 |
|
| 97 |
# Save locally
|
|
|
|
| 106 |
# Push to HuggingFace
|
| 107 |
success, message = push_to_hf_dataset(new_row)
|
| 108 |
|
| 109 |
+
request_id_msg = f" (Request ID: {request_id})" if request_id else ""
|
| 110 |
+
|
| 111 |
if success:
|
| 112 |
+
return f"Selection saved for text ID: {text_id}{request_id_msg}! You chose {'Summary A' if choice == 'Summary A' else 'Summary B'}. Pushed to HuggingFace."
|
| 113 |
else:
|
| 114 |
+
return f"Selection saved locally for text ID: {text_id}{request_id_msg}. HuggingFace push failed: {message}"
|
| 115 |
|
| 116 |
class SummaryChooser:
|
| 117 |
def __init__(self):
|
|
|
|
| 119 |
self.current_index = 0
|
| 120 |
self.total_items = len(self.df)
|
| 121 |
self.hf_status = initialize_hf_dataset()
|
| 122 |
+
self.request_id = "" # Initialize empty request ID
|
| 123 |
+
|
| 124 |
+
def set_request_id(self, request: gr.Request):
|
| 125 |
+
"""Set the request ID from the URL query parameters"""
|
| 126 |
+
try:
|
| 127 |
+
query_params = request.query_params
|
| 128 |
+
self.request_id = query_params.get("id", "")
|
| 129 |
+
return f"Request ID: {self.request_id}" if self.request_id else "No Request ID provided"
|
| 130 |
+
except:
|
| 131 |
+
self.request_id = ""
|
| 132 |
+
return "Failed to get Request ID"
|
| 133 |
|
| 134 |
def get_current_item(self):
|
| 135 |
"""Get the current item from the dataframe"""
|
|
|
|
| 148 |
# Get current values
|
| 149 |
text_id, text, summary_a, summary_b, _ = self.get_current_item()
|
| 150 |
|
| 151 |
+
# Save the choice with the request ID
|
| 152 |
+
result_message = save_choice(text_id, text, summary_a, summary_b, choice, notes, self.request_id)
|
| 153 |
|
| 154 |
# Move to next item or wrap around
|
| 155 |
self.current_index = (self.current_index + 1) % self.total_items
|
|
|
|
| 173 |
def get_hf_status(self):
|
| 174 |
"""Get the status of HuggingFace integration"""
|
| 175 |
success, message = self.hf_status
|
| 176 |
+
return f"{'Connected' if success else 'Not Connected'} - {message}"
|
| 177 |
|
| 178 |
# Create the application
|
| 179 |
app = SummaryChooser()
|
|
|
|
| 185 |
|
| 186 |
with gr.Row():
|
| 187 |
with gr.Column():
|
| 188 |
+
progress_label = gr.Label(label="Progress")
|
| 189 |
+
|
| 190 |
with gr.Column():
|
| 191 |
hf_status = gr.Label(label="HuggingFace Status", value=app.get_hf_status())
|
| 192 |
+
|
| 193 |
+
with gr.Column():
|
| 194 |
+
request_id_label = gr.Label(label="Request ID")
|
| 195 |
+
|
| 196 |
with gr.Column():
|
| 197 |
text_id_box = gr.Textbox(label="Text ID", interactive=False)
|
| 198 |
|
|
|
|
| 241 |
inputs=[],
|
| 242 |
outputs=[text_id_box, text_box, summary_a, summary_b, progress_label, result_box]
|
| 243 |
)
|
| 244 |
+
|
| 245 |
+
# Load the request ID from the URL when the page loads
|
| 246 |
+
interface.load(
|
| 247 |
+
fn=app.set_request_id,
|
| 248 |
+
inputs=[],
|
| 249 |
+
outputs=[request_id_label]
|
| 250 |
+
)
|
| 251 |
|
| 252 |
# Launch the application
|
| 253 |
if __name__ == "__main__":
|