Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ TITLE = """<h1 align="center">Gemini Playground ✨</h1>"""
|
|
2 |
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
3 |
|
4 |
import os
|
|
|
5 |
import uuid
|
6 |
from typing import List, Tuple, Optional, Union
|
7 |
|
@@ -26,46 +27,11 @@ IMAGE_CACHE_DIRECTORY = "/tmp"
|
|
26 |
IMAGE_WIDTH = 512
|
27 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
28 |
|
29 |
-
# Función para transformar el historial del chat
|
30 |
-
def transform_history(history: CHAT_HISTORY):
|
31 |
-
"""
|
32 |
-
Transforma el historial del chat en el formato necesario para el modelo.
|
33 |
-
"""
|
34 |
-
transformed = []
|
35 |
-
for user_input, bot_response in history:
|
36 |
-
if user_input:
|
37 |
-
transformed.append({"role": "user", "content": user_input})
|
38 |
-
if bot_response:
|
39 |
-
transformed.append({"role": "assistant", "content": bot_response})
|
40 |
-
return transformed
|
41 |
-
|
42 |
-
# Función de generación de respuesta
|
43 |
-
def response(message: str, history: CHAT_HISTORY, model: genai.GenerativeModel):
|
44 |
-
"""
|
45 |
-
Genera una respuesta basada en el historial del chat y el mensaje del usuario.
|
46 |
-
"""
|
47 |
-
# Crear el input para el modelo basado en el historial y el mensaje del usuario
|
48 |
-
input_text = "\n".join(
|
49 |
-
[
|
50 |
-
f"User: {item[0]}" if item[0] else f"Bot: {item[1]}"
|
51 |
-
for item in history
|
52 |
-
]
|
53 |
-
)
|
54 |
-
input_text += f"\nUser: {message}"
|
55 |
-
|
56 |
-
# Generar la respuesta
|
57 |
-
model_response = model.generate(input_text)
|
58 |
-
|
59 |
-
# Retornar la respuesta generada
|
60 |
-
return model_response.text
|
61 |
-
|
62 |
-
# Preprocesamiento de imágenes
|
63 |
def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
|
64 |
if image:
|
65 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
66 |
return image.resize((IMAGE_WIDTH, image_height))
|
67 |
|
68 |
-
# Guardar imágenes en caché
|
69 |
def cache_pil_image(image: Image.Image) -> str:
|
70 |
image_filename = f"{uuid.uuid4()}.jpeg"
|
71 |
os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
|
@@ -73,28 +39,26 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
73 |
image.save(image_path, "JPEG")
|
74 |
return image_path
|
75 |
|
76 |
-
# Subir imágenes
|
77 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
78 |
for file in files:
|
79 |
image = Image.open(file).convert('RGB')
|
80 |
image_preview = preprocess_image(image)
|
81 |
if image_preview:
|
|
|
82 |
gr.Image(image_preview).render()
|
83 |
image_path = cache_pil_image(image)
|
84 |
chatbot.append(((image_path,), None))
|
85 |
return chatbot
|
86 |
|
87 |
-
# Manejo del usuario
|
88 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
89 |
if text_prompt:
|
90 |
chatbot.append((text_prompt, None))
|
91 |
return "", chatbot
|
92 |
|
93 |
-
# Manejo del bot con historial
|
94 |
def bot(
|
95 |
files: Optional[List[str]],
|
96 |
model_choice: str,
|
97 |
-
system_instruction: str,
|
98 |
chatbot: CHAT_HISTORY
|
99 |
):
|
100 |
if not GOOGLE_API_KEY:
|
@@ -103,31 +67,38 @@ def bot(
|
|
103 |
# Configurar la API con la clave
|
104 |
genai.configure(api_key=GOOGLE_API_KEY)
|
105 |
generation_config = genai.types.GenerationConfig(
|
106 |
-
temperature=0.7,
|
107 |
-
max_output_tokens=8192,
|
108 |
-
top_k=10,
|
109 |
-
top_p=0.9
|
110 |
)
|
111 |
|
112 |
-
text_prompt = chatbot[-1][0] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else
|
113 |
-
|
114 |
-
|
115 |
# Crear el modelo con la instrucción del sistema
|
116 |
model = genai.GenerativeModel(
|
117 |
model_name=model_choice,
|
118 |
generation_config=generation_config,
|
119 |
-
system_instruction=system_instruction
|
120 |
)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
chatbot[-1] =
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
-
#
|
128 |
system_instruction_component = gr.Textbox(
|
129 |
placeholder="Enter system instruction...", show_label=True, scale=8
|
130 |
)
|
|
|
|
|
131 |
chatbot_component = gr.Chatbot(
|
132 |
label='Gemini',
|
133 |
bubble_full_width=False,
|
@@ -156,22 +127,22 @@ user_inputs = [
|
|
156 |
bot_inputs = [
|
157 |
upload_button_component,
|
158 |
model_choice_component,
|
159 |
-
system_instruction_component,
|
160 |
chatbot_component
|
161 |
]
|
162 |
|
163 |
-
#
|
164 |
with gr.Blocks() as demo:
|
165 |
gr.HTML(TITLE)
|
166 |
gr.HTML(SUBTITLE)
|
167 |
with gr.Column():
|
168 |
-
model_choice_component.render()
|
169 |
chatbot_component.render()
|
170 |
with gr.Row():
|
171 |
text_prompt_component.render()
|
172 |
upload_button_component.render()
|
173 |
run_button_component.render()
|
174 |
-
|
|
|
175 |
|
176 |
run_button_component.click(
|
177 |
fn=user,
|
|
|
2 |
SUBTITLE = """<h2 align="center">Play with Gemini Pro and Gemini Pro Vision</h2>"""
|
3 |
|
4 |
import os
|
5 |
+
import time
|
6 |
import uuid
|
7 |
from typing import List, Tuple, Optional, Union
|
8 |
|
|
|
27 |
IMAGE_WIDTH = 512
|
28 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
|
31 |
if image:
|
32 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
33 |
return image.resize((IMAGE_WIDTH, image_height))
|
34 |
|
|
|
35 |
def cache_pil_image(image: Image.Image) -> str:
|
36 |
image_filename = f"{uuid.uuid4()}.jpeg"
|
37 |
os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
|
|
|
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):
|
54 |
if text_prompt:
|
55 |
chatbot.append((text_prompt, None))
|
56 |
return "", chatbot
|
57 |
|
|
|
58 |
def bot(
|
59 |
files: Optional[List[str]],
|
60 |
model_choice: str,
|
61 |
+
system_instruction: str, # Añadido el parámetro para la instrucción del sistema
|
62 |
chatbot: CHAT_HISTORY
|
63 |
):
|
64 |
if not GOOGLE_API_KEY:
|
|
|
67 |
# Configurar la API con la clave
|
68 |
genai.configure(api_key=GOOGLE_API_KEY)
|
69 |
generation_config = genai.types.GenerationConfig(
|
70 |
+
temperature=0.7, # Valor predeterminado
|
71 |
+
max_output_tokens=8192, # Fijar el límite de tokens a 8,192
|
72 |
+
top_k=10, # Valor predeterminado
|
73 |
+
top_p=0.9 # Valor predeterminado
|
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 |
# Crear el modelo con la instrucción del sistema
|
80 |
model = genai.GenerativeModel(
|
81 |
model_name=model_choice,
|
82 |
generation_config=generation_config,
|
83 |
+
system_instruction=system_instruction # Se pasa la instrucción del sistema
|
84 |
)
|
85 |
|
86 |
+
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
87 |
+
|
88 |
+
chatbot[-1][1] = ""
|
89 |
+
for chunk in response:
|
90 |
+
for i in range(0, len(chunk.text), 10):
|
91 |
+
section = chunk.text[i:i + 10]
|
92 |
+
chatbot[-1][1] += section
|
93 |
+
time.sleep(0.01)
|
94 |
+
yield chatbot
|
95 |
|
96 |
+
# Componente para ingresar la instrucción del sistema
|
97 |
system_instruction_component = gr.Textbox(
|
98 |
placeholder="Enter system instruction...", show_label=True, scale=8
|
99 |
)
|
100 |
+
|
101 |
+
# Definir los componentes de entrada y salida
|
102 |
chatbot_component = gr.Chatbot(
|
103 |
label='Gemini',
|
104 |
bubble_full_width=False,
|
|
|
127 |
bot_inputs = [
|
128 |
upload_button_component,
|
129 |
model_choice_component,
|
130 |
+
system_instruction_component, # Añadido el campo de instrucción del sistema
|
131 |
chatbot_component
|
132 |
]
|
133 |
|
134 |
+
# Definir la interfaz de usuario
|
135 |
with gr.Blocks() as demo:
|
136 |
gr.HTML(TITLE)
|
137 |
gr.HTML(SUBTITLE)
|
138 |
with gr.Column():
|
|
|
139 |
chatbot_component.render()
|
140 |
with gr.Row():
|
141 |
text_prompt_component.render()
|
142 |
upload_button_component.render()
|
143 |
run_button_component.render()
|
144 |
+
model_choice_component.render()
|
145 |
+
system_instruction_component.render() # Mostrar el campo de la instrucción del sistema
|
146 |
|
147 |
run_button_component.click(
|
148 |
fn=user,
|