Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -39,46 +39,15 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
39 |
image.save(image_path, "JPEG")
|
40 |
return image_path
|
41 |
|
42 |
-
def upload_to_gemini(path, mime_type=None):
|
43 |
-
"""Uploads the given file to Gemini."""
|
44 |
-
file = genai.upload_file(path, mime_type=mime_type)
|
45 |
-
print(f"Uploaded file '{file.display_name}' as: {file.uri}")
|
46 |
-
return file
|
47 |
-
|
48 |
-
def wait_for_files_active(files):
|
49 |
-
"""Waits for the given files to be active."""
|
50 |
-
print("Waiting for file processing...")
|
51 |
-
for name in (file.name for file in files):
|
52 |
-
file = genai.get_file(name)
|
53 |
-
while file.state.name == "PROCESSING":
|
54 |
-
print(".", end="", flush=True)
|
55 |
-
time.sleep(10)
|
56 |
-
file = genai.get_file(name)
|
57 |
-
if file.state.name != "ACTIVE":
|
58 |
-
raise Exception(f"File {file.name} failed to process")
|
59 |
-
print("...all files ready")
|
60 |
-
print()
|
61 |
-
|
62 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
63 |
-
gemini_files = []
|
64 |
for file in files:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
# Display a preview of the uploaded image
|
73 |
-
gr.Image(image_preview).render()
|
74 |
-
image_path = cache_pil_image(image)
|
75 |
-
gemini_file = upload_to_gemini(image_path, mime_type="image/jpeg")
|
76 |
-
|
77 |
-
gemini_files.append(gemini_file)
|
78 |
-
|
79 |
-
# Esperar a que los archivos se procesen en Gemini
|
80 |
-
wait_for_files_active(gemini_files)
|
81 |
-
chatbot.append(((gemini_file.uri,), None))
|
82 |
return chatbot
|
83 |
|
84 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
@@ -104,7 +73,7 @@ def bot(
|
|
104 |
)
|
105 |
|
106 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
107 |
-
image_prompt = [preprocess_image(Image.open(file
|
108 |
model = genai.GenerativeModel(model_choice)
|
109 |
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
110 |
|
@@ -125,8 +94,8 @@ chatbot_component = gr.Chatbot(
|
|
125 |
text_prompt_component = gr.Textbox(
|
126 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
127 |
)
|
128 |
-
upload_button_component = gr.
|
129 |
-
label="Upload Images
|
130 |
)
|
131 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
132 |
model_choice_component = gr.Dropdown(
|
|
|
39 |
image.save(image_path, "JPEG")
|
40 |
return image_path
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
|
|
43 |
for file in files:
|
44 |
+
image = Image.open(file).convert('RGB')
|
45 |
+
image_preview = preprocess_image(image)
|
46 |
+
if image_preview:
|
47 |
+
# Display a preview of the uploaded image
|
48 |
+
gr.Image(image_preview).render()
|
49 |
+
image_path = cache_pil_image(image)
|
50 |
+
chatbot.append(((image_path,), None))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return chatbot
|
52 |
|
53 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
|
73 |
)
|
74 |
|
75 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
76 |
+
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
|
77 |
model = genai.GenerativeModel(model_choice)
|
78 |
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
79 |
|
|
|
94 |
text_prompt_component = gr.Textbox(
|
95 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
96 |
)
|
97 |
+
upload_button_component = gr.UploadButton(
|
98 |
+
label="Upload Images", file_count="multiple", file_types=["image"], scale=1
|
99 |
)
|
100 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
101 |
model_choice_component = gr.Dropdown(
|