JeCabrera commited on
Commit
c99083d
verified
1 Parent(s): da9245b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -75
app.py CHANGED
@@ -3,19 +3,15 @@ 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
 
@@ -23,10 +19,17 @@ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
23
  if not GOOGLE_API_KEY:
24
  raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
25
 
 
 
 
 
26
  IMAGE_WIDTH = 512
27
  CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
28
 
29
  def user(text_prompt: str, chatbot: CHAT_HISTORY):
 
 
 
30
  if text_prompt:
31
  chatbot.append((text_prompt, None))
32
  return "", chatbot
@@ -36,10 +39,9 @@ def bot(
36
  system_instruction: Optional[str],
37
  chatbot: CHAT_HISTORY
38
  ):
39
- if not GOOGLE_API_KEY:
40
- raise ValueError("GOOGLE_API_KEY is not set.")
41
-
42
- genai.configure(api_key=GOOGLE_API_KEY)
43
  generation_config = genai.types.GenerationConfig(
44
  temperature=0.7,
45
  max_output_tokens=8192,
@@ -47,90 +49,79 @@ def bot(
47
  top_p=0.9
48
  )
49
 
 
50
  if not system_instruction:
51
- system_instruction = "1"
 
 
 
52
 
53
- text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
54
-
55
  model = genai.GenerativeModel(
56
  model_name=model_choice,
57
  generation_config=generation_config,
58
- system_instruction=system_instruction
59
  )
60
 
61
- response = model.generate_content(text_prompt, stream=True, generation_config=generation_config)
 
62
 
63
- chatbot[-1][1] = ""
 
64
  for chunk in response:
65
- for i in range(0, len(chunk.text), 10):
66
- section = chunk.text[i:i + 10]
67
- chatbot[-1][1] += section
68
- time.sleep(0.01)
69
- yield chatbot
70
-
71
- def multimodal(file, chatbot: CHAT_HISTORY):
72
- """
73
- Procesa un archivo multimodal (imagen, PDF, texto) y genera resultados
74
- utilizando Gemini Vision y Pro.
75
- """
76
- if not GOOGLE_API_KEY:
77
- raise ValueError("GOOGLE_API_KEY is not set.")
78
-
79
- genai.configure(api_key=GOOGLE_API_KEY)
80
- file_type = file.name.split(".")[-1].lower()
81
-
82
- if file_type in ["png", "jpg", "jpeg"]:
83
- # Procesar im谩genes
84
- image = Image.open(file.name)
85
- chatbot.append(("Image uploaded for analysis.", None))
86
- result = genai.generate_images(
87
- prompt="Analyze this image.",
88
- image=image,
89
- model="gemini-vision"
90
- )
91
- chatbot[-1] = ("Image uploaded for analysis.", result[0]["description"])
92
-
93
- elif file_type == "txt":
94
- # Procesar texto
95
- content = file.read().decode("utf-8")
96
- chatbot.append((content, None))
97
- response = genai.generate_text(prompt=content, model="gemini-pro")
98
- chatbot[-1] = (content, response.result)
99
-
100
- elif file_type == "pdf":
101
- # Procesar PDFs (extraer texto de la primera p谩gina)
102
- import fitz # PyMuPDF
103
- doc = fitz.open(file.name)
104
- page_text = doc[0].get_text() if len(doc) > 0 else "PDF vac铆o."
105
- chatbot.append((page_text, None))
106
- response = genai.generate_text(prompt=page_text, model="gemini-pro")
107
- chatbot[-1] = (page_text, response.result)
108
-
109
- else:
110
- chatbot.append(("Unsupported file type.", "Please upload a valid file."))
111
-
112
- return chatbot
113
-
114
- # Componentes de interfaz actualizados
115
- chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False, scale=2, height=300)
116
- file_upload_component = gr.File(label="Upload File (Image, PDF, or TXT)")
117
- run_button_component = gr.Button(value="Analyze File", variant="primary", scale=1)
118
-
119
- user_inputs = [file_upload_component, chatbot_component]
120
-
121
- # Interfaz actualizada
122
  with gr.Blocks() as demo:
123
  gr.HTML(TITLE)
124
  gr.HTML(SUBTITLE)
125
  with gr.Column():
 
 
126
  chatbot_component.render()
127
- file_upload_component.render()
128
- run_button_component.render()
 
 
 
 
 
129
 
130
  run_button_component.click(
131
- fn=multimodal,
 
 
 
 
 
 
 
 
 
132
  inputs=user_inputs,
133
- outputs=[chatbot_component],
 
 
 
134
  )
135
 
136
  # Lanzar la aplicaci贸n
 
3
 
4
  import os
5
  import time
 
6
  from typing import List, Tuple, Optional, Union
7
 
8
  import google.generativeai as genai
9
  import gradio as gr
 
10
  from dotenv import load_dotenv
11
 
12
  # Cargar las variables de entorno desde el archivo .env
13
  load_dotenv()
14
 
 
 
15
  # Obtener la clave de la API de las variables de entorno
16
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
17
 
 
19
  if not GOOGLE_API_KEY:
20
  raise ValueError("GOOGLE_API_KEY is not set in environment variables.")
21
 
22
+ # Configurar la API
23
+ genai.configure(api_key=GOOGLE_API_KEY)
24
+
25
+ # Constantes
26
  IMAGE_WIDTH = 512
27
  CHAT_HISTORY = List[Tuple[Optional[Union[Tuple[str], str]], Optional[str]]]
28
 
29
  def user(text_prompt: str, chatbot: CHAT_HISTORY):
30
+ """
31
+ Maneja las entradas del usuario en el chatbot.
32
+ """
33
  if text_prompt:
34
  chatbot.append((text_prompt, None))
35
  return "", chatbot
 
39
  system_instruction: Optional[str],
40
  chatbot: CHAT_HISTORY
41
  ):
42
+ """
43
+ Maneja las respuestas del modelo generativo.
44
+ """
 
45
  generation_config = genai.types.GenerationConfig(
46
  temperature=0.7,
47
  max_output_tokens=8192,
 
49
  top_p=0.9
50
  )
51
 
52
+ # Usar un valor predeterminado si system_instruction est谩 vac铆o
53
  if not system_instruction:
54
+ system_instruction = "You are a helpful assistant."
55
+
56
+ # Obtener el prompt m谩s reciente del usuario
57
+ text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] else []
58
 
59
+ # Crear y configurar el modelo generativo
 
60
  model = genai.GenerativeModel(
61
  model_name=model_choice,
62
  generation_config=generation_config,
63
+ system_instruction=system_instruction,
64
  )
65
 
66
+ # Generar contenido usando streaming
67
+ response = model.generate_content(text_prompt, stream=True)
68
 
69
+ # Preparar la respuesta para el chatbot
70
+ chatbot[-1] = (chatbot[-1][0], "")
71
  for chunk in response:
72
+ chatbot[-1] = (chatbot[-1][0], chatbot[-1][1] + chunk.text)
73
+ yield chatbot
74
+
75
+ # Componentes de la interfaz de usuario
76
+ system_instruction_component = gr.Textbox(
77
+ placeholder="Enter system instruction...",
78
+ label="System Instruction",
79
+ lines=2
80
+ )
81
+ chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False, height=300)
82
+ text_prompt_component = gr.Textbox(placeholder="Message...", show_label=False, autofocus=True)
83
+ run_button_component = gr.Button(value="Run", variant="primary")
84
+ model_choice_component = gr.Dropdown(
85
+ choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
86
+ value="gemini-1.5-flash",
87
+ label="Select Model"
88
+ )
89
+
90
+ user_inputs = [text_prompt_component, chatbot_component]
91
+ bot_inputs = [model_choice_component, system_instruction_component, chatbot_component]
92
+
93
+ # Definir la interfaz de usuario
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  with gr.Blocks() as demo:
95
  gr.HTML(TITLE)
96
  gr.HTML(SUBTITLE)
97
  with gr.Column():
98
+ # Campo de selecci贸n de modelo arriba
99
+ model_choice_component.render()
100
  chatbot_component.render()
101
+ with gr.Row():
102
+ text_prompt_component.render()
103
+ run_button_component.render()
104
+
105
+ # Crear el acorde贸n para la instrucci贸n del sistema al final
106
+ with gr.Accordion("System Instruction", open=False):
107
+ system_instruction_component.render()
108
 
109
  run_button_component.click(
110
+ fn=user,
111
+ inputs=user_inputs,
112
+ outputs=[text_prompt_component, chatbot_component],
113
+ queue=False
114
+ ).then(
115
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
116
+ )
117
+
118
+ text_prompt_component.submit(
119
+ fn=user,
120
  inputs=user_inputs,
121
+ outputs=[text_prompt_component, chatbot_component],
122
+ queue=False
123
+ ).then(
124
+ fn=bot, inputs=bot_inputs, outputs=[chatbot_component],
125
  )
126
 
127
  # Lanzar la aplicaci贸n