shukdevdatta123 commited on
Commit
ebe6f08
·
verified ·
1 Parent(s): 6f32a8f

Delete version1.txt

Browse files
Files changed (1) hide show
  1. version1.txt +0 -240
version1.txt DELETED
@@ -1,240 +0,0 @@
1
- import gradio as gr
2
- import openai
3
- import fitz # PyMuPDF for PDF processing
4
- import base64
5
- import io
6
-
7
- # Variable to store API key
8
- api_key = ""
9
-
10
- # Function to update API key
11
- def set_api_key(key):
12
- global api_key
13
- api_key = key
14
- return "API Key Set Successfully!"
15
-
16
- # Function to interact with OpenAI API
17
- def query_openai(messages, temperature, top_p, max_output_tokens):
18
- if not api_key:
19
- return "Please enter your OpenAI API key first."
20
-
21
- try:
22
- openai.api_key = api_key # Set API key dynamically
23
-
24
- # Ensure numeric values for OpenAI parameters
25
- temperature = float(temperature) if temperature else 1.0
26
- top_p = float(top_p) if top_p else 1.0
27
- max_output_tokens = int(max_output_tokens) if max_output_tokens else 2048
28
-
29
- response = openai.ChatCompletion.create(
30
- model="gpt-4.5-preview",
31
- messages=messages,
32
- temperature=temperature,
33
- top_p=top_p,
34
- max_tokens=max_output_tokens
35
- )
36
- return response["choices"][0]["message"]["content"]
37
- except Exception as e:
38
- return f"Error: {str(e)}"
39
-
40
- # Function to process image URL input
41
- def image_url_chat(image_url, text_query, temperature, top_p, max_output_tokens):
42
- if not image_url or not text_query:
43
- return "Please provide an image URL and a query."
44
-
45
- messages = [
46
- {"role": "user", "content": [
47
- {"type": "image_url", "image_url": {"url": image_url}}, # Corrected format
48
- {"type": "text", "text": text_query}
49
- ]},
50
- ]
51
- return query_openai(messages, temperature, top_p, max_output_tokens)
52
-
53
- # Function to process text input
54
- def text_chat(text_query, temperature, top_p, max_output_tokens):
55
- if not text_query:
56
- return "Please enter a query."
57
-
58
- messages = [{"role": "user", "content": [{"type": "text", "text": text_query}]}]
59
- return query_openai(messages, temperature, top_p, max_output_tokens)
60
-
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."
65
-
66
- # Encode image as base64
67
- with open(image_file, "rb") as img:
68
- base64_image = base64.b64encode(img.read()).decode("utf-8")
69
-
70
- image_data = f"data:image/jpeg;base64,{base64_image}"
71
-
72
- messages = [
73
- {"role": "user", "content": [
74
- {"type": "image_url", "image_url": {"url": image_data}}, # Fixed format
75
- {"type": "text", "text": text_query}
76
- ]},
77
- ]
78
- return query_openai(messages, temperature, top_p, max_output_tokens)
79
-
80
- # Function to process uploaded PDF input
81
- def pdf_chat(pdf_file, text_query, temperature, top_p, max_output_tokens):
82
- if pdf_file is None or not text_query:
83
- return "Please upload a PDF and provide a query."
84
-
85
- try:
86
- # Extract text from all pages of the PDF
87
- doc = fitz.open(pdf_file.name)
88
- text = "\n".join([page.get_text("text") for page in doc]) # Extract text from all pages
89
-
90
- # If no text found, return an error
91
- if not text.strip():
92
- return "No text found in the PDF."
93
-
94
- # Create the query message with the extracted text and the user's query
95
- messages = [
96
- {"role": "user", "content": [
97
- {"type": "text", "text": text}, # The extracted text from the PDF
98
- {"type": "text", "text": text_query}
99
- ]},
100
- ]
101
- return query_openai(messages, temperature, top_p, max_output_tokens)
102
-
103
- except Exception as e:
104
- return f"Error processing the PDF: {str(e)}"
105
-
106
- # Function to transcribe audio to text using OpenAI Whisper API
107
- def transcribe_audio(audio_binary, openai_api_key):
108
- if not openai_api_key:
109
- return "Error: No API key provided."
110
-
111
- openai.api_key = openai_api_key
112
-
113
- try:
114
- # Use the correct transcription API call
115
- audio_file_obj = io.BytesIO(audio_binary)
116
- audio_file_obj.name = 'audio.wav' # Set a name for the file object (as OpenAI expects it)
117
-
118
- # Transcribe the audio to text using OpenAI's whisper model
119
- audio_file_transcription = openai.Audio.transcribe(file=audio_file_obj, model="whisper-1")
120
- return audio_file_transcription.text
121
- except Exception as e:
122
- return f"Error transcribing audio: {str(e)}"
123
-
124
- # Function to clear the chat (Fix: Returns the correct number of outputs)
125
- def clear_chat():
126
- return "", "", "", "", "", "", "", None, "", None, "", 1.0, 1.0, 2048
127
-
128
- # Gradio UI Layout
129
- with gr.Blocks() as demo:
130
- gr.Markdown("## GPT-4.5 Preview Chatbot")
131
-
132
- # Accordion for explaining hyperparameters
133
- with gr.Accordion("Hyperparameters", open=False):
134
- gr.Markdown("""
135
- ### Temperature:
136
- Controls the randomness of the model's output. A lower temperature makes the model more deterministic, while a higher temperature makes it more creative and varied.
137
- ### Top-P (Nucleus Sampling):
138
- Controls the cumulative probability distribution from which the model picks the next word. A lower value makes the model more focused and deterministic, while a higher value increases randomness.
139
- ### Max Output Tokens:
140
- Limits the number of tokens (words or subwords) the model can generate in its response. You can use this to control the length of the response.
141
- """)
142
-
143
- gr.HTML("""
144
- <style>
145
- #api_key_button {
146
- margin-top: 27px; /* Add margin-top to the button */
147
- background: linear-gradient(135deg, #4a00e0 0%, #8e2de2 100%); /* Purple gradient */
148
- }
149
- #api_key_button:hover {
150
- background: linear-gradient(135deg, #5b10f1 0%, #9f3ef3 100%); /* Slightly lighter */
151
- }
152
- #clear_chat_button {
153
- background: linear-gradient(135deg, #e53e3e 0%, #f56565 100%); /* Red gradient */
154
- }
155
- #clear_chat_button:hover {
156
- background: linear-gradient(135deg, #c53030 0%, #e53e3e 100%); /* Slightly darker red gradient on hover */
157
- }
158
- #ask_button {
159
- background: linear-gradient(135deg, #fbd38d 0%, #f6e05e 100%); /* Yellow gradient */
160
- }
161
- #ask_button:hover {
162
- background: linear-gradient(135deg, #ecc94b 0%, #fbd38d 100%); /* Slightly darker yellow gradient on hover */
163
- }
164
- </style>
165
- """)
166
-
167
- # API Key Input
168
- with gr.Row():
169
- api_key_input = gr.Textbox(label="Enter OpenAI API Key", type="password")
170
- api_key_button = gr.Button("Set API Key", elem_id="api_key_button")
171
- api_key_output = gr.Textbox(label="API Key Status", interactive=False)
172
-
173
- with gr.Row():
174
- temperature = gr.Slider(0, 2, value=1.0, step=0.1, label="Temperature")
175
- top_p = gr.Slider(0, 1, value=1.0, step=0.1, label="Top-P")
176
- max_output_tokens = gr.Slider(0, 16384, value=2048, step=512, label="Max Output Tokens") # Changed default to 2048
177
-
178
- with gr.Tabs():
179
- with gr.Tab("Image URL Chat"):
180
- image_url = gr.Textbox(label="Enter Image URL")
181
- image_query = gr.Textbox(label="Ask about the Image")
182
- image_url_output = gr.Textbox(label="Response", interactive=False)
183
- image_url_button = gr.Button("Ask",elem_id="ask_button")
184
-
185
- with gr.Tab("Text Chat"):
186
- text_query = gr.Textbox(label="Enter your query")
187
- text_output = gr.Textbox(label="Response", interactive=False)
188
- text_button = gr.Button("Ask",elem_id="ask_button")
189
-
190
- with gr.Tab("Image Chat"):
191
- image_upload = gr.File(label="Upload an Image", type="filepath")
192
- image_text_query = gr.Textbox(label="Ask about the uploaded image")
193
- image_output = gr.Textbox(label="Response", interactive=False)
194
- image_button = gr.Button("Ask",elem_id="ask_button")
195
-
196
- with gr.Tab("PDF Chat"):
197
- pdf_upload = gr.File(label="Upload a PDF", type="filepath")
198
- pdf_text_query = gr.Textbox(label="Ask about the uploaded PDF")
199
- pdf_output = gr.Textbox(label="Response", interactive=False)
200
- pdf_button = gr.Button("Ask",elem_id="ask_button")
201
-
202
- with gr.Tab("Voice Chat"):
203
- audio_upload = gr.File(label="Upload an Audio File", type="binary")
204
- audio_query = gr.Textbox(label="Ask about the transcription")
205
- audio_output = gr.Textbox(label="Response", interactive=False)
206
- audio_button = gr.Button("Ask",elem_id="ask_button")
207
-
208
- # Clear chat button
209
- clear_button = gr.Button("Clear Chat",elem_id="clear_chat_button")
210
-
211
- # Button Click Actions
212
- api_key_button.click(set_api_key, inputs=[api_key_input], outputs=[api_key_output])
213
- image_url_button.click(image_url_chat, [image_url, image_query, temperature, top_p, max_output_tokens], image_url_output)
214
- text_button.click(text_chat, [text_query, temperature, top_p, max_output_tokens], text_output)
215
- image_button.click(image_chat, [image_upload, image_text_query, temperature, top_p, max_output_tokens], image_output)
216
- pdf_button.click(pdf_chat, [pdf_upload, pdf_text_query, temperature, top_p, max_output_tokens], pdf_output)
217
-
218
- # For Voice Chat
219
- audio_button.click(
220
- lambda audio_binary, query, temperature, top_p, max_output_tokens: query_openai(
221
- [{"role": "user", "content": [{"type": "text", "text": transcribe_audio(audio_binary, api_key)}, {"type": "text", "text": query}]}],
222
- temperature, top_p, max_output_tokens
223
- ), [audio_upload, audio_query, temperature, top_p, max_output_tokens], audio_output
224
- )
225
-
226
- # Fix: Clear button resets all necessary fields correctly
227
- clear_button.click(
228
- clear_chat,
229
- outputs=[
230
- image_url, image_query, image_url_output,
231
- text_query, text_output,
232
- image_text_query, image_output,
233
- pdf_upload, pdf_text_query, pdf_output,
234
- temperature, top_p, max_output_tokens
235
- ]
236
- )
237
-
238
- # Launch Gradio App
239
- if __name__ == "__main__":
240
- demo.launch()