RemiProAtos commited on
Commit
072674d
·
verified ·
1 Parent(s): 997c2f3

create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ from PIL import Image
5
+ import pytesseract
6
+ from mistralai.client import Mistral
7
+ from dotenv import load_dotenv
8
+ load_dotenv()
9
+
10
+ client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
11
+
12
+
13
+ def extract_info(image: Image.Image) -> str:
14
+
15
+ # Convert to grayscale (makes it easier for OCR)
16
+ image = image.convert("L")
17
+ # Extract text from the image
18
+ text = pytesseract.image_to_string(image)
19
+ if not text.strip():
20
+ return json.dumps({"error": "No text could be extracted from the image."}, indent=2)
21
+
22
+ response = client.chat.complete(
23
+ model="mistral-small-latest",
24
+ messages=[
25
+ {
26
+ "role": "system",
27
+ "content": (
28
+ "You are a data extraction assistant. "
29
+ "Extract all structured information from the given OCR text "
30
+ "and return it as a clean JSON object. "
31
+ "Do not include any explanation or markdown formatting."
32
+ ),
33
+ },
34
+ {"role": "user", "content": f"Extract structured JSON from this OCR text:\n\n{text}"},
35
+ ],
36
+ )
37
+
38
+ return response.choices[0].message.content
39
+
40
+
41
+ demo = gr.Interface(
42
+ fn=extract_info,
43
+ inputs=gr.Image(type="pil", label="Upload Image"),
44
+ outputs=gr.Textbox(label="Extracted JSON"),
45
+ title="Doc Xtract",
46
+ description="Upload an image to extract structured information as JSON.",
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch(server_name="0.0.0.0")