Rajarshi255 commited on
Commit
9646da0
Β·
verified Β·
1 Parent(s): 2ecc2ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py CHANGED
@@ -71,6 +71,61 @@ Return ONLY raw JSON (no markdown fences, no labels), exactly:
71
  - If no grammar issues, use an empty list [].
72
  - Numbers only for scores.
73
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  def student_key(name, klass):
75
  first = str(name).strip().split()[0].lower()
76
  return f"{first}_{str(klass).strip().lower()}"
@@ -524,6 +579,11 @@ STUDENT SUMMARY (ORIGINAL TEXT BELOW):
524
 
525
  #status_text = "Saved to Google Sheet βœ…" if sheets_status == "OK" else f"Not saved ❌ β€” {sheets_status}"
526
  status_text = sheets_status
 
 
 
 
 
527
 
528
 
529
  return md_str, corrected, status_text
 
71
  - If no grammar issues, use an empty list [].
72
  - Numbers only for scores.
73
  """
74
+ def save_to_master_doc(name, klass, title, summary):
75
+ from googleapiclient.discovery import build
76
+ from google.oauth2.service_account import Credentials
77
+ import json, os, datetime
78
+
79
+ # Use the ID from your document link:
80
+ MASTER_DOC_ID = "1IW_XsQsR3AIHpaxY3tJZuEujClC_KIHb0QeYhKLYbUM"
81
+
82
+ # Use the secret name confirmed for your Hugging Face Space
83
+ secret_data = os.environ.get("gcp_service_account")
84
+
85
+ if not secret_data:
86
+ return "Doc Error: Secret 'gcp_service_account' missing"
87
+
88
+ try:
89
+ creds = Credentials.from_service_account_info(
90
+ json.loads(secret_data),
91
+ scopes=["https://www.googleapis.com/auth/documents"]
92
+ )
93
+ service = build('docs', 'v1', credentials=creds)
94
+
95
+ # 1. Format the Entry with "Metadata Anchors" for future extraction
96
+ timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
97
+
98
+ # This structure allows a code to easily split the file later
99
+ new_entry = (
100
+ f"\n\n### START_ENTRY ###\n"
101
+ f"[[METADATA|NAME:{name.strip()}|CLASS:{klass.strip()}|DATE:{timestamp}|TITLE:{title.strip()}]]\n"
102
+ f"--- ESSAY START ---\n"
103
+ f"{summary.strip()}\n"
104
+ f"--- ESSAY END ---\n"
105
+ f"### END_ENTRY ###\n"
106
+ )
107
+
108
+ # 2. Find the end of the document to append the new text
109
+ doc = service.documents().get(documentId=MASTER_DOC_ID).execute()
110
+ # endIndex - 1 is the position just before the very end of the doc
111
+ end_index = doc.get('body').get('content')[-1].get('endIndex') - 1
112
+
113
+ requests = [
114
+ {
115
+ 'insertText': {
116
+ 'location': {'index': end_index},
117
+ 'text': new_entry
118
+ }
119
+ }
120
+ ]
121
+
122
+ # 3. Append the formatted entry to the master doc
123
+ service.documents().batchUpdate(documentId=MASTER_DOC_ID, body={'requests': requests}).execute()
124
+ return "βœ… Essay appended to Master Doc"
125
+
126
+ except Exception as e:
127
+ print(f"Doc Append Error: {e}")
128
+ return f"❌ Doc Error: {e}"
129
  def student_key(name, klass):
130
  first = str(name).strip().split()[0].lower()
131
  return f"{first}_{str(klass).strip().lower()}"
 
579
 
580
  #status_text = "Saved to Google Sheet βœ…" if sheets_status == "OK" else f"Not saved ❌ β€” {sheets_status}"
581
  status_text = sheets_status
582
+ # After your sheets_status call:
583
+ doc_status = save_to_master_doc(student_name, student_class, title, student_summary)
584
+
585
+ # Update your final status message to show both results
586
+ status_text = f"{sheets_status} | {doc_status}"
587
 
588
 
589
  return md_str, corrected, status_text