Update app.py
Browse files
app.py
CHANGED
@@ -39,7 +39,7 @@ def query_openai(messages, temperature, top_p, max_output_tokens):
|
|
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,7 +47,8 @@ def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens)
|
|
47 |
{"type": "text", "text": text_query}
|
48 |
]}
|
49 |
]
|
50 |
-
|
|
|
51 |
|
52 |
# Function to process text input
|
53 |
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
@@ -60,7 +61,7 @@ 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,7 +75,8 @@ def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
|
74 |
{"type": "text", "text": text_query}
|
75 |
]}
|
76 |
]
|
77 |
-
|
|
|
78 |
|
79 |
# Function to process uploaded PDF input
|
80 |
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
@@ -117,6 +119,7 @@ with gr.Blocks() as demo:
|
|
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,6 +131,7 @@ with gr.Blocks() as demo:
|
|
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,18 +145,18 @@ with gr.Blocks() as demo:
|
|
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 |
]
|
|
|
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.", None
|
43 |
|
44 |
messages = [
|
45 |
{"role": "user", "content": [
|
|
|
47 |
{"type": "text", "text": text_query}
|
48 |
]}
|
49 |
]
|
50 |
+
response = query_openai(messages, temperature, top_p, max_output_tokens)
|
51 |
+
return response, image_url # Return image URL to display it
|
52 |
|
53 |
# Function to process text input
|
54 |
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
|
|
61 |
# Function to process uploaded image input
|
62 |
def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
63 |
if image_file is None or not text_query:
|
64 |
+
return "Please upload an image and provide a query.", None
|
65 |
|
66 |
# Encode image as base64
|
67 |
with open(image_file, "rb") as img:
|
|
|
75 |
{"type": "text", "text": text_query}
|
76 |
]}
|
77 |
]
|
78 |
+
response = query_openai(messages, temperature, top_p, max_output_tokens)
|
79 |
+
return response, image_data # Return image data for display
|
80 |
|
81 |
# Function to process uploaded PDF input
|
82 |
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
|
|
119 |
image_url = gr.Textbox(label="Enter Image URL")
|
120 |
image_query = gr.Textbox(label="Ask about the Image")
|
121 |
image_url_output = gr.Textbox(label="Response", interactive=False)
|
122 |
+
image_url_display = gr.Image(label="Image", interactive=False)
|
123 |
image_url_button = gr.Button("Ask")
|
124 |
|
125 |
with gr.Tab("Text Chat"):
|
|
|
131 |
image_upload = gr.File(label="Upload an Image", type="filepath")
|
132 |
image_text_query = gr.Textbox(label="Ask about the uploaded image")
|
133 |
image_output = gr.Textbox(label="Response", interactive=False)
|
134 |
+
image_display = gr.Image(label="Uploaded Image", interactive=False)
|
135 |
image_button = gr.Button("Ask")
|
136 |
|
137 |
with gr.Tab("PDF Chat"):
|
|
|
145 |
|
146 |
# Button Click Actions
|
147 |
api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
|
148 |
+
image_url_button.click(image_url_chat, [image_url, image_query, temperature, top_p, max_output_tokens], [image_url_output, image_url_display])
|
149 |
text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
|
150 |
+
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], [image_output, image_display])
|
151 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
152 |
|
153 |
# Fix: Clear button resets all necessary fields correctly
|
154 |
clear_button.click(
|
155 |
clear_chat,
|
156 |
outputs=[
|
157 |
+
image_url, image_query, image_url_output, image_url_display,
|
158 |
text_query, text_output,
|
159 |
+
image_text_query, image_output, image_display,
|
160 |
pdf_upload, pdf_text_query, pdf_output,
|
161 |
temperature, top_p, max_output_tokens
|
162 |
]
|