Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -27,11 +27,37 @@ 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:
|
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,26 +65,28 @@ 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: 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,
|
62 |
chatbot: CHAT_HISTORY
|
63 |
):
|
64 |
if not GOOGLE_API_KEY:
|
@@ -67,38 +95,31 @@ def bot(
|
|
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,
|
71 |
-
max_output_tokens=8192,
|
72 |
-
top_k=10,
|
73 |
-
top_p=0.9
|
74 |
)
|
75 |
|
76 |
-
text_prompt =
|
77 |
-
|
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
|
84 |
)
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
chatbot[-1]
|
89 |
-
|
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 |
-
#
|
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,
|
@@ -126,24 +147,23 @@ user_inputs = [
|
|
126 |
|
127 |
bot_inputs = [
|
128 |
upload_button_component,
|
129 |
-
model_choice_component,
|
130 |
-
system_instruction_component,
|
131 |
chatbot_component
|
132 |
]
|
133 |
|
134 |
-
#
|
135 |
with gr.Blocks() as demo:
|
136 |
gr.HTML(TITLE)
|
137 |
gr.HTML(SUBTITLE)
|
138 |
with gr.Column():
|
139 |
-
# Campo de selecci贸n de modelo arriba
|
140 |
model_choice_component.render()
|
141 |
chatbot_component.render()
|
142 |
with gr.Row():
|
143 |
text_prompt_component.render()
|
144 |
upload_button_component.render()
|
145 |
run_button_component.render()
|
146 |
-
system_instruction_component.render()
|
147 |
|
148 |
run_button_component.click(
|
149 |
fn=user,
|
|
|
27 |
IMAGE_WIDTH = 512
|
28 |
CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
|
29 |
|
30 |
+
# Funci贸n para transformar el historial del chat
|
31 |
+
def transform_history(history: CHAT_HISTORY):
|
32 |
+
"""
|
33 |
+
Transforma el historial del chat en el formato necesario para el modelo.
|
34 |
+
"""
|
35 |
+
transformed = []
|
36 |
+
for user_input, bot_response in history:
|
37 |
+
if user_input:
|
38 |
+
transformed.append({"role": "user", "content": user_input})
|
39 |
+
if bot_response:
|
40 |
+
transformed.append({"role": "assistant", "content": bot_response})
|
41 |
+
return transformed
|
42 |
+
|
43 |
+
# Funci贸n de generaci贸n de respuesta
|
44 |
+
def response(message: str, history: CHAT_HISTORY):
|
45 |
+
"""
|
46 |
+
Genera una respuesta basada en el historial del chat y el mensaje del usuario.
|
47 |
+
"""
|
48 |
+
global chat
|
49 |
+
chat.history = transform_history(history)
|
50 |
+
model_response = chat.send_message(message)
|
51 |
+
model_response.resolve()
|
52 |
+
return model_response.text
|
53 |
+
|
54 |
+
# Preprocesamiento de im谩genes
|
55 |
def preprocess_image(image: Image.Image) -> Optional[Image.Image]:
|
56 |
if image:
|
57 |
image_height = int(image.height * IMAGE_WIDTH / image.width)
|
58 |
return image.resize((IMAGE_WIDTH, image_height))
|
59 |
|
60 |
+
# Guardar im谩genes en cach茅
|
61 |
def cache_pil_image(image: Image.Image) -> str:
|
62 |
image_filename = f"{uuid.uuid4()}.jpeg"
|
63 |
os.makedirs(IMAGE_CACHE_DIRECTORY, exist_ok=True)
|
|
|
65 |
image.save(image_path, "JPEG")
|
66 |
return image_path
|
67 |
|
68 |
+
# Subir im谩genes
|
69 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
70 |
for file in files:
|
71 |
image = Image.open(file).convert('RGB')
|
72 |
image_preview = preprocess_image(image)
|
73 |
if image_preview:
|
|
|
74 |
gr.Image(image_preview).render()
|
75 |
image_path = cache_pil_image(image)
|
76 |
chatbot.append(((image_path,), None))
|
77 |
return chatbot
|
78 |
|
79 |
+
# Manejo del usuario
|
80 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
81 |
if text_prompt:
|
82 |
chatbot.append((text_prompt, None))
|
83 |
return "", chatbot
|
84 |
|
85 |
+
# Manejo del bot con historial
|
86 |
def bot(
|
87 |
files: Optional[List[str]],
|
88 |
model_choice: str,
|
89 |
+
system_instruction: str,
|
90 |
chatbot: CHAT_HISTORY
|
91 |
):
|
92 |
if not GOOGLE_API_KEY:
|
|
|
95 |
# Configurar la API con la clave
|
96 |
genai.configure(api_key=GOOGLE_API_KEY)
|
97 |
generation_config = genai.types.GenerationConfig(
|
98 |
+
temperature=0.7,
|
99 |
+
max_output_tokens=8192,
|
100 |
+
top_k=10,
|
101 |
+
top_p=0.9
|
102 |
)
|
103 |
|
104 |
+
text_prompt = chatbot[-1][0] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else ""
|
105 |
+
transformed_history = transform_history(chatbot)
|
106 |
+
|
107 |
# Crear el modelo con la instrucci贸n del sistema
|
108 |
model = genai.GenerativeModel(
|
109 |
model_name=model_choice,
|
110 |
generation_config=generation_config,
|
111 |
+
system_instruction=system_instruction
|
112 |
)
|
113 |
|
114 |
+
# Generar la respuesta usando la funci贸n `response`
|
115 |
+
bot_reply = response(text_prompt, transformed_history)
|
116 |
+
chatbot[-1] = (text_prompt, bot_reply)
|
117 |
+
return chatbot
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
+
# Componentes de la interfaz
|
120 |
system_instruction_component = gr.Textbox(
|
121 |
placeholder="Enter system instruction...", show_label=True, scale=8
|
122 |
)
|
|
|
|
|
123 |
chatbot_component = gr.Chatbot(
|
124 |
label='Gemini',
|
125 |
bubble_full_width=False,
|
|
|
147 |
|
148 |
bot_inputs = [
|
149 |
upload_button_component,
|
150 |
+
model_choice_component,
|
151 |
+
system_instruction_component,
|
152 |
chatbot_component
|
153 |
]
|
154 |
|
155 |
+
# Interfaz de usuario
|
156 |
with gr.Blocks() as demo:
|
157 |
gr.HTML(TITLE)
|
158 |
gr.HTML(SUBTITLE)
|
159 |
with gr.Column():
|
|
|
160 |
model_choice_component.render()
|
161 |
chatbot_component.render()
|
162 |
with gr.Row():
|
163 |
text_prompt_component.render()
|
164 |
upload_button_component.render()
|
165 |
run_button_component.render()
|
166 |
+
system_instruction_component.render()
|
167 |
|
168 |
run_button_component.click(
|
169 |
fn=user,
|