Manojajj commited on
Commit
955e522
·
verified ·
1 Parent(s): 1a13bae

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import fitz # PyMuPDF for PDF extraction
3
+ from huggingface_hub import InferenceClient
4
+ import os
5
+
6
+ # Function to extract text from PDF
7
+ def extract_text_from_pdf(pdf_path):
8
+ try:
9
+ doc = fitz.open(pdf_path)
10
+ text = ""
11
+ for page in doc:
12
+ text += page.get_text()
13
+ return text
14
+ except Exception as e:
15
+ print(f"Error extracting text from PDF: {str(e)}")
16
+ return None
17
+
18
+ # Function to send extracted text to the model and get the details
19
+ def extract_invoice_details_from_text(text, prompt, model_name, api_key):
20
+ try:
21
+ # Initialize the InferenceClient with the provided API key
22
+ client = InferenceClient(api_key=api_key)
23
+
24
+ # Send prompt to the selected model
25
+ response = client.chat.completions.create(
26
+ model=model_name, # Use the model selected by the user
27
+ messages=[{"role": "user", "content": prompt + text}], # Append extracted PDF text to prompt
28
+ max_tokens=2000 # Adjust token size as needed
29
+ )
30
+ # Ensure the response format is correct and extract the content
31
+ return response['choices'][0]['message']['content']
32
+ except Exception as e:
33
+ print(f"Error occurred while processing the request: {str(e)}")
34
+ return "Error occurred while processing the request."
35
+
36
+ # Main function to process PDFs
37
+ def process_files(files, prompt, model_name, api_key):
38
+ if not api_key.strip():
39
+ return "Please provide a valid Hugging Face API key."
40
+
41
+ default_prompt = (
42
+ "Can you please parse below details from attached documents in excel format?\n"
43
+ "information to extract: DATE, NAME & ADDRESS OF BUYER, Item Code, HSN CODE, UOM, Qty, Unit Price\n\nInvoice text:\n"
44
+ )
45
+ user_prompt = prompt.strip() if prompt.strip() else default_prompt
46
+ all_extracted_data = []
47
+
48
+ for file in files:
49
+ try:
50
+ pdf_text = extract_text_from_pdf(file.name)
51
+ if not pdf_text:
52
+ all_extracted_data.append(f"Failed to extract text from {file.name}")
53
+ continue
54
+
55
+ # Get details from the model based on extracted text and the provided prompt
56
+ extracted_text = extract_invoice_details_from_text(pdf_text, user_prompt, model_name, api_key)
57
+ all_extracted_data.append(f"File: {file.name}\n{extracted_text.strip()}")
58
+ except Exception as e:
59
+ print(f"Error processing file {file.name}: {str(e)}")
60
+ all_extracted_data.append(f"Error processing {file.name}: {str(e)}")
61
+
62
+ return "\n\n".join(all_extracted_data)
63
+
64
+ # Define the Gradio app interface
65
+ with gr.Blocks() as app:
66
+ gr.Markdown("# Information Parser App")
67
+ gr.Markdown("Upload PDF with text, provide a prompt, your Hugging Face API key, and select a model to extract details.")
68
+
69
+ with gr.Row():
70
+ file_input = gr.File(label="Upload PDF(s)", file_types=[".pdf"], file_count="multiple")
71
+ model_dropdown = gr.Dropdown(
72
+ label="Select Model",
73
+ choices=["Qwen/Qwen2.5-Coder-32B-Instruct", "gpt-3.5-turbo", "other-model-name"],
74
+ value="Qwen/Qwen2.5-Coder-32B-Instruct"
75
+ )
76
+ prompt_input = gr.Textbox(
77
+ label="Custom Prompt",
78
+ placeholder="Enter your custom prompt here (leave blank to use default prompt).",
79
+ lines=3
80
+ )
81
+ api_key_input = gr.Textbox(
82
+ label="Hugging Face API Key",
83
+ placeholder="Enter your Hugging Face API key here.",
84
+ type="password",
85
+ lines=1
86
+ )
87
+ extract_button = gr.Button("Extract Details from PDF")
88
+ output_box = gr.Textbox(
89
+ label="Extracted Data",
90
+ placeholder="The extracted details will appear here.",
91
+ lines=15,
92
+ interactive=False
93
+ )
94
+
95
+ extract_button.click(
96
+ process_files,
97
+ inputs=[file_input, prompt_input, model_dropdown, api_key_input],
98
+ outputs=output_box
99
+ )
100
+
101
+ # Launch the app
102
+ app.launch()