Spaces:
Sleeping
Sleeping
Add updated code
Browse files- app.py +87 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Standard library imports
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
import io
|
5 |
+
|
6 |
+
# External libraries for image processing and visualization
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Azure AI services
|
10 |
+
from azure.core.credentials import AzureKeyCredential
|
11 |
+
from azure.ai.formrecognizer import DocumentAnalysisClient
|
12 |
+
|
13 |
+
# Gradio for creating interfaces
|
14 |
+
import gradio as gr
|
15 |
+
|
16 |
+
# Other libraries
|
17 |
+
import numpy as np
|
18 |
+
import openai
|
19 |
+
|
20 |
+
# Azure Cognitive Services endpoint and key
|
21 |
+
endpoint = "https://herbariumsamplerecognition.cognitiveservices.azure.com/"
|
22 |
+
key = os.environ['KEY1']
|
23 |
+
|
24 |
+
|
25 |
+
def sanitize_filename(filename):
|
26 |
+
# Remove characters that are not alphanumeric, spaces, dots, or underscores
|
27 |
+
return re.sub(r'[^\w\s\.-]', '', filename)
|
28 |
+
|
29 |
+
def extract_info(text):
|
30 |
+
# Set your OpenAI API key
|
31 |
+
openai.api_key = os.environ['KEY2']
|
32 |
+
|
33 |
+
# Prepare the prompt for the API
|
34 |
+
prompt = f"From the provided text, return only the relevant information in a JSON format according to the Darwin Core standard for biodiversity specimen. Note: make sure that each output has a 'country' field. If you do not find an explicit country, make your best guess at the country using the context of the other text.\n{text}\n{text}"
|
35 |
+
|
36 |
+
try:
|
37 |
+
# Send the request to the API
|
38 |
+
response = openai.ChatCompletion.create(
|
39 |
+
model="gpt-4-1106-preview",
|
40 |
+
messages=[{"role": "system", "content": "You are a helpful assistant."},
|
41 |
+
{"role": "user", "content": prompt}]
|
42 |
+
)
|
43 |
+
|
44 |
+
# Extract the response
|
45 |
+
return response.choices[0].message['content'] if response.choices else "No response from the API."
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
return f"An error occurred: {str(e)}"
|
49 |
+
|
50 |
+
def analyze_read(image_stream):
|
51 |
+
try:
|
52 |
+
document_analysis_client = DocumentAnalysisClient(
|
53 |
+
endpoint=endpoint, credential=AzureKeyCredential(key)
|
54 |
+
)
|
55 |
+
|
56 |
+
poller = document_analysis_client.begin_analyze_document(
|
57 |
+
"prebuilt-read", image_stream)
|
58 |
+
result = poller.result()
|
59 |
+
|
60 |
+
# Collect the content from the document
|
61 |
+
document_content = result.content
|
62 |
+
extracted_info = extract_info(document_content)
|
63 |
+
|
64 |
+
return extracted_info
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
return f"An error occurred: {str(e)}"
|
68 |
+
|
69 |
+
def model_function(image):
|
70 |
+
# Convert the NumPy array to a PIL Image object
|
71 |
+
image = Image.fromarray(np.uint8(image)).convert('RGB')
|
72 |
+
|
73 |
+
# Convert the uploaded image to a byte stream
|
74 |
+
image_bytes = io.BytesIO()
|
75 |
+
image.save(image_bytes, format='JPEG') # Using 'JPEG' as the format
|
76 |
+
image_bytes = image_bytes.getvalue()
|
77 |
+
|
78 |
+
output_text = analyze_read(image_bytes)
|
79 |
+
return output_text
|
80 |
+
|
81 |
+
title = "HerbariaOCR"
|
82 |
+
description = "Upload your Herbaria specimen and let the Azure-GPT pipeline work its magic!\nYou will find all the textual data from the image extracted in the DarwinCore json format standard."
|
83 |
+
article = "Check out [the GitHub repository](https://github.com/BU-Spark/HerbariaOCR) that this demo is based off of."
|
84 |
+
|
85 |
+
iface = gr.Interface(fn=model_function,title=title,
|
86 |
+
description=description,article=article, inputs="image", outputs="text")
|
87 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==3.48.0
|
2 |
+
pydantic==1.10.11
|
3 |
+
typing-extensions
|
4 |
+
pillow
|
5 |
+
azure-ai-formrecognizer
|
6 |
+
openai==0.28
|
7 |
+
ipywidgets
|