Update app.py
Browse files
app.py
CHANGED
@@ -3,24 +3,24 @@ import openai
|
|
3 |
import fitz # PyMuPDF for PDF processing
|
4 |
import base64
|
5 |
|
6 |
-
#
|
7 |
api_key = ""
|
8 |
|
9 |
-
# Function to update API
|
10 |
def set_api_key(key):
|
11 |
global api_key
|
12 |
api_key = key
|
13 |
return "API Key Set Successfully!"
|
14 |
|
15 |
-
# Function to interact with OpenAI API
|
16 |
def query_openai(messages, temperature, top_p, max_output_tokens):
|
17 |
if not api_key:
|
18 |
-
return
|
19 |
|
20 |
try:
|
21 |
-
openai.api_key = api_key # Set API
|
22 |
|
23 |
-
# Ensure
|
24 |
temperature = float(temperature) if temperature else 1.0
|
25 |
top_p = float(top_p) if top_p else 1.0
|
26 |
max_output_tokens = int(max_output_tokens) if max_output_tokens else 2048
|
@@ -32,38 +32,35 @@ def query_openai(messages, temperature, top_p, max_output_tokens):
|
|
32 |
top_p=top_p,
|
33 |
max_tokens=max_output_tokens
|
34 |
)
|
35 |
-
|
36 |
-
bot_response = response["choices"][0]["message"]["content"]
|
37 |
-
messages.append({"role": "assistant", "content": bot_response}) # Store bot response in history
|
38 |
-
|
39 |
-
return messages, messages # Return updated conversation
|
40 |
-
|
41 |
except Exception as e:
|
42 |
-
return
|
43 |
|
44 |
-
#
|
45 |
-
def image_url_chat(image_url, text_query,
|
46 |
if not image_url or not text_query:
|
47 |
-
return
|
48 |
-
|
49 |
-
messages
|
50 |
-
{"
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
54 |
|
55 |
-
#
|
56 |
-
def text_chat(text_query,
|
57 |
if not text_query:
|
58 |
-
return
|
59 |
|
60 |
-
messages
|
61 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
62 |
|
63 |
-
#
|
64 |
-
def image_chat(image_file, text_query,
|
65 |
if image_file is None or not text_query:
|
66 |
-
return
|
67 |
|
68 |
# Encode image as base64
|
69 |
with open(image_file, "rb") as img:
|
@@ -71,34 +68,39 @@ def image_chat(image_file, text_query, messages, temperature, top_p, max_output_
|
|
71 |
|
72 |
image_data = f"data:image/jpeg;base64,{base64_image}"
|
73 |
|
74 |
-
messages
|
75 |
-
{"
|
76 |
-
|
77 |
-
|
|
|
|
|
78 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
79 |
|
80 |
-
# PDF
|
81 |
-
def pdf_chat(pdf_file, text_query,
|
82 |
if pdf_file is None or not text_query:
|
83 |
-
return
|
84 |
|
|
|
85 |
doc = fitz.open(pdf_file)
|
86 |
-
text = "\n".join([page.get_text("text") for page in doc][:5]) #
|
87 |
-
|
88 |
-
messages
|
89 |
-
{"
|
90 |
-
|
91 |
-
|
|
|
|
|
92 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
93 |
|
94 |
-
# Function to clear chat
|
95 |
def clear_chat():
|
96 |
-
return
|
97 |
|
98 |
# Gradio UI Layout
|
99 |
with gr.Blocks() as demo:
|
100 |
-
gr.Markdown("## GPT-4.5 Preview
|
101 |
-
|
102 |
# API Key Input
|
103 |
with gr.Row():
|
104 |
api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
|
@@ -108,56 +110,49 @@ with gr.Blocks() as demo:
|
|
108 |
with gr.Row():
|
109 |
temperature = gr.Slider(0, 2, value=1.0, step=0.1, label="Temperature")
|
110 |
top_p = gr.Slider(0, 1, value=1.0, step=0.1, label="Top-P")
|
111 |
-
max_output_tokens = gr.Slider(0, 16384, value=2048, step=512, label="Max Output Tokens")
|
112 |
-
|
113 |
with gr.Tabs():
|
114 |
with gr.Tab("Image URL Chat"):
|
115 |
image_url = gr.Textbox(label="Enter Image URL")
|
116 |
image_query = gr.Textbox(label="Ask about the Image")
|
117 |
-
image_url_output = gr.
|
118 |
image_url_button = gr.Button("Ask")
|
119 |
-
|
120 |
with gr.Tab("Text Chat"):
|
121 |
text_query = gr.Textbox(label="Enter your query")
|
122 |
-
text_output = gr.
|
123 |
text_button = gr.Button("Ask")
|
124 |
-
|
125 |
with gr.Tab("Image Chat"):
|
126 |
image_upload = gr.File(label="Upload an Image", type="filepath")
|
127 |
image_text_query = gr.Textbox(label="Ask about the uploaded image")
|
128 |
-
image_output = gr.
|
129 |
image_button = gr.Button("Ask")
|
130 |
-
|
131 |
with gr.Tab("PDF Chat"):
|
132 |
pdf_upload = gr.File(label="Upload a PDF", type="filepath")
|
133 |
pdf_text_query = gr.Textbox(label="Ask about the uploaded PDF")
|
134 |
-
pdf_output = gr.
|
135 |
pdf_button = gr.Button("Ask")
|
136 |
|
137 |
# Clear chat button
|
138 |
clear_button = gr.Button("Clear Chat")
|
139 |
|
140 |
-
# Chat Histories
|
141 |
-
image_url_chat_history = gr.State([])
|
142 |
-
text_chat_history = gr.State([])
|
143 |
-
image_chat_history = gr.State([])
|
144 |
-
pdf_chat_history = gr.State([])
|
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,
|
149 |
-
text_button.click(text_chat, [text_query,
|
150 |
-
image_button.click(image_chat, [image_upload, image_text_query,
|
151 |
-
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query,
|
152 |
|
153 |
# Fix: Clear button resets all necessary fields correctly
|
154 |
clear_button.click(
|
155 |
clear_chat,
|
156 |
outputs=[
|
157 |
-
image_url_chat_history, text_chat_history, image_chat_history, pdf_chat_history,
|
158 |
image_url, image_query, image_url_output,
|
159 |
text_query, text_output,
|
160 |
-
|
161 |
pdf_upload, pdf_text_query, pdf_output,
|
162 |
temperature, top_p, max_output_tokens
|
163 |
]
|
|
|
3 |
import fitz # PyMuPDF for PDF processing
|
4 |
import base64
|
5 |
|
6 |
+
# Variable to store API key
|
7 |
api_key = ""
|
8 |
|
9 |
+
# Function to update API key
|
10 |
def set_api_key(key):
|
11 |
global api_key
|
12 |
api_key = key
|
13 |
return "API Key Set Successfully!"
|
14 |
|
15 |
+
# Function to interact with OpenAI API
|
16 |
def query_openai(messages, temperature, top_p, max_output_tokens):
|
17 |
if not api_key:
|
18 |
+
return "Please enter your OpenAI API key first."
|
19 |
|
20 |
try:
|
21 |
+
openai.api_key = api_key # Set API key dynamically
|
22 |
|
23 |
+
# Ensure numeric values for OpenAI parameters
|
24 |
temperature = float(temperature) if temperature else 1.0
|
25 |
top_p = float(top_p) if top_p else 1.0
|
26 |
max_output_tokens = int(max_output_tokens) if max_output_tokens else 2048
|
|
|
32 |
top_p=top_p,
|
33 |
max_tokens=max_output_tokens
|
34 |
)
|
35 |
+
return response["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
+
return f"Error: {str(e)}"
|
38 |
|
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": [
|
46 |
+
{"type": "image_url", "image_url": {"url": image_url}}, # Corrected format
|
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):
|
54 |
if not text_query:
|
55 |
+
return "Please enter a query."
|
56 |
|
57 |
+
messages = [{"role": "user", "content": [{"type": "text", "text": text_query}]}]
|
58 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
59 |
|
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:
|
|
|
68 |
|
69 |
image_data = f"data:image/jpeg;base64,{base64_image}"
|
70 |
|
71 |
+
messages = [
|
72 |
+
{"role": "user", "content": [
|
73 |
+
{"type": "image_url", "image_url": {"url": image_data}}, # Fixed format
|
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):
|
81 |
if pdf_file is None or not text_query:
|
82 |
+
return "Please upload a PDF and provide a query."
|
83 |
|
84 |
+
# Extract text from the first few pages
|
85 |
doc = fitz.open(pdf_file)
|
86 |
+
text = "\n".join([page.get_text("text") for page in doc][:5]) # Limit extraction for performance
|
87 |
+
|
88 |
+
messages = [
|
89 |
+
{"role": "user", "content": [
|
90 |
+
{"type": "text", "text": text}, # Fixed format
|
91 |
+
{"type": "text", "text": text_query}
|
92 |
+
]}
|
93 |
+
]
|
94 |
return query_openai(messages, temperature, top_p, max_output_tokens)
|
95 |
|
96 |
+
# Function to clear the chat (Fix: Returns the correct number of outputs)
|
97 |
def clear_chat():
|
98 |
+
return "", "", "", "", "", "", "", None, "", None, "", 1.0, 1.0, 2048
|
99 |
|
100 |
# Gradio UI Layout
|
101 |
with gr.Blocks() as demo:
|
102 |
+
gr.Markdown("## GPT-4.5 Preview Chatbot")
|
103 |
+
|
104 |
# API Key Input
|
105 |
with gr.Row():
|
106 |
api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
|
|
|
110 |
with gr.Row():
|
111 |
temperature = gr.Slider(0, 2, value=1.0, step=0.1, label="Temperature")
|
112 |
top_p = gr.Slider(0, 1, value=1.0, step=0.1, label="Top-P")
|
113 |
+
max_output_tokens = gr.Slider(0, 16384, value=2048, step=512, label="Max Output Tokens") # Changed default to 2048
|
114 |
+
|
115 |
with gr.Tabs():
|
116 |
with gr.Tab("Image URL Chat"):
|
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"):
|
123 |
text_query = gr.Textbox(label="Enter your query")
|
124 |
+
text_output = gr.Textbox(label="Response", interactive=False)
|
125 |
text_button = gr.Button("Ask")
|
126 |
+
|
127 |
with gr.Tab("Image 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"):
|
134 |
pdf_upload = gr.File(label="Upload a PDF", type="filepath")
|
135 |
pdf_text_query = gr.Textbox(label="Ask about the uploaded PDF")
|
136 |
+
pdf_output = gr.Textbox(label="Response", interactive=False)
|
137 |
pdf_button = gr.Button("Ask")
|
138 |
|
139 |
# Clear chat button
|
140 |
clear_button = gr.Button("Clear 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 |
]
|