Spaces:
Runtime error
Runtime error
Commit
·
c412123
1
Parent(s):
f801525
integrated save ai generated text to the bucket
Browse files
app.py
CHANGED
@@ -441,6 +441,85 @@ def update_structure(format_choice):
|
|
441 |
return gr.update(value="Introduction, Body, Conclusion", interactive=True)
|
442 |
|
443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
444 |
def generate_and_format(
|
445 |
input_role,
|
446 |
topic,
|
@@ -525,6 +604,35 @@ def generate_and_format(
|
|
525 |
reference_formatted = format_references(article)
|
526 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
527 |
history.append((f"Generated Text | {timestamp}\nInput: {topic}", reference_formatted))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
528 |
return reference_formatted, history
|
529 |
|
530 |
|
|
|
441 |
return gr.update(value="Introduction, Body, Conclusion", interactive=True)
|
442 |
|
443 |
|
444 |
+
import uuid
|
445 |
+
import json
|
446 |
+
from datetime import datetime
|
447 |
+
from google.cloud import storage
|
448 |
+
|
449 |
+
# Initialize Google Cloud Storage client
|
450 |
+
client = storage.Client()
|
451 |
+
bucket_name = "ai-source-detection"
|
452 |
+
bucket = client.bucket(bucket_name)
|
453 |
+
|
454 |
+
|
455 |
+
def save_to_cloud_storage(
|
456 |
+
article,
|
457 |
+
input_role,
|
458 |
+
topic_context,
|
459 |
+
context,
|
460 |
+
keywords,
|
461 |
+
article_length,
|
462 |
+
format,
|
463 |
+
writing_style,
|
464 |
+
tone,
|
465 |
+
user_category,
|
466 |
+
depth_of_content,
|
467 |
+
structure,
|
468 |
+
references,
|
469 |
+
num_examples,
|
470 |
+
conclusion_type,
|
471 |
+
ai_model,
|
472 |
+
content_string,
|
473 |
+
url_content,
|
474 |
+
generated_article,
|
475 |
+
user_comments,
|
476 |
+
timestamp,
|
477 |
+
):
|
478 |
+
"""Save generated article and metadata to Google Cloud Storage within a specific folder."""
|
479 |
+
# Create a unique filename
|
480 |
+
file_id = str(uuid.uuid4())
|
481 |
+
|
482 |
+
# Define the file path and name in the bucket
|
483 |
+
folder_path = "ai-writer/"
|
484 |
+
file_name = f"{folder_path}{timestamp.replace(' ', '_').replace(':', '-')}_{file_id}.json"
|
485 |
+
|
486 |
+
# Create a dictionary with the article and all relevant metadata
|
487 |
+
data = {
|
488 |
+
"article": article,
|
489 |
+
"metadata": {
|
490 |
+
"input_role": input_role,
|
491 |
+
"topic_context": topic_context,
|
492 |
+
"context": context,
|
493 |
+
"keywords": keywords,
|
494 |
+
"article_length": article_length,
|
495 |
+
"format": format,
|
496 |
+
"writing_style": writing_style,
|
497 |
+
"tone": tone,
|
498 |
+
"user_category": user_category,
|
499 |
+
"depth_of_content": depth_of_content,
|
500 |
+
"structure": structure,
|
501 |
+
"references": references,
|
502 |
+
"num_examples": num_examples,
|
503 |
+
"conclusion_type": conclusion_type,
|
504 |
+
"ai_model": ai_model,
|
505 |
+
"content_string": content_string,
|
506 |
+
"url_content": url_content,
|
507 |
+
"generated_article": generated_article,
|
508 |
+
"user_comments": user_comments,
|
509 |
+
"timestamp": timestamp,
|
510 |
+
},
|
511 |
+
}
|
512 |
+
|
513 |
+
# Convert data to JSON string
|
514 |
+
json_data = json.dumps(data)
|
515 |
+
|
516 |
+
# Create a blob and upload to GCS
|
517 |
+
blob = bucket.blob(file_name)
|
518 |
+
blob.upload_from_string(json_data, content_type="application/json")
|
519 |
+
|
520 |
+
return f"Data saved as {file_name} in GCS."
|
521 |
+
|
522 |
+
|
523 |
def generate_and_format(
|
524 |
input_role,
|
525 |
topic,
|
|
|
604 |
reference_formatted = format_references(article)
|
605 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
606 |
history.append((f"Generated Text | {timestamp}\nInput: {topic}", reference_formatted))
|
607 |
+
|
608 |
+
# Save the article and metadata to Cloud Storage
|
609 |
+
# We dont save if there is PDF input for privacy reasons
|
610 |
+
if pdf_file_input is None:
|
611 |
+
save_message = save_to_cloud_storage(
|
612 |
+
article,
|
613 |
+
input_role,
|
614 |
+
topic_context,
|
615 |
+
context,
|
616 |
+
keywords,
|
617 |
+
article_length,
|
618 |
+
format,
|
619 |
+
writing_style,
|
620 |
+
tone,
|
621 |
+
user_category,
|
622 |
+
depth_of_content,
|
623 |
+
structure,
|
624 |
+
references,
|
625 |
+
num_examples,
|
626 |
+
conclusion_type,
|
627 |
+
ai_model,
|
628 |
+
content_string,
|
629 |
+
url_content,
|
630 |
+
generated_article,
|
631 |
+
user_comments,
|
632 |
+
timestamp,
|
633 |
+
)
|
634 |
+
print(save_message)
|
635 |
+
|
636 |
return reference_formatted, history
|
637 |
|
638 |
|