MCPcopy Create free account
hub / github.com/clockworknowledge/menome_processor / process_documents

Function process_documents

app/routers/processing.py:112–156  ·  view source on GitHub ↗
(
    document_limit: int = Query(default=None, description="Limit on number of documents to process"),
    generateQuestions: bool = Query(default=False, description="Flag to generate questions"),
    generateSummaries: bool = Query(default=False, description="Flag to generate summaries"),
    current_user: User = Depends(get_current_user))

Source from the content-addressed store, hash-verified

110 , description="Process documents in the database"
111 , summary="Process documents in the database that have not had pages, questions or summaries created. Helps illustrate async processing")
112async def process_documents(
113 document_limit: int = Query(default=None, description="Limit on number of documents to process"),
114 generateQuestions: bool = Query(default=False, description="Flag to generate questions"),
115 generateSummaries: bool = Query(default=False, description="Flag to generate summaries"),
116 current_user: User = Depends(get_current_user)):
117 logging.basicConfig(level=logging.INFO)
118
119 # Setup neo4j driver
120 driver = GraphDatabase.driver(AppConfig.NEO4J_URI, auth=(AppConfig.NEO4J_USER, AppConfig.NEO4J_PASSWORD))
121
122 query = "MATCH (a:Document) WHERE NOT (a)-[:HAS_PAGE]->(:Page) and a.text <> '' and a.process=True RETURN a.uuid as uuid"
123 if document_limit is not None:
124 query += f" LIMIT {document_limit}"
125
126 logging.info("Querying for documents to process.")
127 document_ids = []
128 with driver.session() as session:
129 result = session.run(query)
130 document_ids = [record['uuid'] for record in result]
131 session.close()
132
133 logging.info(f"Found {len(document_ids)} documents to process.")
134
135 task_ids = []
136 for document_id in document_ids:
137 try:
138 logging.info(f"Queueing document {document_id} for processing.")
139 with driver.session() as session:
140 result = session.run("MATCH (a:Document {uuid: $uuid}) RETURN a", {"uuid": document_id})
141 document_data = result.single().value()
142 text = document_data['text']
143 # Pass the generateQuestions and generateSummaries flags to the task
144 task = process_text_task.delay(text, document_id, generateQuestions, generateSummaries)
145 task_ids.append(task.id)
146 logging.info(f"Queued document {document_id} with task ID {task.id}")
147 except Exception as e:
148 logging.error(f"Failed to queue document {document_id}: {e}")
149 finally:
150 session.close()
151 driver.close()
152
153 return {
154 "message": f"Processing started for {len(document_ids)} documents",
155 "task_ids": task_ids
156 }
157
158## Returns an access token based on username and password
159@router.post("/token", response_model=Token, description="Returns an access token", summary="Returns an access token", tags=["Users"])

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected