Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,7 @@
|
|
1 |
-
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 time
|
6 |
import uuid
|
7 |
-
from typing import List,
|
8 |
-
|
9 |
import google.generativeai as genai
|
10 |
import gradio as gr
|
11 |
from PIL import Image
|
@@ -16,16 +12,13 @@ load_dotenv()
|
|
16 |
|
17 |
print("google-generativeai:", genai.__version__)
|
18 |
|
19 |
-
# Obtener la clave de la API de las variables de entorno
|
20 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
21 |
|
22 |
-
# Verificar que la clave de la API esté configurada
|
23 |
if not GOOGLE_API_KEY:
|
24 |
raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
|
25 |
|
26 |
IMAGE_CACHE_DIRECTORY = "/tmp"
|
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:
|
@@ -39,7 +32,7 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
39 |
image.save(image_path, "JPEG")
|
40 |
return image_path
|
41 |
|
42 |
-
def upload(files: Optional[List[str]], chatbot:
|
43 |
for file in files:
|
44 |
image = Image.open(file).convert('RGB')
|
45 |
image_preview = preprocess_image(image)
|
@@ -49,7 +42,7 @@ def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
|
49 |
chatbot.append(((image_path,), None))
|
50 |
return chatbot
|
51 |
|
52 |
-
def user(text_prompt: str, chatbot:
|
53 |
if text_prompt:
|
54 |
chatbot.append((text_prompt, None))
|
55 |
return "", chatbot
|
@@ -57,8 +50,8 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
57 |
def bot(
|
58 |
files: Optional[List[str]],
|
59 |
model_choice: str,
|
60 |
-
system_instruction: Optional[str],
|
61 |
-
chatbot:
|
62 |
):
|
63 |
if not GOOGLE_API_KEY:
|
64 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
@@ -71,9 +64,8 @@ def bot(
|
|
71 |
top_p=0.9
|
72 |
)
|
73 |
|
74 |
-
# Usar el valor por defecto para system_instruction si está vacío
|
75 |
if not system_instruction:
|
76 |
-
system_instruction = "
|
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 []
|
@@ -81,7 +73,7 @@ def bot(
|
|
81 |
model = genai.GenerativeModel(
|
82 |
model_name=model_choice,
|
83 |
generation_config=generation_config,
|
84 |
-
system_instruction=system_instruction
|
85 |
)
|
86 |
|
87 |
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
@@ -94,14 +86,12 @@ def bot(
|
|
94 |
time.sleep(0.01)
|
95 |
yield chatbot
|
96 |
|
97 |
-
# Componente para el acordeón que contiene el cuadro de texto para la instrucción del sistema
|
98 |
system_instruction_component = gr.Textbox(
|
99 |
placeholder="Enter system instruction...",
|
100 |
show_label=True,
|
101 |
scale=8
|
102 |
)
|
103 |
|
104 |
-
# Definir los componentes de entrada y salida
|
105 |
chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False, scale=2, height=300)
|
106 |
text_prompt_component = gr.Textbox(placeholder="Message...", show_label=False, autofocus=True, scale=8)
|
107 |
upload_button_component = gr.UploadButton(label="Upload Images", file_count="multiple", file_types=["image"], scale=1)
|
@@ -116,12 +106,11 @@ model_choice_component = gr.Dropdown(
|
|
116 |
user_inputs = [text_prompt_component, chatbot_component]
|
117 |
bot_inputs = [upload_button_component, model_choice_component, system_instruction_component, chatbot_component]
|
118 |
|
119 |
-
# Definir la interfaz de usuario
|
120 |
with gr.Blocks() as demo:
|
121 |
-
gr.HTML(
|
122 |
-
gr.HTML(
|
|
|
123 |
with gr.Column():
|
124 |
-
# Campo de selección de modelo arriba
|
125 |
model_choice_component.render()
|
126 |
chatbot_component.render()
|
127 |
with gr.Row():
|
@@ -129,8 +118,7 @@ with gr.Blocks() as demo:
|
|
129 |
upload_button_component.render()
|
130 |
run_button_component.render()
|
131 |
|
132 |
-
|
133 |
-
with gr.Accordion("System Instruction", open=False): # Acordeón cerrado por defecto
|
134 |
system_instruction_component.render()
|
135 |
|
136 |
run_button_component.click(
|
@@ -158,5 +146,4 @@ with gr.Blocks() as demo:
|
|
158 |
queue=False
|
159 |
)
|
160 |
|
161 |
-
# Lanzar la aplicación
|
162 |
demo.queue(max_size=99).launch(debug=False, show_error=True)
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import time
|
3 |
import uuid
|
4 |
+
from typing import List, Optional
|
|
|
5 |
import google.generativeai as genai
|
6 |
import gradio as gr
|
7 |
from PIL import Image
|
|
|
12 |
|
13 |
print("google-generativeai:", genai.__version__)
|
14 |
|
|
|
15 |
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
16 |
|
|
|
17 |
if not GOOGLE_API_KEY:
|
18 |
raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
|
19 |
|
20 |
IMAGE_CACHE_DIRECTORY = "/tmp"
|
21 |
IMAGE_WIDTH = 512
|
|
|
22 |
|
23 |
def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
|
24 |
if image:
|
|
|
32 |
image.save(image_path, "JPEG")
|
33 |
return image_path
|
34 |
|
35 |
+
def upload(files: Optional[List[str]], chatbot: List[tuple]) -> List[tuple]:
|
36 |
for file in files:
|
37 |
image = Image.open(file).convert('RGB')
|
38 |
image_preview = preprocess_image(image)
|
|
|
42 |
chatbot.append(((image_path,), None))
|
43 |
return chatbot
|
44 |
|
45 |
+
def user(text_prompt: str, chatbot: List[tuple]) -> Tuple[str, List[tuple]]:
|
46 |
if text_prompt:
|
47 |
chatbot.append((text_prompt, None))
|
48 |
return "", chatbot
|
|
|
50 |
def bot(
|
51 |
files: Optional[List[str]],
|
52 |
model_choice: str,
|
53 |
+
system_instruction: Optional[str],
|
54 |
+
chatbot: List[tuple]
|
55 |
):
|
56 |
if not GOOGLE_API_KEY:
|
57 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
|
|
64 |
top_p=0.9
|
65 |
)
|
66 |
|
|
|
67 |
if not system_instruction:
|
68 |
+
system_instruction = "No system instruction provided."
|
69 |
|
70 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
71 |
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
|
|
|
73 |
model = genai.GenerativeModel(
|
74 |
model_name=model_choice,
|
75 |
generation_config=generation_config,
|
76 |
+
system_instruction=system_instruction
|
77 |
)
|
78 |
|
79 |
response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
|
|
|
86 |
time.sleep(0.01)
|
87 |
yield chatbot
|
88 |
|
|
|
89 |
system_instruction_component = gr.Textbox(
|
90 |
placeholder="Enter system instruction...",
|
91 |
show_label=True,
|
92 |
scale=8
|
93 |
)
|
94 |
|
|
|
95 |
chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False, scale=2, height=300)
|
96 |
text_prompt_component = gr.Textbox(placeholder="Message...", show_label=False, autofocus=True, scale=8)
|
97 |
upload_button_component = gr.UploadButton(label="Upload Images", file_count="multiple", file_types=["image"], scale=1)
|
|
|
106 |
user_inputs = [text_prompt_component, chatbot_component]
|
107 |
bot_inputs = [upload_button_component, model_choice_component, system_instruction_component, chatbot_component]
|
108 |
|
|
|
109 |
with gr.Blocks() as demo:
|
110 |
+
gr.HTML("<h1 align='center'>Gemini Playground ✨</h1>")
|
111 |
+
gr.HTML("<h2 align='center'>Play with Gemini Pro and Gemini Pro Vision</h2>")
|
112 |
+
|
113 |
with gr.Column():
|
|
|
114 |
model_choice_component.render()
|
115 |
chatbot_component.render()
|
116 |
with gr.Row():
|
|
|
118 |
upload_button_component.render()
|
119 |
run_button_component.render()
|
120 |
|
121 |
+
with gr.Accordion("System Instruction", open=False):
|
|
|
122 |
system_instruction_component.render()
|
123 |
|
124 |
run_button_component.click(
|
|
|
146 |
queue=False
|
147 |
)
|
148 |
|
|
|
149 |
demo.queue(max_size=99).launch(debug=False, show_error=True)
|