Update app.py
Browse files
app.py
CHANGED
|
@@ -37,47 +37,53 @@ def encode_image_to_base64(image):
|
|
| 37 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 38 |
|
| 39 |
|
| 40 |
-
def query_gpt4o_mini(query, images):
|
| 41 |
"""Calls OpenAI's GPT-4o-mini with the query and image data."""
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
response = client.chat.completions.create(
|
| 58 |
-
model="gpt-4o-mini",
|
| 59 |
-
messages=[
|
| 60 |
-
{
|
| 61 |
-
"role": "user",
|
| 62 |
-
"content": [
|
| 63 |
{
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
@spaces.GPU
|
| 80 |
-
def search(query: str, ds, images, k):
|
| 81 |
k = min(k, len(ds))
|
| 82 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 83 |
if device != model.device:
|
|
@@ -98,7 +104,7 @@ def search(query: str, ds, images, k):
|
|
| 98 |
results.append((images[idx], f"Page {idx}"))
|
| 99 |
|
| 100 |
# Generate response from GPT-4o-mini
|
| 101 |
-
ai_response = query_gpt4o_mini(query, results)
|
| 102 |
|
| 103 |
return results, ai_response
|
| 104 |
|
|
@@ -164,6 +170,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 164 |
|
| 165 |
convert_button = gr.Button("🔄 Index documents")
|
| 166 |
message = gr.Textbox("Files not yet uploaded", label="Status")
|
|
|
|
| 167 |
embeds = gr.State(value=[])
|
| 168 |
imgs = gr.State(value=[])
|
| 169 |
|
|
@@ -179,7 +186,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 179 |
output_text = gr.Textbox(label="AI Response", placeholder="Generated response based on retrieved documents")
|
| 180 |
|
| 181 |
convert_button.click(index, inputs=[file, embeds], outputs=[message, embeds, imgs])
|
| 182 |
-
search_button.click(search, inputs=[query, embeds, imgs, k], outputs=[output_gallery, output_text])
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
demo.queue(max_size=10).launch(debug=True)
|
|
|
|
| 37 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 38 |
|
| 39 |
|
| 40 |
+
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 |
+
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)
|
| 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 |
+
|
| 82 |
+
return "Enter your OpenAI API key to get a custom response"
|
| 83 |
|
| 84 |
|
| 85 |
@spaces.GPU
|
| 86 |
+
def search(query: str, ds, images, k, api_key):
|
| 87 |
k = min(k, len(ds))
|
| 88 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 89 |
if device != model.device:
|
|
|
|
| 104 |
results.append((images[idx], f"Page {idx}"))
|
| 105 |
|
| 106 |
# Generate response from GPT-4o-mini
|
| 107 |
+
ai_response = query_gpt4o_mini(query, results, api_key)
|
| 108 |
|
| 109 |
return results, ai_response
|
| 110 |
|
|
|
|
| 170 |
|
| 171 |
convert_button = gr.Button("🔄 Index documents")
|
| 172 |
message = gr.Textbox("Files not yet uploaded", label="Status")
|
| 173 |
+
api_key = gr.Textbox(placeholder="Enter your OpenAI KEY here (optional)", label="API key")
|
| 174 |
embeds = gr.State(value=[])
|
| 175 |
imgs = gr.State(value=[])
|
| 176 |
|
|
|
|
| 186 |
output_text = gr.Textbox(label="AI Response", placeholder="Generated response based on retrieved documents")
|
| 187 |
|
| 188 |
convert_button.click(index, inputs=[file, embeds], outputs=[message, embeds, imgs])
|
| 189 |
+
search_button.click(search, inputs=[query, embeds, imgs, k, api_key], outputs=[output_gallery, output_text])
|
| 190 |
|
| 191 |
if __name__ == "__main__":
|
| 192 |
demo.queue(max_size=10).launch(debug=True)
|