Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import base64
|
4 |
+
import io
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Use Hugging Face Secrets to hide API Key
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
10 |
+
|
11 |
+
# Prompt definition
|
12 |
+
prompt = """
|
13 |
+
You are analyzing a government employment application form from Namibia.
|
14 |
+
Extract the following fields as JSON:
|
15 |
+
- Position applied for
|
16 |
+
- Office/Ministry
|
17 |
+
- Duty station
|
18 |
+
- First name(s)
|
19 |
+
- Surname
|
20 |
+
- Date of birth
|
21 |
+
- Gender
|
22 |
+
- Citizenship
|
23 |
+
- Postal Address
|
24 |
+
- Residential Address
|
25 |
+
- Email
|
26 |
+
- Phone number (mobile)
|
27 |
+
"""
|
28 |
+
|
29 |
+
def process_image(image: Image.Image):
|
30 |
+
buffered = io.BytesIO()
|
31 |
+
image.save(buffered, format="JPEG")
|
32 |
+
base64_image = base64.b64encode(buffered.getvalue()).decode()
|
33 |
+
|
34 |
+
response = openai.chat.completions.create(
|
35 |
+
model="gpt-4o",
|
36 |
+
messages=[
|
37 |
+
{"role": "user", "content": [
|
38 |
+
{"type": "text", "text": prompt},
|
39 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
40 |
+
]}
|
41 |
+
],
|
42 |
+
max_tokens=1000
|
43 |
+
)
|
44 |
+
|
45 |
+
return response.choices[0].message.content
|
46 |
+
|
47 |
+
# Gradio interface
|
48 |
+
demo = gr.Interface(
|
49 |
+
fn=process_image,
|
50 |
+
inputs=gr.Image(type="pil"),
|
51 |
+
outputs="textbox",
|
52 |
+
title="Namibia Form Extractor",
|
53 |
+
description="Upload a scanned Namibia government employment form to extract key fields."
|
54 |
+
)
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|