Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -41,43 +41,45 @@ def query_gpt4o_mini(query, images, api_key):
|
|
41 |
"""Calls OpenAI's GPT-4o-mini with the query and image data."""
|
42 |
|
43 |
if api_key and api_key.startswith("sk"):
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
response = client.chat.completions.create(
|
62 |
-
model="gpt-4o-mini",
|
63 |
-
messages=[
|
64 |
-
{
|
65 |
-
"role": "user",
|
66 |
-
"content": [
|
67 |
{
|
68 |
-
"
|
69 |
-
"
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
return "Enter your OpenAI API key to get a custom response"
|
83 |
|
|
|
41 |
"""Calls OpenAI's GPT-4o-mini with the query and image data."""
|
42 |
|
43 |
if api_key and api_key.startswith("sk"):
|
44 |
+
try:
|
45 |
+
from openai import OpenAI
|
46 |
|
47 |
+
base64_images = [encode_image_to_base64(image[0]) for image in images]
|
48 |
+
client = OpenAI(api_key=api_key.strip())
|
49 |
+
PROMPT = """
|
50 |
+
You are a smart assistant designed to answer questions about a PDF document.
|
51 |
+
You are given relevant information in the form of PDF pages. Use them to construct a short response to the question, and cite your sources (page numbers, etc).
|
52 |
+
If it is not possible to answer using the provided pages, do not attempt to provide an answer and simply say the answer is not present within the documents.
|
53 |
+
Give detailed and extensive answers, only containing info in the pages you are given.
|
54 |
+
You can answer using information contained in plots and figures if necessary.
|
55 |
+
Answer in the same language as the query.
|
56 |
+
|
57 |
+
Query: {query}
|
58 |
+
PDF pages:
|
59 |
+
"""
|
60 |
|
61 |
+
response = client.chat.completions.create(
|
62 |
+
model="gpt-4o-mini",
|
63 |
+
messages=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
{
|
65 |
+
"role": "user",
|
66 |
+
"content": [
|
67 |
+
{
|
68 |
+
"type": "text",
|
69 |
+
"text": PROMPT.format(query=query)
|
70 |
+
}] + [{
|
71 |
+
"type": "image_url",
|
72 |
+
"image_url": {
|
73 |
+
"url": f"data:image/jpeg;base64,{im}"
|
74 |
+
},
|
75 |
+
} for im in base64_images]
|
76 |
+
}
|
77 |
+
],
|
78 |
+
max_tokens=500,
|
79 |
+
)
|
80 |
+
return response.choices[0].message.content
|
81 |
+
except Exception as e:
|
82 |
+
return "OpenAI API connection failure. Verify the provided key is correct (sk-***)."
|
83 |
|
84 |
return "Enter your OpenAI API key to get a custom response"
|
85 |
|