Update app.py
Browse files
app.py
CHANGED
@@ -32,6 +32,9 @@ def query_openai(messages, temperature, top_p, max_output_tokens):
|
|
32 |
|
33 |
# Function to process image URL input
|
34 |
def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens):
|
|
|
|
|
|
|
35 |
messages = [
|
36 |
{"role": "user", "content": [
|
37 |
{"type": "image_url", "image_url": {"url": image_url}}, # Corrected format
|
@@ -42,13 +45,16 @@ def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens)
|
|
42 |
|
43 |
# Function to process text input
|
44 |
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
|
|
|
|
|
|
45 |
messages = [{"role": "user", "content": [{"type": "text", "text": text_query}]}]
|
46 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
47 |
|
48 |
# Function to process uploaded image input
|
49 |
def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
50 |
-
if image_file is None:
|
51 |
-
return "Please upload an image."
|
52 |
|
53 |
# Encode image as base64
|
54 |
with open(image_file, "rb") as img:
|
@@ -66,8 +72,8 @@ def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
|
66 |
|
67 |
# Function to process uploaded PDF input
|
68 |
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
69 |
-
if pdf_file is None:
|
70 |
-
return "Please upload a PDF."
|
71 |
|
72 |
# Extract text from the first few pages
|
73 |
doc = fitz.open(pdf_file)
|
@@ -81,9 +87,9 @@ def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
|
81 |
]
|
82 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
83 |
|
84 |
-
# Function to clear the chat
|
85 |
def clear_chat():
|
86 |
-
return "", "",
|
87 |
|
88 |
# Gradio UI Layout
|
89 |
with gr.Blocks() as demo:
|
@@ -98,7 +104,7 @@ with gr.Blocks() as demo:
|
|
98 |
with gr.Row():
|
99 |
temperature = gr.Slider(0, 2, value=1.0, step=0.1, label="Temperature")
|
100 |
top_p = gr.Slider(0, 1, value=1.0, step=0.1, label="Top-P")
|
101 |
-
max_output_tokens = gr.Slider(0, 16384, value=
|
102 |
|
103 |
with gr.Tabs():
|
104 |
with gr.Tab("Image URL Chat"):
|
@@ -133,7 +139,18 @@ with gr.Blocks() as demo:
|
|
133 |
text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
|
134 |
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
|
135 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
# Launch Gradio App
|
139 |
if __name__ == "__main__":
|
|
|
32 |
|
33 |
# Function to process image URL input
|
34 |
def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens):
|
35 |
+
if not image_url or not text_query:
|
36 |
+
return "Please provide an image URL and a query."
|
37 |
+
|
38 |
messages = [
|
39 |
{"role": "user", "content": [
|
40 |
{"type": "image_url", "image_url": {"url": image_url}}, # Corrected format
|
|
|
45 |
|
46 |
# Function to process text input
|
47 |
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
48 |
+
if not text_query:
|
49 |
+
return "Please enter a query."
|
50 |
+
|
51 |
messages = [{"role": "user", "content": [{"type": "text", "text": text_query}]}]
|
52 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
53 |
|
54 |
# Function to process uploaded image input
|
55 |
def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
56 |
+
if image_file is None or not text_query:
|
57 |
+
return "Please upload an image and provide a query."
|
58 |
|
59 |
# Encode image as base64
|
60 |
with open(image_file, "rb") as img:
|
|
|
72 |
|
73 |
# Function to process uploaded PDF input
|
74 |
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
75 |
+
if pdf_file is None or not text_query:
|
76 |
+
return "Please upload a PDF and provide a query."
|
77 |
|
78 |
# Extract text from the first few pages
|
79 |
doc = fitz.open(pdf_file)
|
|
|
87 |
]
|
88 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
89 |
|
90 |
+
# Function to clear the chat (Resets input and output)
|
91 |
def clear_chat():
|
92 |
+
return "", "", "", "", "", "", "", 1.0, 1.0, 1024
|
93 |
|
94 |
# Gradio UI Layout
|
95 |
with gr.Blocks() as demo:
|
|
|
104 |
with gr.Row():
|
105 |
temperature = gr.Slider(0, 2, value=1.0, step=0.1, label="Temperature")
|
106 |
top_p = gr.Slider(0, 1, value=1.0, step=0.1, label="Top-P")
|
107 |
+
max_output_tokens = gr.Slider(0, 16384, value=1024, step=512, label="Max Output Tokens")
|
108 |
|
109 |
with gr.Tabs():
|
110 |
with gr.Tab("Image URL Chat"):
|
|
|
139 |
text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
|
140 |
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
|
141 |
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
142 |
+
|
143 |
+
# Fix: Clear button resets only necessary fields
|
144 |
+
clear_button.click(
|
145 |
+
clear_chat,
|
146 |
+
outputs=[
|
147 |
+
image_url, image_query, image_url_output,
|
148 |
+
text_query, text_output,
|
149 |
+
image_text_query, image_output,
|
150 |
+
pdf_text_query, pdf_output,
|
151 |
+
temperature, top_p, max_output_tokens
|
152 |
+
]
|
153 |
+
)
|
154 |
|
155 |
# Launch Gradio App
|
156 |
if __name__ == "__main__":
|