Team Ai
Apppublic

Multimedika/Bot_Development

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
reader_v3.py58 linesDownload Raw Back to service
1import os2import nest_asyncio3 4from llama_parse import LlamaParse5from dotenv import load_dotenv6from fastapi import UploadFile7from fastapi.responses import JSONResponse8 9from script.get_metadata import Metadata10 11load_dotenv()12nest_asyncio.apply()13 14 15def parse_journal(content: bytes, file_name: str):16    """Parse the journal using LlamaParse."""17    try:18        # Initialize the parser19        parser = LlamaParse(20            api_key=os.getenv("LLAMA_PARSE_API_KEY"),21            result_type="markdown",22            # use_vendor_multimodal_model=True,23            # vendor_multimodal_model_name="openai-gpt-4o-mini",24        )25 26        # Load and process the document27        llama_parse_documents = parser.load_data(28            content, extra_info={"file_name": file_name}29        )30 31        return llama_parse_documents32 33    except Exception as e:34        return JSONResponse(status_code=400, content=f"Error processing file: {e}")35 36async def upload_file(reference, file: UploadFile):37    try:38        # Read the binary content of the uploaded file once39        content = await file.read()40        # Parse the journal41        parsed_documents = parse_journal(content, file.filename)42        # Extract metadata43        # metadata_dict = await extract_metadata(content)44        # print("Metadata Dictionary : \n\n", metadata_dict)45 46        metadata_gen = Metadata(reference)47        documents_with_metadata = metadata_gen.apply_metadata(parsed_documents)48        49        # document_with_metadata = 50 51        print("Document with Metadata : \n\n", documents_with_metadata)52        print("Banyak documents : \n", len(documents_with_metadata))53 54        # Return both parsed documents and metadata55        return documents_with_metadata56 57    except Exception as e:58        return JSONResponse(status_code=500, content=f"Error processing file: {e}")