Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>
|
|
4 |
import os
|
5 |
import time
|
6 |
import uuid
|
7 |
-
from typing import List, Tuple, Optional,
|
8 |
|
9 |
import google.generativeai as genai
|
10 |
import gradio as gr
|
@@ -44,6 +44,7 @@ def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
|
44 |
image = Image.open(file).convert('RGB')
|
45 |
image_preview = preprocess_image(image)
|
46 |
if image_preview:
|
|
|
47 |
gr.Image(image_preview).render()
|
48 |
image_path = cache_pil_image(image)
|
49 |
chatbot.append(((image_path,), None))
|
@@ -57,26 +58,26 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
57 |
def bot(
|
58 |
files: Optional[List[str]],
|
59 |
temperature: float,
|
60 |
-
max_output_tokens: int,
|
61 |
top_k: int,
|
62 |
top_p: float,
|
|
|
63 |
chatbot: CHAT_HISTORY
|
64 |
):
|
65 |
if not GOOGLE_API_KEY:
|
66 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
67 |
|
|
|
68 |
genai.configure(api_key=GOOGLE_API_KEY)
|
69 |
generation_config = genai.types.GenerationConfig(
|
70 |
temperature=temperature,
|
71 |
-
max_output_tokens=
|
72 |
top_k=top_k,
|
73 |
top_p=top_p
|
74 |
)
|
75 |
|
76 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
77 |
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
|
78 |
-
|
79 |
-
model = genai.GenerativeModel(model_name)
|
80 |
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
81 |
|
82 |
chatbot[-1][1] = ""
|
@@ -107,13 +108,6 @@ temperature_component = gr.Slider(
|
|
107 |
step=0.05,
|
108 |
label="Temperature",
|
109 |
)
|
110 |
-
max_output_tokens_component = gr.Slider(
|
111 |
-
minimum=1,
|
112 |
-
maximum=2048,
|
113 |
-
value=1024,
|
114 |
-
step=1,
|
115 |
-
label="Token limit",
|
116 |
-
)
|
117 |
top_k_component = gr.Slider(
|
118 |
minimum=1,
|
119 |
maximum=40,
|
@@ -128,6 +122,12 @@ top_p_component = gr.Slider(
|
|
128 |
step=0.01,
|
129 |
label="Top-P",
|
130 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
user_inputs = [
|
133 |
text_prompt_component,
|
@@ -137,9 +137,9 @@ user_inputs = [
|
|
137 |
bot_inputs = [
|
138 |
upload_button_component,
|
139 |
temperature_component,
|
140 |
-
max_output_tokens_component,
|
141 |
top_k_component,
|
142 |
top_p_component,
|
|
|
143 |
chatbot_component
|
144 |
]
|
145 |
|
@@ -154,10 +154,10 @@ with gr.Blocks() as demo:
|
|
154 |
run_button_component.render()
|
155 |
with gr.Accordion("Parameters", open=False):
|
156 |
temperature_component.render()
|
157 |
-
max_output_tokens_component.render()
|
158 |
with gr.Accordion("Advanced", open=False):
|
159 |
top_k_component.render()
|
160 |
top_p_component.render()
|
|
|
161 |
|
162 |
run_button_component.click(
|
163 |
fn=user,
|
|
|
4 |
import os
|
5 |
import time
|
6 |
import uuid
|
7 |
+
from typing import List, Tuple, Optional, Union
|
8 |
|
9 |
import google.generativeai as genai
|
10 |
import gradio as gr
|
|
|
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))
|
|
|
58 |
def bot(
|
59 |
files: Optional[List[str]],
|
60 |
temperature: float,
|
|
|
61 |
top_k: int,
|
62 |
top_p: float,
|
63 |
+
model_choice: str,
|
64 |
chatbot: CHAT_HISTORY
|
65 |
):
|
66 |
if not GOOGLE_API_KEY:
|
67 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
68 |
|
69 |
+
# Configurar la API con la clave
|
70 |
genai.configure(api_key=GOOGLE_API_KEY)
|
71 |
generation_config = genai.types.GenerationConfig(
|
72 |
temperature=temperature,
|
73 |
+
max_output_tokens=8192, # Fijar el límite de tokens a 8,192
|
74 |
top_k=top_k,
|
75 |
top_p=top_p
|
76 |
)
|
77 |
|
78 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
79 |
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
|
80 |
+
model = genai.GenerativeModel(model_choice)
|
|
|
81 |
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
82 |
|
83 |
chatbot[-1][1] = ""
|
|
|
108 |
step=0.05,
|
109 |
label="Temperature",
|
110 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
top_k_component = gr.Slider(
|
112 |
minimum=1,
|
113 |
maximum=40,
|
|
|
122 |
step=0.01,
|
123 |
label="Top-P",
|
124 |
)
|
125 |
+
model_choice_component = gr.Dropdown(
|
126 |
+
choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
|
127 |
+
value="gemini-1.5-flash",
|
128 |
+
label="Select Model",
|
129 |
+
scale=2
|
130 |
+
)
|
131 |
|
132 |
user_inputs = [
|
133 |
text_prompt_component,
|
|
|
137 |
bot_inputs = [
|
138 |
upload_button_component,
|
139 |
temperature_component,
|
|
|
140 |
top_k_component,
|
141 |
top_p_component,
|
142 |
+
model_choice_component,
|
143 |
chatbot_component
|
144 |
]
|
145 |
|
|
|
154 |
run_button_component.render()
|
155 |
with gr.Accordion("Parameters", open=False):
|
156 |
temperature_component.render()
|
|
|
157 |
with gr.Accordion("Advanced", open=False):
|
158 |
top_k_component.render()
|
159 |
top_p_component.render()
|
160 |
+
model_choice_component.render()
|
161 |
|
162 |
run_button_component.click(
|
163 |
fn=user,
|