Update app.py
Browse files
app.py
CHANGED
@@ -46,8 +46,7 @@ def bot(
|
|
46 |
google_key: str,
|
47 |
model_name: str,
|
48 |
image_prompt: Optional[Image.Image],
|
49 |
-
video_prompt,
|
50 |
-
file_prompt,
|
51 |
temperature: float,
|
52 |
max_output_tokens: int,
|
53 |
stop_sequences: str,
|
@@ -64,17 +63,6 @@ def bot(
|
|
64 |
|
65 |
text_prompt = chatbot[-1][0]
|
66 |
|
67 |
-
if file_prompt is not None:
|
68 |
-
# Initialize an empty string to store content
|
69 |
-
all_content = ""
|
70 |
-
for file in file_prompt:
|
71 |
-
# Open the file in read binary mode ('rb')
|
72 |
-
with open(file.name, 'rb') as f:
|
73 |
-
# Read the entire file content into a byte string
|
74 |
-
content = f.read()
|
75 |
-
# Decode the byte string to a UTF-8 string (assuming text file)
|
76 |
-
all_content += content.decode('utf-8')
|
77 |
-
text_prompt = all_content + " " + chatbot[-1][0]
|
78 |
genai.configure(api_key=google_key)
|
79 |
generation_config = genai.types.GenerationConfig(
|
80 |
temperature=temperature,
|
@@ -87,41 +75,41 @@ def bot(
|
|
87 |
model = genai.GenerativeModel(model_name)
|
88 |
|
89 |
image_prompt = preprocess_image(image_prompt)
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
elif video_prompt is not None:
|
107 |
model = genai.GenerativeModel(model_name)
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
elif image_prompt is not None:
|
126 |
image_prompt = preprocess_image(image_prompt)
|
127 |
model = genai.GenerativeModel(model_name)
|
@@ -157,15 +145,8 @@ google_key_component = gr.Textbox(
|
|
157 |
visible=GOOGLE_API_KEY is None
|
158 |
)
|
159 |
|
160 |
-
def upload_file(files):
|
161 |
-
file_paths = [file.name for file in files]
|
162 |
-
return file_paths
|
163 |
-
|
164 |
image_prompt_component = gr.Image(type="pil", label="Image", scale=1)
|
165 |
-
video_prompt_component = gr.
|
166 |
-
file_prompt_component = gr.UploadButton("Click to Upload Text Files", file_count="multiple", label="File")
|
167 |
-
|
168 |
-
#model_selection = gr.Dropdown(["gemini-1.0-pro", "gemini-pro-vision","gemini-1.5-flash-latest", "gemini-1.5-pro-latest","gemini-1.0-pro-001"],label="Select Gemini Model",value="gemini-1.0-pro")
|
169 |
model_selection = gr.Dropdown(["gemini-1.5-flash-latest", "gemini-1.5-pro-latest"],label="Select Gemini Model",value="gemini-1.5-pro-latest")
|
170 |
|
171 |
chatbot_component = gr.Chatbot(
|
@@ -217,7 +198,7 @@ top_k_component = gr.Slider(
|
|
217 |
label="Top-K",
|
218 |
info=(
|
219 |
"Top-k changes how the model selects tokens for output. A top-k of 1 means the "
|
220 |
-
"selected token is the most probable among all tokens in the model
|
221 |
"vocabulary (also called greedy decoding), while a top-k of 3 means that the "
|
222 |
"next token is selected from among the 3 most probable tokens (using "
|
223 |
"temperature)."
|
@@ -245,7 +226,6 @@ bot_inputs = [
|
|
245 |
model_selection,
|
246 |
image_prompt_component,
|
247 |
video_prompt_component,
|
248 |
-
file_prompt_component,
|
249 |
temperature_component,
|
250 |
max_output_tokens_component,
|
251 |
stop_sequences_component,
|
@@ -264,9 +244,6 @@ with gr.Blocks() as demo:
|
|
264 |
with gr.Row():
|
265 |
image_prompt_component.render()
|
266 |
video_prompt_component.render()
|
267 |
-
file_prompt_component.render()
|
268 |
-
file_output = gr.File()
|
269 |
-
file_prompt_component.upload(upload_file, file_prompt_component, file_output)
|
270 |
model_selection.render()
|
271 |
chatbot_component.render()
|
272 |
text_prompt_component.render()
|
@@ -308,7 +285,6 @@ with gr.Blocks() as demo:
|
|
308 |
"gemini-1.5-pro-latest",
|
309 |
None,
|
310 |
None,
|
311 |
-
None,
|
312 |
.4,
|
313 |
1024,
|
314 |
"",
|
@@ -322,7 +298,6 @@ with gr.Blocks() as demo:
|
|
322 |
"gemini-1.5-pro-latest",
|
323 |
None,
|
324 |
None,
|
325 |
-
None,
|
326 |
.4,
|
327 |
1024,
|
328 |
"",
|
@@ -336,7 +311,6 @@ with gr.Blocks() as demo:
|
|
336 |
"gemini-1.5-pro-latest",
|
337 |
"./example1.webp",
|
338 |
None,
|
339 |
-
None,
|
340 |
.4,
|
341 |
1024,
|
342 |
"",
|
@@ -350,7 +324,6 @@ with gr.Blocks() as demo:
|
|
350 |
"gemini-1.5-pro-latest",
|
351 |
"./example2.jpg",
|
352 |
None,
|
353 |
-
None,
|
354 |
.4,
|
355 |
1024,
|
356 |
"",
|
@@ -363,8 +336,7 @@ with gr.Blocks() as demo:
|
|
363 |
"",
|
364 |
"gemini-1.5-pro-latest",
|
365 |
None,
|
366 |
-
"./example1.mp4",
|
367 |
-
None,
|
368 |
.4,
|
369 |
1024,
|
370 |
"",
|
@@ -377,8 +349,7 @@ with gr.Blocks() as demo:
|
|
377 |
"",
|
378 |
"gemini-1.5-pro-latest",
|
379 |
None,
|
380 |
-
"./example2.mp4",
|
381 |
-
None,
|
382 |
.4,
|
383 |
1024,
|
384 |
"",
|
@@ -391,8 +362,7 @@ with gr.Blocks() as demo:
|
|
391 |
"",
|
392 |
"gemini-1.5-pro-latest",
|
393 |
None,
|
394 |
-
"./example3.mp4",
|
395 |
-
None,
|
396 |
.4,
|
397 |
1024,
|
398 |
"",
|
@@ -405,5 +375,4 @@ with gr.Blocks() as demo:
|
|
405 |
#cache_examples="lazy",
|
406 |
)
|
407 |
|
408 |
-
demo.queue(max_size=99).launch(debug=False, show_error=True)
|
409 |
-
|
|
|
46 |
google_key: str,
|
47 |
model_name: str,
|
48 |
image_prompt: Optional[Image.Image],
|
49 |
+
video_prompt: List[Path],
|
|
|
50 |
temperature: float,
|
51 |
max_output_tokens: int,
|
52 |
stop_sequences: str,
|
|
|
63 |
|
64 |
text_prompt = chatbot[-1][0]
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
genai.configure(api_key=google_key)
|
67 |
generation_config = genai.types.GenerationConfig(
|
68 |
temperature=temperature,
|
|
|
75 |
model = genai.GenerativeModel(model_name)
|
76 |
|
77 |
image_prompt = preprocess_image(image_prompt)
|
78 |
+
for video_path in video_prompt:
|
79 |
+
video_file = genai.upload_file(path=video_path)
|
80 |
+
while video_file.state.name == "PROCESSING":
|
81 |
+
print('.', end='')
|
82 |
+
time.sleep(10)
|
83 |
+
video_file = genai.get_file(video_file.name)
|
84 |
+
|
85 |
+
if video_file.state.name == "FAILED":
|
86 |
+
raise ValueError(video_file.state.name)
|
87 |
+
|
88 |
+
response = model.generate_content(
|
89 |
+
contents=[video_file, image_prompt, text_prompt],
|
90 |
+
stream=True,
|
91 |
+
generation_config=generation_config,
|
92 |
+
request_options={"timeout": 600})
|
93 |
+
response.resolve()
|
94 |
elif video_prompt is not None:
|
95 |
model = genai.GenerativeModel(model_name)
|
96 |
|
97 |
+
for video_path in video_prompt:
|
98 |
+
video_file = genai.upload_file(path=video_path)
|
99 |
+
while video_file.state.name == "PROCESSING":
|
100 |
+
print('.', end='')
|
101 |
+
time.sleep(10)
|
102 |
+
video_file = genai.get_file(video_file.name)
|
103 |
+
|
104 |
+
if video_file.state.name == "FAILED":
|
105 |
+
raise ValueError(video_file.state.name)
|
106 |
+
|
107 |
+
response = model.generate_content(
|
108 |
+
contents=[video_file, text_prompt],
|
109 |
+
stream=True,
|
110 |
+
generation_config=generation_config,
|
111 |
+
request_options={"timeout": 600})
|
112 |
+
response.resolve()
|
113 |
elif image_prompt is not None:
|
114 |
image_prompt = preprocess_image(image_prompt)
|
115 |
model = genai.GenerativeModel(model_name)
|
|
|
145 |
visible=GOOGLE_API_KEY is None
|
146 |
)
|
147 |
|
|
|
|
|
|
|
|
|
148 |
image_prompt_component = gr.Image(type="pil", label="Image", scale=1)
|
149 |
+
video_prompt_component = gr.File(label="Video", file_count="multiple")
|
|
|
|
|
|
|
150 |
model_selection = gr.Dropdown(["gemini-1.5-flash-latest", "gemini-1.5-pro-latest"],label="Select Gemini Model",value="gemini-1.5-pro-latest")
|
151 |
|
152 |
chatbot_component = gr.Chatbot(
|
|
|
198 |
label="Top-K",
|
199 |
info=(
|
200 |
"Top-k changes how the model selects tokens for output. A top-k of 1 means the "
|
201 |
+
"selected token is the most probable among all tokens in the model's "
|
202 |
"vocabulary (also called greedy decoding), while a top-k of 3 means that the "
|
203 |
"next token is selected from among the 3 most probable tokens (using "
|
204 |
"temperature)."
|
|
|
226 |
model_selection,
|
227 |
image_prompt_component,
|
228 |
video_prompt_component,
|
|
|
229 |
temperature_component,
|
230 |
max_output_tokens_component,
|
231 |
stop_sequences_component,
|
|
|
244 |
with gr.Row():
|
245 |
image_prompt_component.render()
|
246 |
video_prompt_component.render()
|
|
|
|
|
|
|
247 |
model_selection.render()
|
248 |
chatbot_component.render()
|
249 |
text_prompt_component.render()
|
|
|
285 |
"gemini-1.5-pro-latest",
|
286 |
None,
|
287 |
None,
|
|
|
288 |
.4,
|
289 |
1024,
|
290 |
"",
|
|
|
298 |
"gemini-1.5-pro-latest",
|
299 |
None,
|
300 |
None,
|
|
|
301 |
.4,
|
302 |
1024,
|
303 |
"",
|
|
|
311 |
"gemini-1.5-pro-latest",
|
312 |
"./example1.webp",
|
313 |
None,
|
|
|
314 |
.4,
|
315 |
1024,
|
316 |
"",
|
|
|
324 |
"gemini-1.5-pro-latest",
|
325 |
"./example2.jpg",
|
326 |
None,
|
|
|
327 |
.4,
|
328 |
1024,
|
329 |
"",
|
|
|
336 |
"",
|
337 |
"gemini-1.5-pro-latest",
|
338 |
None,
|
339 |
+
["./example1.mp4"],
|
|
|
340 |
.4,
|
341 |
1024,
|
342 |
"",
|
|
|
349 |
"",
|
350 |
"gemini-1.5-pro-latest",
|
351 |
None,
|
352 |
+
["./example2.mp4"],
|
|
|
353 |
.4,
|
354 |
1024,
|
355 |
"",
|
|
|
362 |
"",
|
363 |
"gemini-1.5-pro-latest",
|
364 |
None,
|
365 |
+
["./example3.mp4"],
|
|
|
366 |
.4,
|
367 |
1024,
|
368 |
"",
|
|
|
375 |
#cache_examples="lazy",
|
376 |
)
|
377 |
|
378 |
+
demo.queue(max_size=99).launch(debug=False, show_error=True)
|
|