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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -46
app.py CHANGED
@@ -33,7 +33,7 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
33
 
34
  def bot(
35
  model_choice: str,
36
- system_instruction: Optional[str], # Sistema de instrucciones opcional
37
  chatbot: CHAT_HISTORY
38
  ):
39
  if not GOOGLE_API_KEY:
@@ -47,9 +47,8 @@ def bot(
47
  top_p=0.9
48
  )
49
 
50
- # Usar el valor por defecto para system_instruction si est谩 vac铆o
51
  if not system_instruction:
52
- system_instruction = "1" # O puedes poner un valor predeterminado como "No system instruction provided."
53
 
54
  text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
55
 
@@ -69,59 +68,69 @@ def bot(
69
  time.sleep(0.01)
70
  yield chatbot
71
 
72
- # Componente para el acorde贸n que contiene el cuadro de texto para la instrucci贸n del sistema
73
- system_instruction_component = gr.Textbox(
74
- placeholder="Enter system instruction...",
75
- show_label=True,
76
- scale=8
77
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- # Definir los componentes de entrada y salida
80
  chatbot_component = gr.Chatbot(label='Gemini', bubble_full_width=False, scale=2, height=300)
81
- text_prompt_component = gr.Textbox(placeholder="Message...", show_label=False, autofocus=True, scale=8)
82
- run_button_component = gr.Button(value="Run", variant="primary", scale=1)
83
- model_choice_component = gr.Dropdown(
84
- choices=["gemini-1.5-flash", "gemini-2.0-flash-exp", "gemini-1.5-pro"],
85
- value="gemini-1.5-flash",
86
- label="Select Model",
87
- scale=2
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): # Acorde贸n cerrado por defecto
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
 
33
 
34
  def bot(
35
  model_choice: str,
36
+ system_instruction: Optional[str],
37
  chatbot: CHAT_HISTORY
38
  ):
39
  if not GOOGLE_API_KEY:
 
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
 
 
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