JeCabrera commited on
Commit
69a8ba9
·
verified ·
1 Parent(s): 88c5efb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -86
app.py CHANGED
@@ -1,98 +1,162 @@
1
- import gradio as gr
 
 
2
  import os
3
  import time
 
 
 
4
  import google.generativeai as genai
5
- from mimetypes import MimeTypes
6
-
7
- # Configurar la API de Gemini
8
- genai.configure(api_key=os.environ["GOOGLE_API_KEY"])
9
-
10
- def upload_and_process_file(file):
11
- """
12
- Sube y procesa un archivo para usarlo con el modelo de Gemini.
13
- """
14
- if isinstance(file, gr.inputs.File):
15
- file_path = file.name # Obtener la ruta temporal del archivo
16
- else:
17
- raise ValueError("El archivo no se subió correctamente.")
18
-
19
- # Detectar el tipo MIME del archivo
20
- mime = MimeTypes()
21
- mime_type, _ = mime.guess_type(file_path)
22
-
23
- if not mime_type:
24
- raise ValueError("No se pudo determinar el tipo MIME del archivo.")
25
-
26
- # Subir el archivo a Gemini
27
- print(f"Subiendo el archivo '{file_path}' con MIME type '{mime_type}'...")
28
- file = genai.upload_file(file_path, mime_type=mime_type)
29
- print(f"Archivo subido: {file.display_name}, URI: {file.uri}")
30
-
31
- # Esperar a que el archivo esté activo
32
- wait_for_files_active([file])
33
- return file
34
-
35
- def wait_for_files_active(files):
36
- """
37
- Espera a que los archivos subidos a Gemini estén activos y listos para su uso.
38
- """
39
- print("Esperando el procesamiento de los archivos...")
40
  for file in files:
41
- status = genai.get_file(file.name)
42
- while status.state.name == "PROCESSING":
43
- print(".", end="", flush=True)
44
- time.sleep(5)
45
- status = genai.get_file(file.name)
46
-
47
- if status.state.name != "ACTIVE":
48
- raise Exception(f"El archivo {file.name} no pudo procesarse correctamente.")
49
- print("\nTodos los archivos están listos.")
50
-
51
- # Configuración del modelo generativo
52
- generation_config = {
53
- "temperature": 1,
54
- "top_p": 0.95,
55
- "top_k": 40,
56
- "max_output_tokens": 8192,
57
- "response_mime_type": "text/plain",
58
- }
59
-
60
- model = genai.GenerativeModel(
61
- model_name="gemini-1.5-flash",
62
- generation_config=generation_config,
63
- )
64
 
65
- def start_chat_with_file(file, user_input):
66
- """
67
- Inicia una conversación con el modelo utilizando un archivo como entrada.
68
- """
69
- chat_session = model.start_chat(
70
- history=[
71
- {
72
- "role": "user",
73
- "parts": [file],
74
- },
75
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  )
77
 
78
- # Enviar mensaje al modelo
79
- response = chat_session.send_message(user_input)
80
- return response.text
81
 
 
 
 
 
 
 
 
82
 
83
- # Interfaz de Gradio
84
- def process_file(file, user_input):
85
- processed_file = upload_and_process_file(file)
86
- response = start_chat_with_file(processed_file, user_input)
87
- return response
 
88
 
89
- # Crear la interfaz de Gradio
90
- iface = gr.Interface(
91
- fn=process_file,
92
- inputs=[gr.File(label="Sube tu archivo"), gr.Textbox(label="Escribe tu mensaje")],
93
- outputs="text",
94
- live=True
 
 
 
 
95
  )
96
 
97
- # Ejecutar la interfaz
98
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, Tuple, Optional, Union
8
+
9
  import google.generativeai as genai
10
+ import gradio as gr
11
+ from PIL import Image
12
+ from dotenv import load_dotenv
13
+
14
+ # Cargar las variables de entorno desde el archivo .env
15
+ 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:
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)
38
+ image_path = os.path.join(IMAGE_CACHE_DIRECTORY, image_filename)
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
+ gr.Image(image_preview).render()
48
+ image_path = cache_pil_image(image)
49
+ chatbot.append(((image_path,), None))
50
+ return chatbot
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ def user(text_prompt: str, chatbot: CHAT_HISTORY):
53
+ if text_prompt:
54
+ chatbot.append((text_prompt, None))
55
+ return "", chatbot
56
+
57
+ def bot(
58
+ files: Optional[List[str]],
59
+ model_choice: str,
60
+ system_instruction: Optional[str], # Sistema de instrucciones opcional
61
+ chatbot: CHAT_HISTORY
62
+ ):
63
+ if not GOOGLE_API_KEY:
64
+ raise ValueError("GOOGLE_API_KEY is not set.")
65
+
66
+ genai.configure(api_key=GOOGLE_API_KEY)
67
+ generation_config = genai.types.GenerationConfig(
68
+ temperature=0.7,
69
+ max_output_tokens=8192,
70
+ top_k=10,
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 = "1" # O puedes poner un valor predeterminado como "No system instruction provided."
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
+
81
+ model = genai.GenerativeModel(
82
+ model_name=model_choice,
83
+ generation_config=generation_config,
84
+ system_instruction=system_instruction # Usar el valor por defecto si está vacío
85
  )
86
 
87
+ response = model.generate_content(text_prompt + image_prompt, stream=True, generation_config=generation_config)
 
 
88
 
89
+ chatbot[-1][1] = ""
90
+ for chunk in response:
91
+ for i in range(0, len(chunk.text), 10):
92
+ section = chunk.text[i:i + 10]
93
+ chatbot[-1][1] += section
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)
108
+ run_button_component = gr.Button(value="Run", variant="primary", scale=1)
109
+ model_choice_component = gr.Dropdown(
110
+ choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
111
+ value="gemini-1.5-flash",
112
+ label="Select Model",
113
+ scale=2
114
  )
115
 
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(TITLE)
122
+ gr.HTML(SUBTITLE)
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():
128
+ text_prompt_component.render()
129
+ upload_button_component.render()
130
+ run_button_component.render()
131
+
132
+ # Crear el acordeón para la instrucción del sistema al final
133
+ with gr.Accordion("System Instruction", open=False): # Acordeón cerrado por defecto
134
+ system_instruction_component.render()
135
+
136
+ run_button_component.click(
137
+ fn=user,
138
+ inputs=user_inputs,
139
+ outputs=[text_prompt_component, chatbot_component],
140
+ queue=False
141
+ ).then(
142
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
143
+ )
144
+
145
+ text_prompt_component.submit(
146
+ fn=user,
147
+ inputs=user_inputs,
148
+ outputs=[text_prompt_component, chatbot_component],
149
+ queue=False
150
+ ).then(
151
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
152
+ )
153
+
154
+ upload_button_component.upload(
155
+ fn=upload,
156
+ inputs=[upload_button_component, chatbot_component],
157
+ outputs=[chatbot_component],
158
+ queue=False
159
+ )
160
+
161
+ # Lanzar la aplicación
162
+ demo.queue(max_size=99).launch(debug=False, show_error=True)