@@ -258,28 +258,43 @@ def extract_text_from_pdf(pdf_file_path):
258258 except Exception as e :
259259 logger .error (f"Failed to extract text from PDF: { e } " )
260260 return None
261+
262+ import os
263+ import nest_asyncio
264+ from llama_parse import LlamaParse
265+
266+ nest_asyncio .apply () # Ensure compatibility with async environments
267+
268+ def pdf_parse (pdf_path ):
269+ # Set API key, can also be configured in the environment
270+ api_key = "LLAMA API"
271+
272+ # Initialize the LlamaParse object
273+ parser = LlamaParse (
274+ api_key = api_key ,
275+ result_type = "markdown" , # Output in Markdown format
276+ num_workers = 4 , # Number of API calls for batch processing
277+ verbose = True , # Print detailed logs
278+ language = "en" # Set language (default is English)
279+ )
261280
262- def llama_parse (pdf_path ):
263- os .environ ['LLAMA_CLOUD_API_KEY' ] = 'LLAMA KEY'
264-
265- output_dir = 'outputs'
281+ # Prepare the output directory and file name
282+ output_dir = "outputs"
266283 os .makedirs (output_dir , exist_ok = True )
267284
268285 pdf_name = os .path .splitext (os .path .basename (pdf_path ))[0 ]
269- markdown_file_path = os .path .join (output_dir , f' { pdf_name } .md' )
286+ markdown_file_path = os .path .join (output_dir , f" { pdf_name } .md" )
270287
271- command = [
272- 'llama-parse' ,
273- pdf_path ,
274- '--result-type' , 'markdown' ,
275- '--output-file' , markdown_file_path
276- ]
288+ # Load and parse the PDF
289+ extra_info = {"file_name" : pdf_name }
277290
278- subprocess .run (command , check = True )
291+ with open (pdf_path , "rb" ) as pdf_file :
292+ # Pass the file object and extra info for parsing
293+ documents = parser .load_data (pdf_file , extra_info = extra_info )
294+
295+ # Save the parsed content to a Markdown file
296+ markdown_content = documents [0 ].text if documents else ""
279297
280- with open (markdown_file_path , 'r' , encoding = 'utf-8' ) as file :
281- markdown_content = file .read ()
282-
283298 return markdown_content
284299
285300def wrap_query_context (user_query , query_context ):
@@ -423,7 +438,7 @@ def add_text(
423438 hint_msg = SLOW_MODEL_MSG
424439
425440 if file_extension == ".pdf" :
426- document_text = llama_parse (files [0 ])
441+ document_text = pdf_parse (files [0 ])
427442 post_processed_text = f"""
428443 The following is the content of a document:
429444
@@ -436,7 +451,7 @@ def add_text(
436451
437452 post_processed_text = wrap_query_context (text , post_processed_text )
438453
439- text = text [:BLIND_MODE_INPUT_CHAR_LEN_LIMIT ] # Hard cut-off
454+ # text = text[:BLIND_MODE_INPUT_CHAR_LEN_LIMIT] # Hard cut-off
440455 for i in range (num_sides ):
441456 states [i ].conv .append_message (states [i ].conv .roles [0 ], post_processed_text )
442457 states [i ].conv .append_message (states [i ].conv .roles [1 ], None )
@@ -553,7 +568,7 @@ def build_side_by_side_vision_ui_anony(context: Context, random_questions=None):
553568 file_types = ["file" ],
554569 show_label = False ,
555570 container = True ,
556- placeholder = "Enter your prompt or add a PDF file here " ,
571+ placeholder = "Enter your prompt here. You can also upload image or PDF file" ,
557572 elem_id = "input_box" ,
558573 scale = 3 ,
559574 )
@@ -563,11 +578,11 @@ def build_side_by_side_vision_ui_anony(context: Context, random_questions=None):
563578
564579 with gr .Row () as button_row :
565580 random_btn = gr .Button (value = "🔮 Random Image" , interactive = True )
566- if random_questions :
567- global vqa_samples
568- with open (random_questions , "r" ) as f :
569- vqa_samples = json .load (f )
570- random_btn = gr .Button (value = "🔮 Random Image" , interactive = True )
581+ # if random_questions:
582+ # global vqa_samples
583+ # with open(random_questions, "r") as f:
584+ # vqa_samples = json.load(f)
585+ # random_btn = gr.Button(value="🔮 Random Image", interactive=True)
571586 clear_btn = gr .Button (value = "🎲 New Round" , interactive = False )
572587 regenerate_btn = gr .Button (value = "🔄 Regenerate" , interactive = False )
573588 share_btn = gr .Button (value = "📷 Share" )
0 commit comments