Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import PIL.Image
|
4 |
+
from pdf2image import convert_from_bytes
|
5 |
+
from google import genai
|
6 |
+
from google.genai import types
|
7 |
+
|
8 |
+
# Retrieve the API key from the environment variable.
|
9 |
+
API_KEY = os.getenv("API_KEY")
|
10 |
+
if API_KEY is None:
|
11 |
+
st.error("API Key not found. Please set the API_KEY environment variable.")
|
12 |
+
st.stop()
|
13 |
+
|
14 |
+
# Initialize the GenAI client.
|
15 |
+
client = genai.Client(api_key=API_KEY)
|
16 |
+
|
17 |
+
def extract_text_from_image(img):
|
18 |
+
"""
|
19 |
+
Extracts text from a PIL image using the Google GenAI API.
|
20 |
+
"""
|
21 |
+
response = client.models.generate_content(
|
22 |
+
model="gemini-2.0-flash",
|
23 |
+
contents=["Extract the text from the image. Do not write anything except the extracted content", img])
|
24 |
+
return response.text
|
25 |
+
|
26 |
+
st.title("PDF/Image Text Extraction using Google GenAI")
|
27 |
+
|
28 |
+
# File uploader (accepts PDF and common image formats).
|
29 |
+
uploaded_file = st.file_uploader("Upload a PDF or image file", type=["pdf", "png", "jpg", "jpeg", "webp"])
|
30 |
+
|
31 |
+
if uploaded_file is not None:
|
32 |
+
output_text = ""
|
33 |
+
st.write("**Uploaded File:**", uploaded_file.name)
|
34 |
+
|
35 |
+
# Process PDF files.
|
36 |
+
if uploaded_file.name.lower().endswith(".pdf"):
|
37 |
+
st.info("Processing PDF file...")
|
38 |
+
with st.spinner("Converting PDF pages to images..."):
|
39 |
+
images = convert_from_bytes(uploaded_file.read(), dpi=200)
|
40 |
+
|
41 |
+
for idx, img in enumerate(images, start=1):
|
42 |
+
with st.spinner(f"Extracting text from page {idx}..."):
|
43 |
+
page_text = extract_text_from_image(img)
|
44 |
+
output_text += f"### Page {idx}\n\n{page_text}\n\n"
|
45 |
+
else:
|
46 |
+
# Process image files.
|
47 |
+
st.info("Processing image file...")
|
48 |
+
with st.spinner("Extracting text from image..."):
|
49 |
+
img = PIL.Image.open(uploaded_file)
|
50 |
+
output_text += extract_text_from_image(img) + "\n\n"
|
51 |
+
|
52 |
+
st.success("Extraction complete!")
|
53 |
+
|
54 |
+
st.markdown("### Extracted Text:")
|
55 |
+
st.markdown(output_text)
|
56 |
+
|
57 |
+
# Provide a download button for the Markdown file.
|
58 |
+
md_bytes = output_text.encode("utf-8")
|
59 |
+
st.download_button(label="Download Markdown file", data=md_bytes, file_name="output.md", mime="text/markdown")
|