Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -41,19 +41,13 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
41 |
|
42 |
def upload(files: Optional[List[str]], chatbot: CHAT_HISTORY) -> CHAT_HISTORY:
|
43 |
for file in files:
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
image_path = cache_pil_image(image)
|
52 |
-
chatbot.append(((image_path,), None))
|
53 |
-
except Exception as e:
|
54 |
-
# Si no es una imagen, no mostramos vista previa y solo se guarda el archivo
|
55 |
-
print(f"Archivo no es una imagen, se sube como archivo: {file}")
|
56 |
-
chatbot.append(((file,), None))
|
57 |
return chatbot
|
58 |
|
59 |
def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
@@ -64,7 +58,8 @@ def user(text_prompt: str, chatbot: CHAT_HISTORY):
|
|
64 |
def bot(
|
65 |
files: Optional[List[str]],
|
66 |
model_choice: str,
|
67 |
-
chatbot: CHAT_HISTORY
|
|
|
68 |
):
|
69 |
if not GOOGLE_API_KEY:
|
70 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
@@ -79,9 +74,13 @@ def bot(
|
|
79 |
)
|
80 |
|
81 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
82 |
-
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files
|
|
|
|
|
|
|
|
|
83 |
model = genai.GenerativeModel(model_choice)
|
84 |
-
response = model.generate_content(
|
85 |
|
86 |
chatbot[-1][1] = ""
|
87 |
for chunk in response:
|
@@ -101,7 +100,7 @@ text_prompt_component = gr.Textbox(
|
|
101 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
102 |
)
|
103 |
upload_button_component = gr.UploadButton(
|
104 |
-
label="Upload
|
105 |
)
|
106 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
107 |
model_choice_component = gr.Dropdown(
|
@@ -110,6 +109,9 @@ model_choice_component = gr.Dropdown(
|
|
110 |
label="Select Model",
|
111 |
scale=2
|
112 |
)
|
|
|
|
|
|
|
113 |
|
114 |
user_inputs = [
|
115 |
text_prompt_component,
|
@@ -119,6 +121,7 @@ user_inputs = [
|
|
119 |
bot_inputs = [
|
120 |
upload_button_component,
|
121 |
model_choice_component,
|
|
|
122 |
chatbot_component
|
123 |
]
|
124 |
|
@@ -132,6 +135,7 @@ with gr.Blocks() as demo:
|
|
132 |
upload_button_component.render()
|
133 |
run_button_component.render()
|
134 |
model_choice_component.render()
|
|
|
135 |
|
136 |
run_button_component.click(
|
137 |
fn=user,
|
|
|
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):
|
|
|
58 |
def bot(
|
59 |
files: Optional[List[str]],
|
60 |
model_choice: str,
|
61 |
+
chatbot: CHAT_HISTORY,
|
62 |
+
system_prompt: str # A帽adimos el par谩metro para el system prompt
|
63 |
):
|
64 |
if not GOOGLE_API_KEY:
|
65 |
raise ValueError("GOOGLE_API_KEY is not set.")
|
|
|
74 |
)
|
75 |
|
76 |
text_prompt = [chatbot[-1][0]] if chatbot and chatbot[-1][0] and isinstance(chatbot[-1][0], str) else []
|
77 |
+
image_prompt = [preprocess_image(Image.open(file).convert('RGB')) for file in files] if files else []
|
78 |
+
|
79 |
+
# Aqu铆 incluimos el system prompt en el flujo de generaci贸n de contenido
|
80 |
+
prompt = [system_prompt] + text_prompt # El prompt del sistema se agrega al principio
|
81 |
+
|
82 |
model = genai.GenerativeModel(model_choice)
|
83 |
+
response = model.generate_content(prompt + image_prompt, stream=True, generation_config=generation_config)
|
84 |
|
85 |
chatbot[-1][1] = ""
|
86 |
for chunk in response:
|
|
|
100 |
placeholder="Message...", show_label=False, autofocus=True, scale=8
|
101 |
)
|
102 |
upload_button_component = gr.UploadButton(
|
103 |
+
label="Upload Images", file_count="multiple", file_types=["image"], scale=1
|
104 |
)
|
105 |
run_button_component = gr.Button(value="Run", variant="primary", scale=1)
|
106 |
model_choice_component = gr.Dropdown(
|
|
|
109 |
label="Select Model",
|
110 |
scale=2
|
111 |
)
|
112 |
+
system_prompt_component = gr.Textbox(
|
113 |
+
placeholder="Enter system prompt...", show_label=True, scale=8
|
114 |
+
)
|
115 |
|
116 |
user_inputs = [
|
117 |
text_prompt_component,
|
|
|
121 |
bot_inputs = [
|
122 |
upload_button_component,
|
123 |
model_choice_component,
|
124 |
+
system_prompt_component, # A帽adimos el prompt del sistema
|
125 |
chatbot_component
|
126 |
]
|
127 |
|
|
|
135 |
upload_button_component.render()
|
136 |
run_button_component.render()
|
137 |
model_choice_component.render()
|
138 |
+
system_prompt_component.render() # Mostrar el campo para el system prompt
|
139 |
|
140 |
run_button_component.click(
|
141 |
fn=user,
|