Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import fitz # PyMuPDF for PDF processing
|
4 |
+
import base64
|
5 |
+
|
6 |
+
# Variable to store the API key
|
7 |
+
api_key = ""
|
8 |
+
|
9 |
+
# Function to update the 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 |
+
response = openai.ChatCompletion.create(
|
23 |
+
model="gpt-4.5-preview",
|
24 |
+
messages=messages,
|
25 |
+
temperature=temperature,
|
26 |
+
top_p=top_p,
|
27 |
+
max_tokens=max_output_tokens
|
28 |
+
)
|
29 |
+
return response["choices"][0]["message"]["content"]
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error: {str(e)}"
|
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": [{"type": "input_image", "image_url": image_url}, {"type": "input_text", "text": text_query}]}
|
37 |
+
]
|
38 |
+
return query_openai(messages, temperature, top_p, max_output_tokens)
|
39 |
+
|
40 |
+
# Function to process text input
|
41 |
+
def text_chat(text_query, temperature, top_p, max_output_tokens):
|
42 |
+
messages = [{"role": "user", "content": text_query}]
|
43 |
+
return query_openai(messages, temperature, top_p, max_output_tokens)
|
44 |
+
|
45 |
+
# Function to process uploaded image input
|
46 |
+
def image_chat(image_file, text_query, temperature, top_p, max_output_tokens):
|
47 |
+
if image_file is None:
|
48 |
+
return "Please upload an image."
|
49 |
+
|
50 |
+
# Encode image as base64
|
51 |
+
with open(image_file, "rb") as img:
|
52 |
+
base64_image = base64.b64encode(img.read()).decode("utf-8")
|
53 |
+
|
54 |
+
image_data = f"data:image/jpeg;base64,{base64_image}"
|
55 |
+
|
56 |
+
messages = [
|
57 |
+
{"role": "user", "content": [{"type": "input_image", "image_url": image_data}, {"type": "input_text", "text": text_query}]}
|
58 |
+
]
|
59 |
+
return query_openai(messages, temperature, top_p, max_output_tokens)
|
60 |
+
|
61 |
+
# Function to process uploaded PDF input
|
62 |
+
def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
|
63 |
+
if pdf_file is None:
|
64 |
+
return "Please upload a PDF."
|
65 |
+
|
66 |
+
# Extract text from the first few pages
|
67 |
+
doc = fitz.open(pdf_file)
|
68 |
+
text = "\n".join([page.get_text("text") for page in doc][:5]) # Limit extraction for performance
|
69 |
+
|
70 |
+
messages = [
|
71 |
+
{"role": "user", "content": [{"type": "input_text", "text": text}, {"type": "input_text", "text": text_query}]}
|
72 |
+
]
|
73 |
+
return query_openai(messages, temperature, top_p, max_output_tokens)
|
74 |
+
|
75 |
+
# Function to clear the chat
|
76 |
+
def clear_chat():
|
77 |
+
return "", "", None, "", None, "", 1.0, 1.0, 1024
|
78 |
+
|
79 |
+
# Gradio UI Layout
|
80 |
+
with gr.Blocks() as demo:
|
81 |
+
gr.Markdown("## GPT-4.5 Preview Chatbot")
|
82 |
+
|
83 |
+
# API Key Input
|
84 |
+
with gr.Row():
|
85 |
+
api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
|
86 |
+
api_key_button = gr.Button("Set API Key")
|
87 |
+
api_key_output = gr.Textbox(label="API Key Status", interactive=False)
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
temperature = gr.Slider(0, 2, value=1.0, step=0.1, label="Temperature")
|
91 |
+
top_p = gr.Slider(0, 1, value=1.0, step=0.1, label="Top-P")
|
92 |
+
max_output_tokens = gr.Slider(0, 16384, value=1024, step=512, label="Max Output Tokens")
|
93 |
+
|
94 |
+
with gr.Tabs():
|
95 |
+
with gr.Tab("Image URL Chat"):
|
96 |
+
image_url = gr.Textbox(label="Enter Image URL")
|
97 |
+
image_query = gr.Textbox(label="Ask about the Image")
|
98 |
+
image_url_output = gr.Textbox(label="Response", interactive=False)
|
99 |
+
image_url_button = gr.Button("Ask")
|
100 |
+
|
101 |
+
with gr.Tab("Text Chat"):
|
102 |
+
text_query = gr.Textbox(label="Enter your query")
|
103 |
+
text_output = gr.Textbox(label="Response", interactive=False)
|
104 |
+
text_button = gr.Button("Ask")
|
105 |
+
|
106 |
+
with gr.Tab("Image Chat"):
|
107 |
+
image_upload = gr.File(label="Upload an Image", type="filepath")
|
108 |
+
image_text_query = gr.Textbox(label="Ask about the uploaded image")
|
109 |
+
image_output = gr.Textbox(label="Response", interactive=False)
|
110 |
+
image_button = gr.Button("Ask")
|
111 |
+
|
112 |
+
with gr.Tab("PDF Chat"):
|
113 |
+
pdf_upload = gr.File(label="Upload a PDF", type="filepath")
|
114 |
+
pdf_text_query = gr.Textbox(label="Ask about the uploaded PDF")
|
115 |
+
pdf_output = gr.Textbox(label="Response", interactive=False)
|
116 |
+
pdf_button = gr.Button("Ask")
|
117 |
+
|
118 |
+
# Clear chat button
|
119 |
+
clear_button = gr.Button("Clear Chat")
|
120 |
+
|
121 |
+
# Button Click Actions
|
122 |
+
api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
|
123 |
+
image_url_button.click(image_url_chat, [image_url, image_query, temperature, top_p, max_output_tokens], image_url_output)
|
124 |
+
text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
|
125 |
+
image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
|
126 |
+
pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
|
127 |
+
clear_button.click(clear_chat, outputs=[image_url, image_query, image_url_output, text_query, text_output, image_upload, image_text_query, image_output, pdf_upload, pdf_text_query, pdf_output, temperature, top_p, max_output_tokens])
|
128 |
+
|
129 |
+
# Launch Gradio App
|
130 |
+
if __name__ == "__main__":
|
131 |
+
demo.launch()
|