Update app.py
Browse files
app.py
CHANGED
@@ -2,8 +2,6 @@ import gradio as gr
|
|
2 |
import openai
|
3 |
import fitz # PyMuPDF for PDF processing
|
4 |
import base64
|
5 |
-
from io import BytesIO
|
6 |
-
from PIL import Image
|
7 |
|
8 |
# Variable to store API key
|
9 |
api_key = ""
|
@@ -41,7 +39,7 @@ def query_openai(messages, temperature, top_p, max_output_tokens):
|
|
41 |
# Function to process image URL input
|
42 |
def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens):
|
43 |
if not image_url or not text_query:
|
44 |
-
return "Please provide an image URL and a query."
|
45 |
|
46 |
messages = [
|
47 |
{"role": "user", "content": [
|
@@ -49,8 +47,7 @@ def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens)
|
|
49 |
{"type": "text", "text": text_query}
|
50 |
]}
|
51 |
]
|
52 |
-
|
53 |
-
return response, image_url # Return image URL to display it
|
54 |
|
55 |
# Function to process text input
|
56 |
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
@@ -63,7 +60,7 @@ def text_chat(text_query, temperature, top_p, max_output_tokens):
|
|
63 |
# Function to process uploaded image input
|
64 |
def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
65 |
if image_file is None or not text_query:
|
66 |
-
return "Please upload an image and provide a query."
|
67 |
|
68 |
# Encode image as base64
|
69 |
with open(image_file, "rb") as img:
|
@@ -77,11 +74,7 @@ def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
|
77 |
{"type": "text", "text": text_query}
|
78 |
]}
|
79 |
]
|
80 |
-
|
81 |
-
|
82 |
-
# Convert base64 image to PIL Image for display in Gradio
|
83 |
-
img = Image.open(BytesIO(base64.b64decode(base64_image)))
|
84 |
-
return response, img # Return image for Gradio to display
|
85 |
|
86 |
# Function to process uploaded PDF input
|
87 |
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
@@ -124,7 +117,6 @@ with gr.Blocks() as demo:
|
|
124 |
image_url = gr.Textbox(label="Enter Image URL")
|
125 |
image_query = gr.Textbox(label="Ask about the Image")
|
126 |
image_url_output = gr.Textbox(label="Response", interactive=False)
|
127 |
-
image_url_display = gr.Image(label="Image", interactive=False)
|
128 |
image_url_button = gr.Button("Ask")
|
129 |
|
130 |
with gr.Tab("Text Chat"):
|
@@ -136,7 +128,6 @@ with gr.Blocks() as demo:
|
|
136 |
image_upload = gr.File(label="Upload an Image", type="filepath")
|
137 |
image_text_query = gr.Textbox(label="Ask about the uploaded image")
|
138 |
image_output = gr.Textbox(label="Response", interactive=False)
|
139 |
-
image_display = gr.Image(label="Uploaded Image", interactive=False)
|
140 |
image_button = gr.Button("Ask")
|
141 |
|
142 |
with gr.Tab("PDF Chat"):
|
@@ -150,18 +141,18 @@ with gr.Blocks() as demo:
|
|
150 |
|
151 |
# Button Click Actions
|
152 |
api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
|
153 |
-
image_url_button.click(image_url_chat, [image_url, image_query, temperature, top_p, max_output_tokens],
|
154 |
text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
|
155 |
-
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens],
|
156 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
157 |
|
158 |
# Fix: Clear button resets all necessary fields correctly
|
159 |
clear_button.click(
|
160 |
clear_chat,
|
161 |
outputs=[
|
162 |
-
image_url, image_query, image_url_output,
|
163 |
text_query, text_output,
|
164 |
-
image_text_query, image_output,
|
165 |
pdf_upload, pdf_text_query, pdf_output,
|
166 |
temperature, top_p, max_output_tokens
|
167 |
]
|
|
|
2 |
import openai
|
3 |
import fitz # PyMuPDF for PDF processing
|
4 |
import base64
|
|
|
|
|
5 |
|
6 |
# Variable to store API key
|
7 |
api_key = ""
|
|
|
39 |
# Function to process image URL input
|
40 |
def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens):
|
41 |
if not image_url or not text_query:
|
42 |
+
return "Please provide an image URL and a query."
|
43 |
|
44 |
messages = [
|
45 |
{"role": "user", "content": [
|
|
|
47 |
{"type": "text", "text": text_query}
|
48 |
]}
|
49 |
]
|
50 |
+
return query_openai(messages, temperature, top_p, max_output_tokens)
|
|
|
51 |
|
52 |
# Function to process text input
|
53 |
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
|
|
60 |
# Function to process uploaded image input
|
61 |
def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
62 |
if image_file is None or not text_query:
|
63 |
+
return "Please upload an image and provide a query."
|
64 |
|
65 |
# Encode image as base64
|
66 |
with open(image_file, "rb") as img:
|
|
|
74 |
{"type": "text", "text": text_query}
|
75 |
]}
|
76 |
]
|
77 |
+
return query_openai(messages, temperature, top_p, max_output_tokens)
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Function to process uploaded PDF input
|
80 |
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
|
|
117 |
image_url = gr.Textbox(label="Enter Image URL")
|
118 |
image_query = gr.Textbox(label="Ask about the Image")
|
119 |
image_url_output = gr.Textbox(label="Response", interactive=False)
|
|
|
120 |
image_url_button = gr.Button("Ask")
|
121 |
|
122 |
with gr.Tab("Text Chat"):
|
|
|
128 |
image_upload = gr.File(label="Upload an Image", type="filepath")
|
129 |
image_text_query = gr.Textbox(label="Ask about the uploaded image")
|
130 |
image_output = gr.Textbox(label="Response", interactive=False)
|
|
|
131 |
image_button = gr.Button("Ask")
|
132 |
|
133 |
with gr.Tab("PDF Chat"):
|
|
|
141 |
|
142 |
# Button Click Actions
|
143 |
api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
|
144 |
+
image_url_button.click(image_url_chat, [image_url, image_query, temperature, top_p, max_output_tokens], image_url_output)
|
145 |
text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
|
146 |
+
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
|
147 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
148 |
|
149 |
# Fix: Clear button resets all necessary fields correctly
|
150 |
clear_button.click(
|
151 |
clear_chat,
|
152 |
outputs=[
|
153 |
+
image_url, image_query, image_url_output,
|
154 |
text_query, text_output,
|
155 |
+
image_text_query, image_output,
|
156 |
pdf_upload, pdf_text_query, pdf_output,
|
157 |
temperature, top_p, max_output_tokens
|
158 |
]
|