File size: 3,852 Bytes
6ea2bcc
 
a10c7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea2bcc
 
6ddaa39
a10c7a6
 
 
6ddaa39
a10c7a6
 
 
 
6ea2bcc
 
 
 
a10c7a6
 
 
 
 
 
6ddaa39
 
 
a10c7a6
6ea2bcc
a10c7a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea2bcc
 
a10c7a6
 
 
 
6ddaa39
 
a10c7a6
 
6ea2bcc
 
a10c7a6
6ea2bcc
6ddaa39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ea2bcc
 
a10c7a6
6ea2bcc
 
a10c7a6
 
 
 
 
6ea2bcc
 
 
 
6ddaa39
6ea2bcc
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import gradio as gr
import requests
from PyPDF2 import PdfReader
import os

# Function to validate PDF file
def is_valid_pdf(file_path):
    try:
        # If file_path is a string (Gradio provides a temporary file path)
        if isinstance(file_path, str) and os.path.exists(file_path):
            with open(file_path, "rb") as f:
                pdf = PdfReader(f)
                if len(pdf.pages) > 0:
                    return True, f"Valid PDF with {len(pdf.pages)} pages"
                return False, "Invalid PDF: No pages found"
        else:
            return False, "Invalid PDF: File path is not valid"
    except Exception as e:
        return False, f"Invalid PDF: {str(e)}"

# Function to send the POST request to the API
def extract_text_from_pdf(pdf_file, page_number, src_lang, tgt_lang, prompt):
    if not pdf_file:
        return "Error: No file uploaded. Please upload a PDF file."

    # Validate the PDF
    valid, message = is_valid_pdf(pdf_file)
    if not valid:
        return f"Error: {message}. Please upload a valid PDF file or repair the current one."

    # API endpoint
    url = "http://209.20.158.215:7861/extract-text-eng/"

    # Prepare the payload
    with open(pdf_file, "rb") as f:
        files = {
            "file": ("uploaded.pdf", f, "application/pdf")
        }
        data = {
            "page_number": str(page_number),
            "src_lang": src_lang,
            "tgt_lang": tgt_lang,
            "prompt": prompt
        }

        # Headers
        headers = {
            "accept": "application/json"
        }

        try:
            # Send the POST request
            response = requests.post(url, files=files, data=data, headers=headers)
            
            # Check if the request was successful
            if response.status_code == 200:
                result = response.json()
                page_content = result.get("page_content", "No description returned from API")
                return page_content
            else:
                return f"Error: {response.status_code} - {response.text}"
        except Exception as e:
            return f"Error: Failed to connect to the API - {str(e)}"

# Gradio interface
with gr.Blocks(title="PDF Content Description") as demo:
    gr.Markdown("# PDF Content Description Extractor")
    gr.Markdown(
        """
        Upload a PDF file (e.g., Dhwani-AI-Pitch-Europe.pdf) and specify parameters to extract a description of its content.
        The API will analyze the page and return a textual description based on the provided prompt and languages.
        """
    )
    
    # Input components
    pdf_input = gr.File(label="Upload PDF File", file_types=[".pdf"], type="filepath")
    page_number_input = gr.Number(label="Page Number", value=1, precision=0, minimum=1)
    src_lang_input = gr.Textbox(
        label="Source Language",
        value="eng_Latn",
        placeholder="Enter source language (e.g., eng_Latn)"
    )
    tgt_lang_input = gr.Textbox(
        label="Target Language",
        value="eng_Latn",
        placeholder="Enter target language (e.g., eng_Latn)"
    )
    prompt_input = gr.Textbox(
        label="Prompt",
        value="describe the image",
        placeholder="Enter prompt (e.g., describe the image)"
    )
    
    # Submit button
    submit_button = gr.Button("Extract Description")
    
    # Output component
    output_text = gr.Textbox(
        label="Content Description",
        lines=10,
        placeholder="The API response will appear here, describing the content of the specified PDF page."
    )
    
    # Connect the button to the function
    submit_button.click(
        fn=extract_text_from_pdf,
        inputs=[pdf_input, page_number_input, src_lang_input, tgt_lang_input, prompt_input],
        outputs=output_text
    )

# Launch the Gradio app
demo.launch()