Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -64,24 +64,27 @@ def cache_pil_image(image: Image.Image) -> str:
|
|
64 |
image.save(image_path, "JPEG")
|
65 |
return image_path
|
66 |
|
67 |
-
def upload(files: Optional[List[
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
return chatbot
|
76 |
|
77 |
def advanced_response(
|
78 |
-
|
|
|
79 |
model_choice: str,
|
80 |
system_instruction: str,
|
81 |
chatbot: List[dict],
|
82 |
):
|
83 |
-
if not files:
|
84 |
-
|
|
|
|
|
85 |
|
86 |
model = genai.GenerativeModel(
|
87 |
model_name=model_choice,
|
@@ -95,13 +98,17 @@ def advanced_response(
|
|
95 |
chat = model.start_chat(history=chatbot)
|
96 |
chat.system_instruction = system_instruction
|
97 |
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
105 |
|
106 |
# Construcci贸n de la interfaz con las dos pesta帽as originales
|
107 |
with gr.Blocks() as demo:
|
@@ -152,7 +159,7 @@ with gr.Blocks() as demo:
|
|
152 |
|
153 |
run_button_2.click(
|
154 |
fn=advanced_response,
|
155 |
-
inputs=[upload_button, model_dropdown_2, system_instruction_2, chatbot_2],
|
156 |
outputs=[chatbot_2],
|
157 |
)
|
158 |
upload_button.upload(
|
|
|
64 |
image.save(image_path, "JPEG")
|
65 |
return image_path
|
66 |
|
67 |
+
def upload(files: Optional[List[gr.File]], chatbot: List[dict]) -> List[dict]:
|
68 |
+
if files:
|
69 |
+
for file in files:
|
70 |
+
image = Image.open(file.name).convert("RGB")
|
71 |
+
image_preview = preprocess_image(image)
|
72 |
+
if image_preview:
|
73 |
+
image_path = cache_pil_image(image)
|
74 |
+
chatbot.append({"role": "user", "content": f"Uploaded image: {image_path}"})
|
75 |
return chatbot
|
76 |
|
77 |
def advanced_response(
|
78 |
+
text_prompt: str,
|
79 |
+
files: Optional[List[gr.File]],
|
80 |
model_choice: str,
|
81 |
system_instruction: str,
|
82 |
chatbot: List[dict],
|
83 |
):
|
84 |
+
if not text_prompt.strip() and not files:
|
85 |
+
chatbot.append({"role": "assistant", "content": "Please provide a prompt or upload an image."})
|
86 |
+
yield chatbot
|
87 |
+
return
|
88 |
|
89 |
model = genai.GenerativeModel(
|
90 |
model_name=model_choice,
|
|
|
98 |
chat = model.start_chat(history=chatbot)
|
99 |
chat.system_instruction = system_instruction
|
100 |
|
101 |
+
if text_prompt:
|
102 |
+
chatbot.append({"role": "user", "content": text_prompt})
|
103 |
+
if files:
|
104 |
+
images = [cache_pil_image(preprocess_image(Image.open(file.name))) for file in files]
|
105 |
+
chatbot.append({"role": "user", "content": f"Uploaded images: {', '.join(images)}"})
|
106 |
|
107 |
+
response = chat.send_message(text_prompt)
|
108 |
+
response.resolve()
|
109 |
+
|
110 |
+
chatbot.append({"role": "assistant", "content": response.text})
|
111 |
+
yield chatbot
|
112 |
|
113 |
# Construcci贸n de la interfaz con las dos pesta帽as originales
|
114 |
with gr.Blocks() as demo:
|
|
|
159 |
|
160 |
run_button_2.click(
|
161 |
fn=advanced_response,
|
162 |
+
inputs=[text_input_2, upload_button, model_dropdown_2, system_instruction_2, chatbot_2],
|
163 |
outputs=[chatbot_2],
|
164 |
)
|
165 |
upload_button.upload(
|