Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
-
import pytesseract
|
4 |
import openai
|
5 |
import os
|
|
|
|
|
|
|
6 |
|
7 |
# Ensure you have your OpenAI API key set as an environment variable
|
8 |
-
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def extract_and_summarize(image):
|
11 |
-
#
|
12 |
-
|
13 |
|
14 |
# Prepare the prompt for GPT-4
|
15 |
-
prompt =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Call GPT-4 API for summarization
|
18 |
response = openai.ChatCompletion.create(
|
19 |
-
model="gpt-
|
20 |
-
messages=
|
21 |
-
|
22 |
-
{"role": "user", "content": prompt}
|
23 |
-
]
|
24 |
)
|
25 |
|
26 |
# Extract summary from GPT-4 response
|
27 |
-
summary = response
|
28 |
|
29 |
return summary
|
30 |
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import openai
|
3 |
import os
|
4 |
+
from PIL import Image
|
5 |
+
import base64
|
6 |
+
import io
|
7 |
|
8 |
# Ensure you have your OpenAI API key set as an environment variable
|
9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY", "<your OpenAI API key if not set as an env var>")
|
10 |
+
|
11 |
+
def image_to_base64(image):
|
12 |
+
buffered = io.BytesIO()
|
13 |
+
image.save(buffered, format="JPEG")
|
14 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
15 |
+
return img_str
|
16 |
|
17 |
def extract_and_summarize(image):
|
18 |
+
# Convert image to base64
|
19 |
+
image_base64 = image_to_base64(image)
|
20 |
|
21 |
# Prepare the prompt for GPT-4
|
22 |
+
prompt = [
|
23 |
+
{
|
24 |
+
"role": "system",
|
25 |
+
"content": "You are a helpful assistant. Summarize the text content of the document image provided."
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"role": "user",
|
29 |
+
"content": [
|
30 |
+
{"type": "text", "text": "Here is an image of a document. Please summarize its content."},
|
31 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
|
32 |
+
]
|
33 |
+
}
|
34 |
+
]
|
35 |
|
36 |
# Call GPT-4 API for summarization
|
37 |
response = openai.ChatCompletion.create(
|
38 |
+
model="gpt-4o",
|
39 |
+
messages=prompt,
|
40 |
+
max_tokens=300,
|
|
|
|
|
41 |
)
|
42 |
|
43 |
# Extract summary from GPT-4 response
|
44 |
+
summary = response.choices[0].message.content
|
45 |
|
46 |
return summary
|
47 |
|