ignaciaginting commited on
Commit
005a185
·
verified ·
1 Parent(s): 6d0fad7

app update

Browse files
Files changed (1) hide show
  1. app.py +38 -24
app.py CHANGED
@@ -1,27 +1,41 @@
1
- import gradio as gr
2
- from modelscope.pipelines import pipeline
3
- from modelscope.utils.constant import Tasks
4
  from huggingface_hub import snapshot_download
 
 
 
5
  import os
6
 
7
- # Step 1: Download the model
8
- model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0')
9
-
10
- # Step 2: Initialize pipeline
11
- pipe = pipeline(
12
- task=Tasks.document_segmentation,
13
- model=model_dir
14
- )
15
-
16
- # Step 3: Define inference function
17
- def extract_info_from_pdf(pdf_file):
18
- result = pipe({'file': pdf_file.name})
19
- return str(result)
20
-
21
- # Step 4: Gradio UI
22
- gr.Interface(
23
- fn=extract_info_from_pdf,
24
- inputs=gr.File(type="binary", label="Upload PDF"),
25
- outputs="text",
26
- title="PDF Extractor (PDF-Extract-Kit)"
27
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
 
 
2
  from huggingface_hub import snapshot_download
3
+ from pdf2image import convert_from_bytes
4
+ from PIL import Image
5
+ import torch
6
  import os
7
 
8
+ st.set_page_config(page_title="PDF Extract Kit QA", layout="centered")
9
+
10
+ @st.cache_resource
11
+ def load_model():
12
+ model_dir = snapshot_download(repo_id="opendatalab/pdf-extract-kit-1.0", local_dir="./pdf_model", max_workers=4)
13
+ # TODO: Load model from model_dir using correct logic, e.g.:
14
+ # model = torch.load(os.path.join(model_dir, "model.pt"))
15
+ # return model
16
+ return model_dir # TEMP placeholder
17
+
18
+ model_or_dir = load_model()
19
+
20
+ def extract_answer(image, question):
21
+ # TODO: Implement the actual inference using the model
22
+ # For now, we return a placeholder
23
+ return "Answering is not implemented yet. Replace this with model inference."
24
+
25
+ st.title("📄 PDF Extract Kit: Question Answering")
26
+
27
+ uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
28
+ question = st.text_input("Ask a question about the document")
29
+
30
+ if uploaded_file and question:
31
+ st.write("Reading and converting PDF...")
32
+ images = convert_from_bytes(uploaded_file.read(), dpi=200)
33
+
34
+ page_number = st.number_input("Select page", min_value=1, max_value=len(images), value=1, step=1)
35
+ page_image = images[page_number - 1]
36
+ st.image(page_image, caption=f"Page {page_number}")
37
+
38
+ with st.spinner("Finding answer..."):
39
+ answer = extract_answer(page_image, question)
40
+ st.success("Answer:")
41
+ st.write(answer)