dhruv575 commited on
Commit ·
12e6f4c
1
Parent(s): 7f082cb
Full Download
Browse files
app.py
CHANGED
|
@@ -4,7 +4,9 @@ Provides endpoints for database operations on prompts and responses.
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
from flask_cors import CORS
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from database import Database
|
|
@@ -545,6 +547,65 @@ def health_check():
|
|
| 545 |
return jsonify({'success': False, 'error': str(e)}), 500
|
| 546 |
|
| 547 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
if __name__ == '__main__':
|
| 549 |
port = int(os.environ.get('PORT', 7860))
|
| 550 |
app.run(host='0.0.0.0', port=port, debug=False)
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
+
import csv
|
| 8 |
+
import io
|
| 9 |
+
from flask import Flask, request, jsonify, Response
|
| 10 |
from flask_cors import CORS
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
from database import Database
|
|
|
|
| 547 |
return jsonify({'success': False, 'error': str(e)}), 500
|
| 548 |
|
| 549 |
|
| 550 |
+
# ========== CSV DOWNLOAD ENDPOINTS ==========
|
| 551 |
+
|
| 552 |
+
@app.route('/api/download/csv', methods=['GET'])
|
| 553 |
+
def download_csv():
|
| 554 |
+
"""
|
| 555 |
+
Download CSV files for prompts and responses tables.
|
| 556 |
+
Returns a zip file or individual CSV files.
|
| 557 |
+
"""
|
| 558 |
+
try:
|
| 559 |
+
# Get all prompts
|
| 560 |
+
prompts = db.get_all_prompts(limit=None, offset=0)
|
| 561 |
+
|
| 562 |
+
# Get all responses
|
| 563 |
+
responses = db.get_all_responses(limit=None, offset=0)
|
| 564 |
+
|
| 565 |
+
# Create CSV for prompts
|
| 566 |
+
prompts_output = io.StringIO()
|
| 567 |
+
prompts_writer = csv.writer(prompts_output)
|
| 568 |
+
prompts_writer.writerow(['ID', 'Text', 'Note'])
|
| 569 |
+
for prompt in prompts:
|
| 570 |
+
prompts_writer.writerow([
|
| 571 |
+
prompt.get('id', ''),
|
| 572 |
+
prompt.get('text', ''),
|
| 573 |
+
prompt.get('note', '') or ''
|
| 574 |
+
])
|
| 575 |
+
prompts_csv = prompts_output.getvalue()
|
| 576 |
+
|
| 577 |
+
# Create CSV for responses
|
| 578 |
+
responses_output = io.StringIO()
|
| 579 |
+
responses_writer = csv.writer(responses_output)
|
| 580 |
+
responses_writer.writerow(['ID', 'Prompt ID', 'LLM', 'Response', 'Jailbroken', 'Note'])
|
| 581 |
+
for response in responses:
|
| 582 |
+
responses_writer.writerow([
|
| 583 |
+
response.get('id', ''),
|
| 584 |
+
response.get('prompt_id', ''),
|
| 585 |
+
response.get('llm', ''),
|
| 586 |
+
response.get('response', ''),
|
| 587 |
+
'Yes' if response.get('jailbroken', False) else 'No',
|
| 588 |
+
response.get('note', '') or ''
|
| 589 |
+
])
|
| 590 |
+
responses_csv = responses_output.getvalue()
|
| 591 |
+
|
| 592 |
+
# Return both CSVs as JSON (frontend will handle download)
|
| 593 |
+
return jsonify({
|
| 594 |
+
'success': True,
|
| 595 |
+
'data': {
|
| 596 |
+
'prompts': prompts_csv,
|
| 597 |
+
'responses': responses_csv,
|
| 598 |
+
'prompts_count': len(prompts),
|
| 599 |
+
'responses_count': len(responses)
|
| 600 |
+
}
|
| 601 |
+
}), 200
|
| 602 |
+
except Exception as e:
|
| 603 |
+
import traceback
|
| 604 |
+
print(f"Error in download_csv: {e}")
|
| 605 |
+
print(traceback.format_exc())
|
| 606 |
+
return jsonify({'error': str(e)}), 500
|
| 607 |
+
|
| 608 |
+
|
| 609 |
if __name__ == '__main__':
|
| 610 |
port = int(os.environ.get('PORT', 7860))
|
| 611 |
app.run(host='0.0.0.0', port=port, debug=False)
|