joeyutong commented on code in PR #1382:
URL: https://github.com/apache/flink-agents/pull/1382#discussion_r4695366308


##########
plan/src/main/java/org/apache/flink/agents/plan/resource/python/PythonMediaModelSetup.java:
##########
@@ -0,0 +1,548 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.agents.plan.resource.python;
+
+import org.apache.flink.agents.api.chat.messages.AudioBlock;
+import org.apache.flink.agents.api.chat.messages.DataContentBlock;
+import org.apache.flink.agents.api.chat.messages.ImageBlock;
+import org.apache.flink.agents.api.chat.messages.TextBlock;
+import org.apache.flink.agents.api.chat.messages.VideoBlock;
+import org.apache.flink.agents.api.media.AsyncJobModel;
+import org.apache.flink.agents.api.media.AsyncJobPolling;
+import org.apache.flink.agents.api.media.BaseImageGenerationModelSetup;
+import org.apache.flink.agents.api.media.BaseMediaModelSetup;
+import org.apache.flink.agents.api.media.BaseSpeechSynthesisModelSetup;
+import org.apache.flink.agents.api.media.BaseTranscriptionModelSetup;
+import org.apache.flink.agents.api.media.BaseVideoGenerationModelSetup;
+import org.apache.flink.agents.api.media.ImageGenerationRequest;
+import org.apache.flink.agents.api.media.MediaJob;
+import org.apache.flink.agents.api.media.MediaJobState;
+import org.apache.flink.agents.api.media.MediaResult;
+import org.apache.flink.agents.api.media.SpeechSynthesisRequest;
+import org.apache.flink.agents.api.media.TranscriptionRequest;
+import org.apache.flink.agents.api.media.VideoGenerationRequest;
+import org.apache.flink.agents.api.metrics.FlinkAgentsMetricGroup;
+import org.apache.flink.agents.api.resource.PythonResourceDescriptor;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.resource.ResourceType;
+import org.apache.flink.agents.api.utils.StringValidationUtils;
+import org.apache.flink.agents.plan.exceptions.UncapturableException;
+import pemja.core.object.PyObject;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * Factory creating Java {@link BaseMediaModelSetup} bridges around Python 
{@link
+ * ResourceType#MEDIA_MODEL} setup instances.
+ *
+ * <p>Each created resource extends the task-specific {@code Base*ModelSetup} 
class and implements
+ * {@link AsyncJobModel} if and only if the underlying Python resource 
supports async jobs.
+ */
+public final class PythonMediaModelSetup {
+    private PythonMediaModelSetup() {}
+
+    /** Inspect the Python media model setup and return a matching typed Java 
wrapper. */
+    public static BaseMediaModelSetup create(
+            PythonResourceAdapter adapter,
+            PyObject resource,
+            ResourceDescriptor descriptor,
+            @Nullable ResourceContext resourceContext) {
+        Objects.requireNonNull(descriptor, "descriptor");
+        Object inspection = 
adapter.invoke("python_java_utils.inspect_media_model", resource);
+        if (!(inspection instanceof Map)) {
+            throw new IllegalStateException("Invalid MediaModel inspection 
result from Python");
+        }
+        Map<?, ?> metadata = (Map<?, ?>) inspection;
+        Object taskValue = metadata.get("task");

Review Comment:
   Done. Extracted named constants for the metadata keys, task identifiers, and 
Python entry-point names in `PythonMediaModelSetup` and `PythonMediaBridge`.



##########
plan/src/main/java/org/apache/flink/agents/plan/resource/python/PythonMediaModelSetup.java:
##########
@@ -0,0 +1,548 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.agents.plan.resource.python;
+
+import org.apache.flink.agents.api.chat.messages.AudioBlock;
+import org.apache.flink.agents.api.chat.messages.DataContentBlock;
+import org.apache.flink.agents.api.chat.messages.ImageBlock;
+import org.apache.flink.agents.api.chat.messages.TextBlock;
+import org.apache.flink.agents.api.chat.messages.VideoBlock;
+import org.apache.flink.agents.api.media.AsyncJobModel;
+import org.apache.flink.agents.api.media.AsyncJobPolling;
+import org.apache.flink.agents.api.media.BaseImageGenerationModelSetup;
+import org.apache.flink.agents.api.media.BaseMediaModelSetup;
+import org.apache.flink.agents.api.media.BaseSpeechSynthesisModelSetup;
+import org.apache.flink.agents.api.media.BaseTranscriptionModelSetup;
+import org.apache.flink.agents.api.media.BaseVideoGenerationModelSetup;
+import org.apache.flink.agents.api.media.ImageGenerationRequest;
+import org.apache.flink.agents.api.media.MediaJob;
+import org.apache.flink.agents.api.media.MediaJobState;
+import org.apache.flink.agents.api.media.MediaResult;
+import org.apache.flink.agents.api.media.SpeechSynthesisRequest;
+import org.apache.flink.agents.api.media.TranscriptionRequest;
+import org.apache.flink.agents.api.media.VideoGenerationRequest;
+import org.apache.flink.agents.api.metrics.FlinkAgentsMetricGroup;
+import org.apache.flink.agents.api.resource.PythonResourceDescriptor;
+import org.apache.flink.agents.api.resource.ResourceContext;
+import org.apache.flink.agents.api.resource.ResourceDescriptor;
+import org.apache.flink.agents.api.resource.ResourceType;
+import org.apache.flink.agents.api.utils.StringValidationUtils;
+import org.apache.flink.agents.plan.exceptions.UncapturableException;
+import pemja.core.object.PyObject;
+
+import javax.annotation.Nullable;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * Factory creating Java {@link BaseMediaModelSetup} bridges around Python 
{@link
+ * ResourceType#MEDIA_MODEL} setup instances.
+ *
+ * <p>Each created resource extends the task-specific {@code Base*ModelSetup} 
class and implements
+ * {@link AsyncJobModel} if and only if the underlying Python resource 
supports async jobs.
+ */
+public final class PythonMediaModelSetup {
+    private PythonMediaModelSetup() {}
+
+    /** Inspect the Python media model setup and return a matching typed Java 
wrapper. */
+    public static BaseMediaModelSetup create(
+            PythonResourceAdapter adapter,
+            PyObject resource,
+            ResourceDescriptor descriptor,
+            @Nullable ResourceContext resourceContext) {
+        Objects.requireNonNull(descriptor, "descriptor");
+        Object inspection = 
adapter.invoke("python_java_utils.inspect_media_model", resource);
+        if (!(inspection instanceof Map)) {
+            throw new IllegalStateException("Invalid MediaModel inspection 
result from Python");
+        }
+        Map<?, ?> metadata = (Map<?, ?>) inspection;
+        Object taskValue = metadata.get("task");
+        Object asyncValue = metadata.get("async");
+        if (!(taskValue instanceof String) || !(asyncValue instanceof 
Boolean)) {
+            throw new IllegalStateException("Invalid MediaModel inspection 
result from Python");
+        }
+        String task = (String) taskValue;
+        boolean async = (Boolean) asyncValue;
+        final String connectionName;
+        final String model;
+        @Nullable final String voice;
+        try {
+            connectionName =
+                    
StringValidationUtils.requireNonBlank(metadata.get("connection"), "connection");
+            model = 
StringValidationUtils.requireNonBlank(metadata.get("model"), "model");
+            voice =
+                    "speech_synthesis".equals(task)
+                            ? 
StringValidationUtils.optionalNonBlank(metadata.get("voice"), "voice")
+                            : null;
+        } catch (IllegalArgumentException e) {
+            throw new IllegalStateException("Invalid MediaModel inspection 
result from Python", e);
+        }
+        ResourceDescriptor effectiveDescriptor =
+                descriptorWithInspectedMetadata(descriptor, connectionName, 
model, voice);
+        Handle handle = new Handle(adapter, resource, task, 
effectiveDescriptor, resourceContext);
+        switch (task) {
+            case "speech_synthesis":
+                return async ? new AsyncSpeech(handle) : new Speech(handle);
+            case "transcription":
+                return async ? new AsyncTranscription(handle) : new 
Transcription(handle);
+            case "image_generation":
+                return async ? new AsyncImage(handle) : new Image(handle);
+            case "video_generation":
+                return async ? new AsyncVideo(handle) : new Video(handle);
+            default:
+                throw new IllegalStateException("Invalid MediaModel inspection 
result from Python");
+        }
+    }
+
+    private static ResourceDescriptor descriptorWithInspectedMetadata(
+            ResourceDescriptor descriptor,
+            String connectionName,
+            String model,
+            @Nullable String voice) {
+        Map<String, Object> mergedArgs =
+                descriptor.getInitialArguments() != null
+                        ? new HashMap<>(descriptor.getInitialArguments())
+                        : new HashMap<>();
+        mergedArgs.put("connection", connectionName);
+        mergedArgs.put("model", model);
+        if (voice != null) {
+            mergedArgs.put("voice", voice);
+        } else {
+            mergedArgs.remove("voice");
+        }
+        if (descriptor instanceof PythonResourceDescriptor) {
+            return new PythonResourceDescriptor(
+                    descriptor.getModule(), descriptor.getClazz(), mergedArgs);
+        }
+        return new ResourceDescriptor(descriptor.getModule(), 
descriptor.getClazz(), mergedArgs);
+    }
+
+    static final class Handle {

Review Comment:
   Done. Merged `Handle` into `PythonMediaBridge` as a single stateful bridge 
owning the Python resource, adapter, lifecycle, conversion, and invocation, 
leaving the task-specific wrappers as thin delegates.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to